HomeAbout

Batching

Batching

Batching consolidates multiple requests to a single request when it calls the same data source.

Batching accepts an array of keys, and returns a Promise that resolves to an Array of values.

Before data loaders can work properly, data source (whether another API, or a database) needs to implement a method that accepts multiple keys (such as IDs), and returns multiple objects (batching function).

const DataLoader = require('dataloader'); // supply batching function const userLoader = new DataLoader(keys => myBatchGetUsers(keys)); // loading data-loaded data const user = await userLoader.load(1); const invitedBy = await userLoader.load(user.invitedByID); // Elsewhere in your application const user = await userLoader.load(2); const lastInvited = await userLoader.load(user.lastInvitedID);

In the example above, a naive approach will make four round trips, but Dataloader will make at most two.

AboutContact