Resolve top-level query fields
A root query needs request-specific data.
GraphQL · Const
A resolver is the server function behind a GraphQL field. GraphQL invokes it during execution, passes field context and inputs, and uses its result to resolve nested selections or produce a leaf value.
A resolver is the server-side function associated with a GraphQL field. When execution reaches that field, GraphQL calls the function, passes the parent value, field arguments, shared context, and resolver metadata, then uses the returned value; nested object results continue execution until scalar or enum leaves.
resolver(obj, args, context, info)| Name | Type | Description |
|---|---|---|
obj | any | The value resolved by the parent field; for a root Query field it is often unused. |
args | object | Arguments supplied to the field in the GraphQL operation. |
context | any | A value shared with every resolver, potentially containing the authenticated user or database access. |
info | object | Information about the field and execution state. |
The value for the field, which may be a scalar, enum, object, list, or asynchronous result that GraphQL completes according to the schema.
const resolvers = {
Query: {
me: (_parent, _args, context) => context.request.auth.user,
},
User: {
name: (user, _args, context) => context.db.getUserFullName(user.id),
},
};Each resolver returns the value for its corresponding field; nested fields can use the parent object to obtain related data.
A root query needs request-specific data.
A nested object field depends on the parent object's identifier.
A field reads from a database or remote service.
A subscription field must obtain event-backed data.
GraphQL calls the resolver associated with each requested field. Execution starts at the root operation fields, and object results trigger execution of the selected child fields until leaf values are produced.
Follow-up: What happens when a resolver returns an object? GraphQL uses the selection set for that object and resolves each requested child field recursively.
In the reference implementation, the resolver receives the previous object, field arguments, shared context, and an information object. The parent value is commonly unused for root Query fields but is useful for nested fields.
Follow-up: Why is context useful? It can carry request-scoped data such as the authenticated user or a database connection.
Resolvers are independent of a particular storage technology. They can retrieve data from databases, services, or other sources, and GraphQL assembles their returned values into the response shape requested by the client.
Follow-up: What if a resolver returns a scalar of the wrong shape? GraphQL applies scalar coercion according to the schema's type contract, or reports an execution error when the value cannot satisfy it.
QueryMutationSubscriptionscalar coercionfield executioncontextselection set