What is the N+1 problem in GraphQL?
Answer
The N+1 problem occurs when fetching a list of N items triggers N additional database queries to fetch related data — one per item. Example: fetching 100 posts, then fetching the author for each post separately results in 1 + 100 = 101 database queries. In GraphQL, this happens when a resolver for a nested field (like post.author) makes a database call per parent: resolve: (post) => db.user.findById(post.authorId). With 100 posts in the list, this fires 100 separate queries. The solution is DataLoader — a batching and caching utility that collects all individual requests within a single tick of the event loop, then makes a single batched query: SELECT * FROM users WHERE id IN (1, 2, 3, ...). This is one of the most critical performance patterns in any GraphQL server.
Previous
What is the difference between a query and a mutation in terms of execution?
Next
What is DataLoader and how does it solve the N+1 problem?