What is a GraphQL resolver? Connecting fields to data and request work
A resolver is a server function that provides a value for a particular field in a GraphQL schema. A top-level Query resolver can return an object, after which nested field resolvers provide its selected values and GraphQL assembles the response shape. Keep resolvers focused, and design data access, authorization, and errors explicitly so field behavior and cost remain traceable.
A resolver is a server function that provides a value for a particular field in a GraphQL schema. A top-level Query resolver can return an object, after which nested field resolvers provide its selected values and GraphQL assembles the response shape. Keep resolvers focused, and design data access, authorization, and errors explicitly so field behavior and cost remain traceable.
This guide combines the GraphQL execution model with resolver conventions documented for Apollo Server's current JavaScript/TypeScript implementation. Argument names and default-resolution details can vary by implementation; this is not a claim about a particular deployed service.
When does a resolver run?
After parsing and validation, a server executes a valid operation. Each selected field can have a resolver. If it returns an object, execution continues through that object's selected fields until scalar or enum leaf values are reached. For example, Query.product(id:) can find a product, Product.name can read its name, and Product.reviews can fetch reviews.
Not every field needs a custom resolver. Apollo Server's default resolver reads a same-named property from the parent object. If a returned product has id and name, those fields can be populated without custom functions. Check the behavior of your chosen server.
What are the resolver arguments?
Apollo Server can pass four arguments in this order: (parent, args, contextValue, info).
| Argument | Purpose | Example |
|---|---|---|
parent | Value returned by the parent field resolver | Current product for Product.reviews |
args | GraphQL arguments supplied to this field | id from product(id: "p-42") |
contextValue | Request-scoped object shared across resolvers for one operation | Authenticated user, data source, DataLoader |
info | Execution metadata for the field | Field path and schema details for advanced tooling |
Illustrative pseudocode:
const resolvers = {
Query: {
product: (_parent, { id }, context) =>
context.products.findVisibleById(id, context.viewer),
},
Product: {
reviews: (product, { first = 10 }, context) =>
context.reviews.listForProduct(product.id, first),
},
};parent carries data down an object relationship; args contains input for that field. contextValue commonly carries request-specific dependencies. Apollo documents auth information, data sources, database connections, and data loaders as examples. Initialize context per request; do not put user-specific state in a global variable. info contains execution metadata and is usually unnecessary for simple data access.
How should data access and responsibilities be split?
Resolvers connect fields to data sources, but putting all business logic in each resolver makes it hard to maintain. Shared product lookups or authorization rules can live in model, service, or data-source layers, leaving resolvers to pass inputs and return results. The appropriate layer depends on the framework and system.
Passing GraphQL type validation does not mean the caller is authorized to access a resource. Enforce access on the server, not by hiding a field in the client. Apollo documents putting identity or scope in context and applying field-level policy in resolvers. Fetch sensitive data only after authorization and avoid exposing sensitive details in errors.
How can N+1 lookups be reduced?
Field-specific functions are easy to compose, but loading nested data separately for each list item can multiply data-source calls. For example, fetching a product and then separately fetching the author for each of 20 reviews can create repeated calls proportional to the list size. This is a common N+1 pattern.
Batching same-kind key lookups within one request and caching repeated results can reduce fan-out. GraphQL's performance guide describes batching tools such as DataLoader; Apollo documents sharing loaders through request context. Check cache scope, authorization isolation, and that batch results correspond to input key order. Batching groups calls; it does not eliminate the cost of database work or large responses.
Observe errors and performance together
When a resolver cannot find data or raises an execution error, the result depends on the field's nullable contract. A nullable field can produce null with error information; null at a non-null field may propagate to a parent. Clients can receive partial data and errors together. Define how “not found,” “forbidden,” and “temporary failure” map to the service contract instead of hiding all errors or converting every failure to an empty list.
Measure field latency, error rates, data-source calls, and batching behavior to locate bottlenecks. Deep or broad operations and large lists can raise server load, so consider page sizes, depth/complexity limits, and rate limits. Confirm actual performance with instrumentation and representative operations; this guide claims no benchmark result.
Resolver checklist
Does the returned value match the schema field's output type?
Are parent values and field arguments sent to the correct data source?
Is authorization checked on the server, with request state isolated per operation?
Which fields use default property lookup, and which need custom functions?
Have nested list lookups, batching scope, and cache isolation been reviewed?
Do null propagation and partial errors match the client contract?
Can production telemetry identify slow fields, backend calls, and errors?
Sources
[GraphQL execution](https://graphql.org/learn/execution/)
[GraphQL performance](https://graphql.org/learn/performance/)
[Apollo Server resolvers](https://www.apollographql.com/docs/apollo-server/data/resolvers)
[Apollo Server context](https://www.apollographql.com/docs/apollo-server/data/context)
[Apollo Server authentication and authorization](https://www.apollographql.com/docs/apollo-server/security/authentication)