APIs
GraphQL N+1: multiple-choice review
Six questions that cut across the whole unit. Each one is a decision you make in a real review or incident — not a definition to recite, but a tradeoff to weigh when one HTTP request is quietly firing a thousand SQL queries.
Confirm you can connect resolver isolation, DataLoader batching and scope, batch-function contracts, federation, and query-shape defences — the synthesis the six lessons built toward.
A REST controller serving the same posts-with-authors page issues one JOIN; the GraphQL server issues 51 queries. The schema has no bug. What is the structural cause?
Why does DataLoader batch on the event-loop tick boundary rather than a fixed 5 ms timer, and what does that buy you?
A refactor moved one DataLoader from the Apollo context factory into module scope. DB call rate is 6× normal at unchanged HTTP rate, and one tenant intermittently sees another tenant's rows. Why?
A batchLoadFn runs SELECT ... WHERE id = ANY($1) and returns the rows in the order Postgres gave them. Intermittently, posts show the wrong author. What is the rule it broke?
An Apollo Federation supergraph routes a query referencing 50 users across two subgraphs. What does the router handle, and what does the users subgraph still owe?
DataLoader is live and the page is fast. An attacker sends one document with 1000 root aliases (q1: user(id:1)…q1000: user(id:1000)). DB trips collapse to one batch, yet the server still struggles. Why, and what stops it?
The unit’s through-line is one pipeline: resolver isolation creates N+1, DataLoader collapses the DB trips at the tick boundary but only with per-request scope and a correct same-order batch contract, federation hands you the network batch while you still owe the DB batch in __resolveReference, and query-shape defences (depth, multiplicative complexity, persisted/trusted documents, alias and batch caps) stop the attacks DataLoader cannot touch. DataLoader fixes how many DB trips a query makes; the defence layers govern what query shapes are even allowed to run.