DataLoader and Batching Solutions
What You'll Learn
You'll discover how the DataLoader pattern solves GraphQL's N+1 query problem through intelligent batching and caching of data-fetching operations.
The Core Problem and Solution
When resolvers fetch data independently (as we saw with the N+1 problem), you might execute hundreds of separate database queries for related data. DataLoader solves this by collecting all data requests within a single execution tick, batching them into one efficient query, and caching results.
Think of DataLoader as a smart grocery shopper: instead of making separate trips to the store for each item your family requests in the next 10 seconds, the shopper waits, collects all requests, removes duplicates, makes one trip, and brings everything back.
How DataLoader Works
Batching: DataLoader delays execution briefly (one event loop tick), collecting all similar requests. Instead of fetching user #5 three separate times, it batches: fetchUsers([5, 12, 5, 23]).
Deduplication: Before executing, DataLoader removes duplicates: [5, 12, 5, 23] becomes [5, 12, 23], executing just three queries instead of four.
Caching: Within a single request, DataLoader caches results. If resolver A and resolver B both need user #5, the second request returns the cached value instantly.
Coalescing: Multiple identical pending requests merge into one, reducing redundant work.
Implementation Pattern
const userLoader = new DataLoader(async (userIds) => {
// Batched function receives array of IDs
const users = await db.getUsersByIds(userIds);
// Must return array matching input order
return userIds.map(id => users.find(u => u.id === id));
});
// In resolvers, use loader instead of direct DB calls
const user = await userLoader.load(userId);
The loader collects multiple load() calls, batches them, and returns individual promises that resolve when the batch completes.
Key Takeaway:
DataLoader eliminates N+1 queries by batching multiple data-fetching requests into single operations, deduplicating redundant calls, and caching results per-request—turning hundreds of queries into one efficient batch.