GraphQL Resolvers and the N+1 Problem (DataLoader)
On this page
How GraphQL resolvers run
Every field in a GraphQL schema can have a resolver: a function returning that field's value. The server walks the query and calls resolvers level by level. A resolver receives:
parent: the object returned by the parent field (the post, forPost.author),args: the field's arguments,context: per-request data like the signed-in user, database handles and loaders,info: details about the query.
Simple, composable, and it hides a trap.
What is the N+1 query problem?
The query asks for 10 posts and each post's author:
{ posts(first: 10) { edges { node { title author { name } } } } }With a straightforward resolver:
Post: {
author: (post) => db.userById(post.authorId), // runs once per post
}The server runs 1 query for the posts and 10 more for authors: N+1. Counting database calls in a test server built with GraphQL Yoga:
without DataLoader: 10 queries(10 author lookups; the posts came from memory.) At 100 posts with comments and their authors, it becomes hundreds of queries per request.
Fixing N+1 with DataLoader
DataLoader collects all load(id) calls made during one tick of the event loop, then calls your batch function once with every ID. In plain words: instead of running to the database each time a resolver asks for a user, it writes the request on a list, waits until every resolver at that level has asked, then makes one trip with the whole list, like a waiter taking the whole table's order before going to the kitchen:
import DataLoader from 'dataloader';
// in the per-request context
loaders: {
user: new DataLoader(async (ids) => {
const rows = await db.usersByIds(ids); // one query: WHERE id IN (...)
return ids.map(id => rows.find(r => r.id === id) ?? null); // same order as ids
}),
},
// the resolver
Post: {
author: (post, _args, ctx) => ctx.loaders.user.load(post.authorId),
}Same query, same server:
with DataLoader: 1 query -> SELECT * FROM users WHERE id IN (10,9,8,7,6,5,4,3,2,1)Three rules make DataLoader correct:
- Return results in the same order as the input IDs, with
null(or an Error) for missing ones. DataLoader matches results by position. - Create loaders per request, in the context. A global loader would cache data across users, leaking one user's data to another and serving stale results.
- Deduplication comes free: if 10 posts share 3 authors, the batch receives 3 IDs.
Other fixes: joins, look-ahead and batch endpoints
- Join or look ahead: inspect the query (
info) and fetch related data with a SQL join up front. Tools like Prisma, Hasura and PostGraphile compile GraphQL to efficient SQL. - Batch across services: a DataLoader can call another service's batch endpoint (
GET /users?ids=1,2,3), not just a database.
Catching N+1 queries before production
- Log the number of database queries per GraphQL operation in development and fail tests if it explodes.
- Tracing (OpenTelemetry) shows a "comb" of identical queries under one request.
- Review new list fields for batching before they ship.
Check yourself
One query loads the posts, then the author resolver runs once per post, issuing a separate query for each author.
DataLoader matches results to callers by position. Databases don't guarantee order for WHERE id IN (...), so map the rows back to the requested ids, returning null or an error for missing ones, or callers get the wrong objects.
A DataLoader caches results by key. Created globally, its cache would persist across requests and users, serving stale data and potentially returning one user's data to another. Per-request loaders batch within a request and are thrown away after.
Count database queries per request in tests or development logs and fail when a query's count grows with the number of items returned.
When a resolver can see that nested fields were requested and fetch everything in one join, for example with look-ahead, avoiding a second round trip.