← GraphQL learning pathGraphQL · Module 6 of 10
The N+1 problem and DataLoader
Batch and cache database calls for nested queries.
📖 Key ideas
- Nested fields can trigger one query per item: the N+1 problem.
- DataLoader batches many loads into one query per tick.
- It also caches results within a request.
- Create loaders per request in the context.
- Measure with query logging.
💡 Real-life example: DataLoader is a delivery boy who collects all orders in the lane before going to the shop once.
N+1 problemOne query for a list plus one per item
DataLoaderBatching and caching utility
BatchingCombining many requests into one
Per-request cacheCache cleared after each request