What are resolvers in GraphQL and how do they work?
Learn what GraphQL resolvers are, their four arguments (parent, args, context, info), execution order, and how they fetch data field by field.
Expected Interview Answer
A resolver is a function attached to a field in a GraphQL schema that tells the server how to fetch or compute the value for that field when a query asks for it.
Each field in a query is backed by its own resolver, and the execution engine walks the query tree depth-first, calling resolvers field by field. Every resolver receives four arguments — parent (the resolved value of the field above it), args (the field's arguments), context (shared per-request state like auth or loaders), and info (AST and schema metadata). Resolvers can return plain values or Promises, and if you omit a resolver GraphQL uses a default one that reads the property of the same name off the parent object.
- Decouples the schema shape from the underlying data sources
- Lets each field pull from a different database, REST API, or computed value
- Only fields requested in the query get resolved, avoiding wasted work
- Centralizes access control and business logic per field
- Composes naturally into nested object graphs
AI Mentor Explanation
Think of a scorecard where each statistic — strike rate, economy, average — is not stored but recomputed by a dedicated statistician on request. Ask for strike rate and only that statistician runs the numbers from the raw ball-by-ball log; ignore it and nobody bothers. A resolver is exactly that per-field statistician: it knows how to produce one value on demand, using the match context, and stays idle when you never ask for its figure.
Step-by-Step Explanation
Step 1
Query arrives
The server parses and validates the incoming query against the schema, producing an execution tree of fields.
Step 2
Walk the tree
The engine traverses fields top-down, starting at the root Query/Mutation type.
Step 3
Call the resolver
For each field it invokes the resolver with (parent, args, context, info), awaiting Promises.
Step 4
Pass parent down
The value a resolver returns becomes the parent argument for the resolvers of that field's children.
Step 5
Apply defaults
If a field has no explicit resolver, the default resolver reads parent[fieldName].
Step 6
Assemble response
Resolved values are shaped to exactly match the query and returned as JSON.
What Interviewer Expects
- Naming the four resolver arguments and what each holds
- Understanding depth-first field-by-field execution
- Knowing about the default resolver behavior
- Awareness that resolvers can return Promises for async data
- How context is used for auth and shared per-request state
Common Mistakes
- Thinking one resolver runs for the whole query instead of per field
- Confusing the parent argument with context
- Forgetting resolvers can be asynchronous
- Putting database access logic that ignores context/auth
- Not realizing unrequested fields are never resolved
Best Answer (HR Friendly)
“A resolver is a small function that tells the GraphQL server where to get the data for one specific field a client asked for. When a query comes in, the server runs the matching resolver for each requested field and stitches all the results together into the response.”
Code Example
const resolvers = {
Query: {
user: (parent, args, context, info) => {
return context.db.users.findById(args.id)
},
},
User: {
// parent is the user object returned above
fullName: (parent) => `${parent.firstName} ${parent.lastName}`,
posts: (parent, args, context) => {
return context.db.posts.findByAuthor(parent.id)
},
},
}Follow-up Questions
- What are the four arguments passed to every resolver?
- What does the default resolver do when you omit one?
- How does resolver execution order work for nested fields?
- How would you handle authorization inside a resolver?
- Can a resolver return a Promise, and what happens if it does?
MCQ Practice
1. Which argument does a resolver receive that holds the value returned by the field's parent resolver?
The first argument, parent (sometimes called root or source), is the resolved value of the field one level up in the query tree.
2. What happens if a field has no explicitly defined resolver?
GraphQL supplies a default resolver that returns the property of the same name from the parent object, or calls it if it is a function.
3. Where should shared per-request data like the authenticated user typically live?
context is built once per request and passed to every resolver, making it the standard place for auth, database connections, and loaders.
Flash Cards
What is a resolver? — A function that fetches or computes the value of a single GraphQL field when a query requests it.
Four resolver arguments? — parent, args, context, info.
What is the default resolver? — Built-in behavior that returns parent[fieldName] when no explicit resolver is defined.
Can resolvers be async? — Yes — they can return Promises, and the engine awaits them before resolving child fields.
What flows from parent to child? — A resolver's return value becomes the parent argument of its child fields' resolvers.