Skip to catalogue

276

DataLoader

also request-scoped batching

Collect ids during one request and load them in one query. The cache dies with the request.

What is DataLoader?

A DataLoader batches keys requested in the same tick and caches the results. It turns a loop of findById into one where-in. The cache is per request. A cache that lives on the process serves one user another user’s row. A loader does not replace a join you could have written when you already knew the query.

Why does DataLoader matter when vibe coding?

A GraphQL resolver calls the database per field. The draft then makes a process-wide cache to “fix N+1.” The cache has no tenant key and no expiry. Or it creates a new loader inside the resolver, so nothing is ever batched.

How do you do DataLoader?

One loader per request, per entity. Batch by id. Include the tenant in the key if the id is not globally unique. Do not store the loader on the server object.

How do you ask a model for DataLoader?

Batch (entity) loads with a DataLoader created per request. Do not query inside the loop. Do not cache across requests. Include the tenant in the key. Do not construct the loader inside the resolver.

What goes wrong with DataLoader?

Awaiting inside the loop before the loader can see the rest of the keys. You serialized the batches back into N queries.

adjacent