Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Executing a query that only contains a fragment spread causes the root value to be null. #486

Open
Stock44 opened this issue Aug 18, 2024 · 1 comment

Comments

@Stock44
Copy link

Stock44 commented Aug 18, 2024

Description

If you construct a query that only contains a spread fragment, it appears that the resolver's root argument becomes null. This only happens whenever there are no other fields being queried, only the fragment.

Repro steps

The following is an example of such a query:

fragment SidebarFragment on Query {
    organizations(first: 10) {
        edges {
            node {
                name
                id
            }
        }
    }
}

query MainLayoutMainQuery {
    ...SidebarFragment
}

The following is the resolver for the organizations field:

let organizationField (organizationType: OutputDef<Organization>) =
    Define.AsyncField(
        "organization",
        Nullable organizationType,
        "Gets one of the user's organizations by id",
        [ Define.Input("id", StringType) ],
        fun ctx (root: Root) ->
            let globalId = ctx.Arg<string> "id"

            match globalId with
            | GlobalId("Organization", id) ->
                let repository = buildOrganizationRepository root.Db root.UserId
                repository.Fetch(Guid(id))
            | _ -> raise (GQLMessageException "Invalid organization ID received.")
    )

And the root is a simple record value:

type Root = {
    Db: DbCtx
    UserId: string
}

If the query is executed against this setup, then the root argument of the resolver becomes null, even when that is not a valid value
for the field. This issue completely goes away whenever there is either another field in the main query, as in the following:

fragment SidebarFragment on Query {
    organizations(first: 10) {
        edges {
            node {
                name
                id
            }
        }
    }
}

query MainLayoutMainQuery {
    node(id: "") {
        id
    }
    ...SidebarFragment
}

Or the fragment is not used, and instead is sent inline as normal:

query MainLayoutMainQuery {
    organizations(first: 10) {
        edges {
            node {
                name
                id
            }
        }
    }
}

Expected behavior

The organizations field should be the same for all of the previous queries, and no null error should occur.

Actual behavior

The root argument is null.

Known workarounds

Add a dummy field to the main query, as that somehow makes the root field not be null.

Related information

  • Arch linux 6.10.5
  • FSharp.Data.GraphQL 2.2.1
  • .NET Core 8
@xperiandri
Copy link
Collaborator

Could you prepare a test for this case in the tests project?
Then I can quickly figure out that and fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants