Return Value
Arbitrary return value on field
/connection
Usually, resolver simply returns a single object or scalar value that comply with defined types.
However, the resolver can bypass type definition with type coercion and can return any arbitrary value regardless of the client's query definition.
- e.g. marking the field as required in client Query, but returning
undefined
despite the query requiring the field.
// example using sql getAuthor(_, args){ return sql.raw('SELECT * FROM authors WHERE id = %s', args.id); } // example calling api endpoint posts(author){ return request(`https://api.blog.io/by_author/${author.id}`); } // returning undefined names(author){ return undefined; }
Meaning, you can create any arbitrary connection between any query/node with another query
/node
.
- Each node's connection in graph (defined by resolver) is an independent connection.
- Because resolver/connection is defined on each individual node's field level, seemingly identical connection will yield different results
- e.g.
Query
>User
>Name
connection may have different data thanQuery
>Group
>User
>Name
- e.g.
- Because resolvers return some arbitrary return value, it may even return results that doesn't make logical sense
- e.g.
User.name
may yield123-213-4545
even though it's clearly a phone number if the resolver defines it that way.
- e.g.
- Because resolver/connection is defined on each individual node's field level, seemingly identical connection will yield different results
User { Address { City } Age Name }
User
, Address
, Age
, and Name
are all individual nodes connected by resolver field definitions.
null
or undefined
return value
Indicates that the value for the field could not be found.
If your schema indicates that this resolver's field is nullable
, then the operation result has a null
value at the field's position.
If this resolver's field is not nullable
, Apollo Server sets the field's parent to null
.
If necessary, this process continues up the resolver chain until it reaches a field that is nullable
.
This ensures that a response never includes a null
value for a non-nullable
field. When this happens, the response's errors
property will be populated with relevant errors concerning the nullability of that field.