6.1 KiB
title | description |
---|---|
Resolvers | How to fetch data, select from the results, and join types together |
Resolvers tell GraphQL execution how to fill in data for each field in your query. Resolvers are organized into a one to one mapping to the fields in your GraphQL schema. This section describes how resolvers are written and organized, the default resolver that applies to every field, and the arguments available to resolvers.
Resolver map
In order to respond to queries, a schema needs to have resolve functions for all fields. This collection of functions is called the "resolver map". This map relates the schema fields and types to a function.
const schema = `
type Book {
title: String
author: Author
}
type Author {
books: [Book]
}
type Query {
author: Author
}
`;
const resolvers = {
Query: {
author(parent, args, context, info) {
return find(authors, { id: args.id });
},
},
Author: {
books(author) {
return filter(books, { author: author.name });
},
},
};
Note that you don't have to put all of your resolvers in one object. Refer to the "modularizing the schema" section to learn how to combine multiple resolver maps into one.
Default resolver
Explicit resolvers are not needed for every type, since Apollo Server provides a default that can perform two actions depending on the contents of parent
:
- Return the property from
parent
with the relevant field name - Calls a function on
parent
with the relevant field name and provide the remaining resolver parameters as arguments
For the following schema, the title
field of Book
would not need a resolver if the result of the books
resolver provided a list of objects that already contained a title
field.
type Book {
title: String
}
type Author {
books: [Book]
}
Resolver Signature
In addition to the parent resolvers' value, resolvers receive a couple more arguments. The full resolver function signature contains four positional arguments: (parent, args, context, info)
and can return an object or Promise. Once a promise resolves, then the children resolvers will continue executing. This is useful for fetching data from a backend.
The resolver parameters generally follow this naming convention and are described in detail:
parent
: The object that contains the result returned from the resolver on the parent field, or, in the case of a top-levelQuery
field, therootValue
passed from the server configuration. This argument enables the nested nature of GraphQL queries.args
: An object with the arguments passed into the field in the query. For example, if the field was called withquery{ key(arg: "you meant") }
, theargs
object would be:{ "arg": "you meant" }
.context
: This is an object shared by all resolvers in a particular query, and is used to contain per-request state, including authentication information, dataloader instances, and anything else that should be taken into account when resolving the query. Read this section for an explanation of when and how to use context.info
: This argument should only be used in advanced cases, but it contains information about the execution state of the query, including the field name, path to the field from the root, and more. It's only documented in the GraphQL.js source code.
In addition to returning GraphQL defined scalars, you can return custom scalars for special use cases, such as JSON or big integers.
parent
argument
The first argument to every resolver, parent
, can be a bit confusing at first, but it makes sense when you consider what a GraphQL query looks like:
query {
getAuthor(id: 5){
name
posts {
title
author {
name # this will be the same as the name above
}
}
}
}
Every GraphQL query is a tree of function calls in the server. So the obj
contains the result of parent resolver, in this case:
parent
inQuery.getAuthor
will be whatever the server configuration passed forrootValue
.parent
inAuthor.name
andAuthor.posts
will be the result fromgetAuthor
, likely an Author object from the backend.parent
inPost.title
andPost.author
will be one item from theposts
result array.parent
inAuthor.name
is the result from the abovePost.author
call.
Every resolver function is called according to the nesting of the query. To understand this transition from query to resolvers from another perspective, read this blog post.
Result format
Resolvers in GraphQL can return different kinds of results which are treated differently:
null
orundefined
- this indicates the object could not be found. If your schema says that field is nullable, then the result will have anull
value at that position. If the field isnon-null
, the result will "bubble up" to the nearest nullable field and that result will be set tonull
. This is to ensure that the API consumer never gets anull
value when they were expecting a result.- An array - this is only valid if the schema indicates that the result of a field should be a list. The sub-selection of the query will run once for every item in this array.
- A promise - resolvers often do asynchronous actions like fetching from a database or backend API, so they can return promises. This can be combined with arrays, so a resolver can return a promise that resolves to an array, or an array of promises, and both are handled correctly.
- A scalar or object value - a resolver can also return any other kind of value, which doesn't have any special meaning but is simply passed down into any nested resolvers, as described in the next section.