DataLoadermedium3-5 years
Walk through what actually changes when a resolver moves from `@SchemaMapping` (per-object) to `@BatchMapping` (per-level). Does the engine call the new method less often in the same way it would call a cached method less often?
Not in the sense of a cache — a cache would still be hit twenty times, just cheaply. @BatchMapping changes what the resolver is: instead of a method that takes one Order and returns one Customer, it's a method that takes the whole list of Orders from that level and returns a map from each one to its Customer. The engine doesn't call it once per order; as it walks the twenty orders resolving customer on each, it defers every one of those instead of resolving them immediately, collects all twenty keys, and once the whole level is done being walked, dispatches the batch method exactly once with all twenty keys at once. One WHERE id IN (...) replaces twenty separate lookups — not a faster twenty calls, one call.
PreviousA query asks for twenty orders and each order's `customer`. The `customer` resolver has a single `findById` call in it — no loop, nothing obviously wrong. Why does this still hit the database twenty times, and whose "fault" is that, the resolver or the query?Next A teammate adds a `DataLoader` in front of an existing per-object resolver and reports the N+1 is fixed because "DataLoader caches now." Their dashboard still shows one query per customer at runtime. What did they actually fix, and what didn't they?