The GraphQL N+1 Query
#backend-graphql — DB CPU 100%, feed frozen
Single GraphQL request: 50 posts + authors = 51 SQL queries. One for posts, fifty SELECT * FROM users WHERE id = ?. Mobile feed 504s. DB connection pool exhausted.
The trap: naive authorResolver calls db.getUser() per post — GraphQL resolves field-by-field.
Your task
Wire DataLoader in `graphql_server.js`: batch load() calls into one db.getUsersByIds() query, dedupe IDs, preserve result order.
Book: GraphQL magic · Prod: DataLoader + WHERE id IN (...) or the database melts.
Skills you'll prove
Interview Tip
When asked how you optimize GraphQL performance or what the N+1 problem is, say: "Because GraphQL resolves field-by-field, a naive resolver will execute a separate database query for every nested object—for example, 50 queries for the authors of 50 posts. I solve this using the DataLoader pattern. DataLoader intercepts individual load calls, batches them into a single tick of the event loop, and executes one SQL query using an IN clause. It also provides built-in memoization, so if two posts share the same author, we only fetch that author once."