5
votes

I have the following schema:

type User {
    email: String!,
    user_id: String!,
    img: String!,
},

type Query {
    getUser(user_id: String!): User
}

The schema reflects the fact that I must return an User object. However, I can not always do this, and sometimes I need to return null. For example, if I make a request to the DB, it will return return object or null (if the user was not found).

In my GraphQL schema, I set the type for a particular field. If I try to return a different type than what I set, I get an error. How do I allow a field to return either an object or null?

2
I see only one way nothing to return if DB give null. - Simon Bran

2 Answers

3
votes

According to the spec:

By default, all types in GraphQL are nullable; the null value is a valid response for all of the above types. To declare a type that disallows null, the GraphQL Non‐Null type can be used.

In other words, types in GraphQL are nullable by default. So a field like

getUser: User

may return either a User object or null. A field like

name: String

may return either a String or null. Only by explicitly specifying a field as non-null (in SDL, by appending a ! to the type), can we specify that a field should never return null. For example:

name: String!

It's also important to note that the Promise returned in your resolver must resolve to either null or undefined in order to be coerced into a null value. In other words, if you return an empty object ({}), an empty array ([]) or some other value, GraphQL will treat this as you returning an object and not a null value!

In your schema, the email field on User is String!, meaning it cannot resolve to null. If you run a query like

query {
  getUser(user_id: "1") {
    email
  }
}

and the resolver for getUser returns an empty object ({}), GraphQL will attempt to resolve email, return null for it and blow up because email is not supposed to be null! If, however, getUser resolves to null, none of the child fields will be resolved and you will not get any error.

0
votes

According to Graphql - get full sub-object, or null if doesn't exist, you get the error you describe when you return empty object (i.e. {}) instead of null from your GraphQL function.

I had similar problem: I kept getting the "error: lack the require field" error in GraphQL response until I made sure I was actually returning null, not empty object.