Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return an object or null in GraphQL?

Tags:

graphql

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?

like image 266
Simon Bran Avatar asked May 09 '17 11:05

Simon Bran


People also ask

How do I return null in GraphQL?

Null is not a GraphQL type so to return null from a GraphQL mutation you have to return a GraphQL type that is nullable. All GraphQL types are nullable by default so you can just return a bool in the schema but return void from the implementation.

How do you return an object in GraphQL?

To declare a type that disallows null, the GraphQL Non‐Null type can be used. 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.

Does GraphQL return null or undefined?

In JavaScript functions without an explicit return statement implicitly return undefined . So our function creates a Promise and then immediately returns undefined , causing GraphQL to return null for the field.

How do you handle null values in GraphQL?

Nulls in the query A GraphQL query can have fields and inputs, with or without variables. Fields are always optional, whether nullable or not. In inputs, such as field arguments, nullable types are always optional and non‐null types are always required.


2 Answers

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.

like image 99
Daniel Rearden Avatar answered Oct 10 '22 06:10

Daniel Rearden


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.

like image 20
markonovak Avatar answered Oct 10 '22 06:10

markonovak