0

In the API I am developing this graphql query

query { user(id: "nonexistentID") { name email } }

returns

{
  "data": {
    "user": {
      "name": null,
      "email": null
    }
  }
}

But I'd like it to return null on the user like this:

{
  "data": {
    "user": null
  }
}

so the API-user knows that there's no such user. Is this intended behaviour or am I missing something?

The schema definition in graphql-js(express) looks like this:

const schema = new GraphQLSchema({
    query: new GraphQLObjectType({
        name: 'Query',
        fields: {
            user: {
                type: userType,
                args: {
                    id: { type: GraphQLString}
                },
                resolve: function resolve(root, args, info) {
                    return adapters.users.byId(args.id);
                }
            }
        }

    })

});
LongHike
  • 4,016
  • 4
  • 37
  • 76
  • 1
    What is return value of `adapters.users.byId(args.id);`? Can you put a debug statement in the resolve function? – Ahmad Ferdous Jan 19 '17 at 00:10
  • 1
    ^ agreed. It looks like `adapters.users.byId(args.id);` is returning an empty object which causes any of its child resolvers i.e. `name` and `email` to return null. If you want the user to be null make sure `adapters.users.byId(args.id);` is returning null. – mparis Jan 19 '17 at 08:17
  • Thank you both. By checking again, I found that the adapters.user.byId wasn't returning null but instead a promise. I should have immediately noticed the mistake. Thanks again for making me _think_. – LongHike Jan 19 '17 at 08:24

0 Answers0