The GraphQL N+1 problem: reduce repeated fetches in nested resolvers
The GraphQL N+1 problem occurs when a list is fetched once and a nested resolver fetches related data separately for each item, increasing backend calls to 1+N. Batching keys into a multi-item lookup and deduplicating within a request can reduce those calls. A batch loader does not guarantee better performance by itself: verify key-to-result ordering, authorization scope, and actual backend cost.
The GraphQL N+1 problem occurs when a list is fetched once and a nested resolver fetches related data separately for each item, increasing backend calls to 1+N. Batching keys into a multi-item lookup and deduplicating within a request can reduce those calls. A batch loader does not guarantee better performance by itself: verify key-to-result ordering, authorization scope, and actual backend cost.
How it happens
Consider a query returning posts and their authors:
query Posts { posts { id title author { id name } } }If posts fetches the list once and every author resolver loads one author independently, N posts can cause 1 post lookup plus N author lookups. Fifty posts may produce 51 backend calls. Repeated author IDs can also be fetched repeatedly without deduplication.
GraphQL lets fields use independent resolvers. That makes field logic composable, but it does not automatically combine backend access. The operation shape alone does not prove an N+1 issue; resolver and data-source implementations matter.
Batch related lookups
A batch loader collects keys requested within a short execution window and sends them to a multi-key backend operation. JavaScript DataLoader is one implementation with batching and request-local memoization; other languages and frameworks differ.
posts = getPosts() # one list fetch
authorIds = [p.authorId for p in posts]
authors = getAuthors(authorIds) # one batch fetchThe nested resolver asks the loader for each key, and the loader combines compatible calls. This can reduce N individual author fetches to one batch, subject to maximum batch sizes, database parameter limits, and service API limits.
The batch function must return results in the same positions as the input keys. If the backend changes order or omits missing records, map results back to key order and represent missing items as null or an error according to policy.
const authorLoader = new DataLoader(async (authorIds) => {
const rows = await getAuthorsByIds(authorIds);
const byId = new Map(rows.map((author) => [author.id, author]));
return authorIds.map((id) => byId.get(id) ?? null);
});This is an illustrative JavaScript example, assumes getAuthorsByIds supports bulk lookup, and was not run or benchmarked. Other implementations can use equivalent request-scoped loaders or repository methods.
Scope caches and authorization
DataLoader memoization usually avoids repeated reads of the same key within one API request. It is not a shared cache such as Redis. Create loaders per request when users can see different data, then discard them when the request ends. A long-lived shared loader can expose one user's cached result to another request if authorization differs.
Apply tenant and authorization rules inside the batch function too. For mutations that change a value cached during the same request, clear or prime the affected key. This cache does not guarantee database freshness or replace a distributed cache.
Choose the right fix and verify it
Database joins, ORM eager loading, repository-level bulk reads, or selection-set optimizations may be a better fit. A loader may add little when parent objects already contain the data or single reads are cheap. Conversely, an inefficient batch query or an API without bulk support can leave the bottleneck intact.
Identify the slow operation and list size; measure backend calls and latency with tracing or query logs; locate repeated lookups; apply batching with request-scoped loaders; verify ordering, missing keys, isolation, and batch limits; then compare backend calls, latency, and resource use under the same conditions. Resolver invocation count alone is misleading: many resolver calls can become one backend request, while one expensive batch can still be slow.
Checklist
Does a list lead to repeated lookups of the same related data?
Does the loader issue a real bulk backend operation?
Are results aligned with input keys, including missing values?
Are loaders and caches request-scoped and authorization-aware?
Are batch-size and database/service limits respected?
Did you compare backend work, latency, and resource use after the change?
Sources
[GraphQL performance: N+1 and batching](https://graphql.org/learn/performance/)
[GraphQL.js: solving N+1 with DataLoader](https://github.com/graphql/graphql-js/blob/17.x.x/website/pages/docs/n1-dataloader.mdx)
[DataLoader repository: batching and per-request cache](https://github.com/graphql/dataloader)
[Apollo GraphQL overview: performance and DataLoader](https://www.apollographql.com/docs/graphos/resources/concepts/graphql)
[Apollo Server resolvers: monitoring resolver performance](https://www.apollographql.com/docs/apollo-server/data/resolvers)