AdvancedGraphQL & API GatewayResolversN+1DataLoader
GraphQL Resolvers & the N+1 Problem — Vocabulary
5 exercises — Learn resolver vocabulary: resolver chain, arguments, the N+1 problem, DataLoader, and batching.
0 / 5 completed
1 / 5
In GraphQL, a resolver is:
Each field in a GraphQL schema can have a resolver — a function that returns the field's value. Resolvers receive (parent, args, context, info) and can fetch from any source: DB, REST API, cache, etc.
2 / 5
You query 10 orders, each with a user field. The naive resolver makes 10 separate DB calls to fetch users. This is the:
The N+1 problem: for N orders, the user resolver runs N times, making N DB calls. With N=10 orders, that's 1 query (orders) + 10 queries (users) = 11 queries. DataLoader batches them into 1+1=2 queries.
3 / 5
DataLoader solves the N+1 problem by:
DataLoader collects all keys requested by resolvers within one event loop tick, then calls the batch function once with all keys (e.g., SELECT * FROM users WHERE id IN (...)), and distributes results back to each resolver.
4 / 5
The context object in a GraphQL resolver is used to:
The context is built once per request and passed to every resolver. It typically contains the authenticated user, database client, per-request DataLoader instances, and any other shared state — without context, resolvers would need to open their own connections.
5 / 5
In GraphQL resolver arguments, the parent argument is:
The parent (also called root or source) is the value returned by the resolver of the parent field. For Order.user, the parent is the resolved Order object — the user resolver uses parent.userId to fetch the correct user.