⬡ GraphQL Intermediate

What is batch loading in GraphQL and how does it improve performance?

Answer

Batch loading is the technique of collecting multiple individual data requests and fulfilling them in a single batched operation. In GraphQL, this is primarily implemented via DataLoader. DataLoader uses the JavaScript event loop: within a single tick, it collects all load(key) calls, then on the next tick executes a single batch function with all keys. For databases, this translates to one query with an IN clause instead of N individual queries. Beyond database queries, batch loading applies to external API calls, file reads, and any I/O. DataLoader also implements per-request caching — loading the same key twice in one request returns the cached result without a second database hit.