Resolve root fields
Fetch a root resource such as the current user from request authentication context.
GraphQL · Const
A field resolver is the server function that supplies a value for a GraphQL field. GraphQL calls resolvers from the root operation fields through nested selections until the requested leaf values are produced.
A field resolver is the server-side function associated with a schema field. GraphQL calls it during execution, starting from root operation fields and continuing through nested selections; the returned value is then completed according to the field's type. Resolvers can use the parent object, field arguments, shared context, and execution metadata.
resolver(obj, args, context, info)| Name | Type | Description |
|---|---|---|
obj | unknown | The previous object; commonly unused for a field on the root Query type. |
args | object | Arguments supplied to the field in the GraphQL operation. |
context | unknown | Shared value available to every resolver, such as the authenticated user or a database handle. |
info | GraphQLResolveInfo | Field-specific information about the current operation and schema; generally for advanced use cases. |
A value for the field's declared type; object values are recursively completed through their selected subfields, while scalar values complete at the leaf.
const resolvers = {
Query: {
me: (_parent, _args, context, _info) => context.request.auth.user,
},
User: {
name: (user, _args, context, _info) =>
context.db.getUserFullName(user.id),
},
};Each resolver receives the parent value and can obtain data from request context or another data source.
Fetch a root resource such as the current user from request authentication context.
Use the parent object's identifier to retrieve a related property from a database or repository.
Access authentication details, database handles, or other request-scoped services shared through context.
Return data from a remote or database-backed source that requires awaiting asynchronous work.
A resolver is the function that supplies a field's value. GraphQL invokes it when executing that field, and nested object results lead to execution of their selected subfields.
Follow-up: What are the resolver arguments? The reference implementation passes the previous object, field arguments, shared context, and field-specific execution information.
The first argument is the previous object, often called the parent value. For a root Query field it is commonly unused; for a nested field it represents the object returned by the parent resolver.
Follow-up: How can a nested resolver fetch related data? It can use an identifier from the parent object and access a repository or database through context.
Resolvers may execute asynchronously, so they can obtain data from remote services or databases before returning a value. GraphQL continues completing the result once the resolver's value is available.
Follow-up: What happens when the value is an object? GraphQL applies the requested selection set recursively until leaf scalar values are reached.
ExecutionQueryMutationSubscriptioncontextGraphQLResolveInfofield errors