DataLoader (GraphQL)
DataLoader is a generic utility to be used as part of your application’s data fetching layer to provide a consistent API over various backends and reduce requests to those backends via batching and caching.
Consider the following GraphQL query
{
users {
id
name
posts {
id
title
}
}
}
For each user, the resolver might query the database to fetch their posts.
- If there are 100 users, this can result in 1 query to fetch users and then 100 additional queries to fetch posts for each user (hence “N+1”).
DataLoader solves the N+1 problem by batching these individual queries into a single query and caching results to prevent redundant queries.
DataLoader is typically used in the context of resolvers in GraphQL. Here’s how it operates:
- Batching: When a resolver calls the DataLoader, it doesn’t immediately execute a database query. Instead, it queues the request. Then, before the response is returned to the client, all the queued requests are bundled into a single query and executed together.
For instance, if multiple users’ posts need to be fetched, instead of making multiple calls like:
SELECT * FROM posts WHERE user_id = 1;
SELECT * FROM posts WHERE user_id = 2;
DataLoader will group them:
SELECT * FROM posts WHERE user_id IN (1, 2);