Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 37 ยท Resolvers and Execution

Resolvers

Learn how GraphQL resolvers work: the (parent, args, context, info) signature, default resolvers, field-by-field execution and context, with a runnable executor.

A schema says what can be asked. Resolvers say how each field gets its value. A resolver is a function attached to a field of a type: when a query selects that field, the server calls the function and uses its return value. The whole GraphQL server is, in essence, a schema plus a map of these functions.

No real GraphQL library runs on this site. The server code you see here in the first blocks is read-only, and the runnable examples use a hand-written executor: about 40 lines of Node that walk a selection and call resolvers the way a real engine does. It leaves out parsing, validation, lists of abstract types and error handling (see How Execution Works and How Errors Behave), but the calling convention and the order of calls are the real thing.

The four arguments

Every resolver receives the same four arguments, in this order:

JavaScript
function resolver(parent, args, context, info) {
  // ...return a value, or a Promise of one
}

Four resolver arguments

parent The object this field belongs to.
args Arguments written in the query.
context Per-request shared data.
info Field name, path and schema details.

Most resolvers use only the first three.

1 / 2
  • parent (sometimes called root or source) is the value returned by the resolver one level up. For a root Query field it is usually undefined (or a root value you configure).
  • args is an object with the arguments of that field, already coerced to the declared types and with defaults applied.
  • context is an object created once per request and shared by all resolvers. It is the place for the logged-in user, database connections and data loaders (The N+1 Problem and DataLoader).
  • info carries the field name, the path in the query, the return type and the schema. You rarely need it, but it is useful for logging and advanced optimizations.

A resolver map mirrors the schema: types at the top level, fields below.

JavaScript
const resolvers = {
  Query: {
    book: (parent, args, context) => context.db.findBook(args.id),
    me: (parent, args, context) => context.user ?? null,
  },
  Book: {
    author: (book, args, context) => context.db.findAuthor(book.authorId),
  },
};

Here Book.author receives the book returned by Query.book as parent, so it can use book.authorId. That is how a graph is walked: each level's output is the next level's parent. (This snippet is read-only: context.db is not a real object here.)

Root versus nested resolvers

Resolvers on Query and Mutation are root resolvers: they are the entry points and typically fetch data. Resolvers on other types are nested resolvers: they get a parent object and often follow a relationship (Book.author) or compute a derived value (Author.displayName). A good rule: root resolvers find the starting objects; nested resolvers fill in the rest only when the client asks for it.

Default resolvers

You do not write a resolver for every field. If a field has none, the engine uses a default resolver: read the property with the field's name from the parent. If that property is a function, call it with (args, context, info).

That is why returning { id: "1", title: "Notes" } from Query.book is enough for title and id: no resolver exists for Book.title, so parent.title is used. You only write resolvers where the data does not line up with the schema: a relationship that needs another lookup, a computed field, or a renamed property.

Field-by-field execution

The executor takes a selection and resolves it one field at a time, depth first, always passing the parent down. For each field:

  1. find the resolver for Type.field (or use the default);
  2. call it with (parent, args, context, info);
  3. wait for the result if it is a Promise;
  4. if the field has a sub-selection, repeat for the returned object (or for each item of a returned list).

Only the fields in the selection are resolved. If the client doesn't ask for author, the Book.author resolver never runs, so no author lookup happens. That is the main performance promise of GraphQL.

Node.js A hand-written executor with a resolver map

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Look at the second run: asking for only title calls just Query.book. The third run shows what happens for a missing book: the resolver returns null, so the engine stops and no nested resolvers run. This is why a book(id:) field is nullable in the schema.

Context: the logged-in user and data sources

context is built once per HTTP request, before any resolver runs. A typical setup authenticates the request in middleware and then builds the context:

JavaScript
// shape only: details depend on the server library
context: async ({ req }) => ({
  user: await authenticate(req.headers.authorization),
  db: createDbConnection(),
  loaders: createLoaders(),
}),

Resolvers then read context.user instead of re-parsing headers. Create new objects (and especially DataLoader instances) per request: a shared one would leak one user's data to another. The next lessons build on this: Authentication and Authorization.

The stand-in below shows the two ideas together: a context built per request, and a resolver that behaves differently for a guest and a signed-in user. Two requests run side by side, each with its own context.

Node.js Stand-in: a context per request

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The same resolvers produce different answers because each request got a different context. The resolver code never looks at headers; authentication happened once, before execution.

Async resolvers and errors

A resolver may return a value or a Promise; async functions are the norm. If a resolver throws or its Promise rejects, that field gets an error and null (following the rules from Lists and Non-Null Types), while sibling fields continue. Keep business logic out of resolvers: they should be thin glue that calls a service layer. That makes the logic testable without GraphQL and keeps resolvers short.

Do not do authorization only inside individual field resolvers and hope you remembered every one. Put the checks in the shared business layer, as explained in Authentication and Authorization.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the first argument of a nested resolver such as Book.author?

    The parent is whatever the parent field's resolver returned. Book.author gets the book and can read book.authorId.
  2. 2. A field has no resolver. What happens?

    The default resolver uses parent[fieldName], and calls it if it is a function.
  3. 3. Where should the logged-in user normally be available to resolvers?

    Context is created per request and shared by all resolvers in that request, which makes it the right place for the user and data sources.
  4. 4. A client does not select author in a query. What happens to Book.author?

    Only fields in the selection are resolved. Unselected fields cost nothing.

Summary

  • A resolver is a function (parent, args, context, info) that provides the value of one field.
  • Each level's result becomes the parent of the next level; the engine resolves only the selected fields.
  • Root resolvers (Query, Mutation) start the walk; nested resolvers follow relationships or compute values.
  • Fields without a resolver use the default one: parent[fieldName].
  • context is per-request: the user, the database and loaders live there.
  • Keep resolvers thin and authorization in the business layer.
  • The executor here is a stand-in; real engines also parse, validate and report errors.

Next: How Execution Works and How Errors Behave.