Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

GraphQL · Const

DataLoader

DataLoader batches keyed data fetches and memoizes repeated keys, helping GraphQL resolvers avoid duplicate backend requests. Create it with a function that maps a list of keys to the corresponding objects.

How it works

DataLoader is a utility for a data-fetching layer that accepts keys, resolves them in batches, and caches repeated lookups. In GraphQL resolvers, load() and loadMany() help avoid repeated backend requests for the same data.

Syntax

new DataLoader(keys => Promise.all(keys.map(fetchFunction)))

Parameters

NameTypeDescription
keyslist of keysKeys collected for the batch operation; the example uses REST API URLs.

Returns

The batch function returns the objects corresponding to the supplied keys; individual load calls return the associated value or Promise.

Key points

  • DataLoader is presented as a library for reducing repeated requests caused by GraphQL query execution.
  • Its constructor takes a batch function whose input is keys and whose output is the associated objects.
  • Keys can be identifiers for an underlying source; the documented example uses REST endpoint URLs.
  • Use load() for an individual key and loadMany() when requesting several keys.
  • Memoization prevents duplicate requests for the same URL through one loader instance.

Common mistakes

  • Caching lasts for the loader's lifetime, so create a loader with an appropriate scope rather than assuming it is a permanent cache.
  • The runtime must provide Promise and Map, either natively or through polyfills.

Example: Batch and cache person lookups

import DataLoader from "dataloader";

const personLoader = new DataLoader(async (urls: readonly string[]) =>
  Promise.all(urls.map(fetchPersonByURL))
);

async function fetchPersonByURL(url: string) {
  const response = await fetch(url);
  return response.json();
}

const person = await personLoader.load("https://example.com/person/1");

The loader receives keys, fetches the corresponding people together, and memoizes repeated keys during its lifetime.

When to use it

Prevent duplicate REST requests

Several GraphQL fields need the same remote resource during one operation.

Batch keyed data fetching

Many resolver calls request related records from a backend.

Load individual or multiple keys

A resolver needs one or several resources through a shared loader.

Interview questions

QWhat problem does DataLoader solve?

DataLoader accepts a batch function that resolves a list of objects from a list of keys. It combines requested keys and can memoize results, reducing duplicate backend requests.

Follow-up: What does the batch function receive? It receives the keys collected by the loader, such as URLs, and returns the corresponding results in the batch.

QHow do callers use a DataLoader instance?

Use load() for one key and loadMany() for multiple keys. Both allow callers to request data without repeatedly contacting the backend for the same key.

Follow-up: When is memoization useful? It is useful when multiple resolvers request the same resource during the loader's lifetime.

QWhat does the DataLoader constructor require?

The constructor receives a function that maps a list of keys to a list of results. For URL-backed data, that function can fetch each URL and combine the resulting promises.

Follow-up: What runtime features does the implementation rely on? It relies on Promise and Map, supplied natively or by polyfills.

  • load()
  • loadMany()
  • Promise
  • Map
  • GraphQL schema
  • resolver

Source: Wrapping a REST API in GraphQL. Checked 2026-10-15.