Resolvers are where GraphQL does its work. Understand the four resolver arguments, return data sync or async, and see how nested fields resolve into a result.
Why: every resolver receives the same four arguments. parent is the result of the field above it; args holds the field's arguments; context is shared per-request data (auth, db connections); info has query metadata you rarely need. Mastering parent and args covers almost everything.
const resolvers = {
Query: {
// parent, args, context, info
book: (_parent, args, _context) => {
return books.find((b) => b.id === args.id)
},
},
}Why: real data comes from a database or another API, so resolvers usually return a Promise — just make the function async and await. GraphQL waits for it. This is how you connect a resolver to Postgres, MongoDB, or even a REST API behind the scenes.
const resolvers = {
Query: {
books: async () => {
const rows = await db.query('SELECT * FROM books')
return rows
},
book: async (_p, { id }) => {
return db.book.findById(id)
},
},
}Why: GraphQL resolves a query field by field, top down. If you do not write a resolver for a field, the default just reads the property of the parent object by name. You only write a resolver when a field needs computation or its own data fetch — like deriving an author's full name from parts.
const resolvers = {
Query: {
authors: () => authors,
},
Author: {
// a computed field with no matching property on the parent
fullName: (author) => author.firstName + ' ' + author.lastName,
},
}Note: GraphQL validates the query against the schema first, then walks it. It resolves Query fields, then for each result resolves the requested sub-fields, recursing down the tree — running independent resolvers in parallel — and assembles the result in the exact shape of the query. Understanding this top-down walk is the key to the N+1 problem two lessons from now.
parse + validate against schema
│
▼
resolve Query.authors ─▶ [a1, a2]
│
├─ resolve Author.fullName for a1
└─ resolve Author.fullName for a2 (run in parallel)
▼
assemble result in the query's shape