GraphQL Resolver Example
One of the most common use cases for dldr is batching GraphQL resolver calls to eliminate the N+1 query problem. When multiple resolvers request the same data, dldr automatically batches these requests into a single operation.The Problem
When executing GraphQL queries with multiple fields that fetch the same type of data, each field resolver typically makes its own database call. This leads to the N+1 query problem:The Solution
Using dldr, all calls within the same tick are automatically batched into a single request:Create a batched loader in context
Add a loader to your GraphQL context using
load.bind(). This loader will batch all calls within the current execution:Complete Example
Here’s a full working example:Key Benefits
- Single database call: All resolver calls within the same tick are batched into one request
- Automatic deduplication: Requesting the same key multiple times only fetches it once
- Type-safe: Full TypeScript support with proper type inference
- Zero configuration: Works out of the box with no setup required