« Previous
Next »
Summary
Wrapping up the GraphQL track with what you can ship and where to go next.
What you learned + first DataLoader
EXAMPLE
# GraphQL summary
You can now:
- Design schemas with nullability discipline and connections
- Author resolvers with per-request DataLoader to avoid N+1
- Build mutations with payload + UserError types
- Implement subscriptions over WebSocket or SSE
- Wire field-level authorisation in resolvers
- Cache on the client (Apollo / Relay) and server
- Operate persisted queries + depth limits in production
- Compose subgraphs with Apollo Federation v2
# Your next step - a per-request DataLoader
import DataLoader from 'dataloader';
function makeUserLoader(db) {
return new DataLoader(async (ids) => {
const rows = await db.users.findMany({ where: { id: { in: [...ids] } } });
const byId = new Map(rows.map((r) => [r.id, r]));
return ids.map((id) => byId.get(id) || null);
});
}
// Create one PER REQUEST (never share across requests)
function createContext(req) {
return { loaders: { user: makeUserLoader(db) } };
}
const resolvers = {
Post: {
author: (post, _, ctx) => ctx.loaders.user.load(post.authorId)
}
};
Why it matters
GraphQL is best when typed clients + nested data justify the schema overhead. Persisted queries + depth limits are non-negotiable in production. For small or public APIs, REST or RPC is often the right answer.
Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.
Example
« Previous
Next »
Discussion
Loading…