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

Confusion about undefined and null types #731

Closed
mlamp opened this issue Feb 28, 2017 · 1 comment
Closed

Confusion about undefined and null types #731

mlamp opened this issue Feb 28, 2017 · 1 comment

Comments

@mlamp
Copy link

mlamp commented Feb 28, 2017

Hello.

I'm trying to unset GraphQLObjectType's field by not providing field from resolve function. My expected result would be that when I'm querying that field it would be not set (because resolve gave undefined). Example what I mean, I have type field "test" on root query witch is type Test like so:
type: new GraphQLObjectType({ name: 'Test', fields: { testField: { type: GraphQLString } } }),
And resolve function for it like so:
resolve: () => { return { }; },
While querying like:

{
  test {
    testField
  }
}

I'm expecting to get result: { "data": { "test": {} } } But actually I get

{
  "data": {
    "test": {
      "testField": null
    }
  }
}

Is there a reason why graphQL adds nulls?

@timsuchanek
Copy link
Member

So I only can speak for Sangria, the Scala implementation, there it would be the same.
To understand that behavior, you need to know 2 things:

  1. The Resolver works top down
  2. null is the undefined or None of GraphQL
  3. The query defines the structure of the response

So the resolver goes top down and looks for test, when test is not there because you didn't provide it in {}, the resolver automatically sets all child fields to null.
That is actually expected behavior as a query should always specify the structure of the response.
A client that does this query must be able to assume the fields data.test.testField, otherwise the whole idea of GraphQL wouldn't make sense 😃
When we would just return "data": { "test" : {} } the whole query doesn't make sense as a client now cannot trust the GraphQL typesystem anymore.
I hope that helped

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

3 participants