Core concepts
- Schema — strongly typed contract (
Query,Mutation,Subscription, object types) - Single endpoint — typically
POST /graphqlwith JSON body{ "query": "...", "variables": {} } - Selection set — client requests exactly the fields needed
- Resolvers — functions backing each field; form a resolver tree executed depth-first
query {
user(id: "1") {
name
orders(first: 5) { total }
}
}Queries vs mutations vs subscriptions
| Operation | Semantics | HTTP analogy |
|---|---|---|
| Query | Read-only | GET (but POST transport) |
| Mutation | Side effects | POST |
| Subscription | Real-time stream | WebSocket (usually) |
GraphQL mutations are not guaranteed sequential — document ordering if business logic requires it (e.g. debit before credit in one mutation field).
The N+1 problem
Naive resolver per field:
User.orders → 1 query for user + N queries for each user's orders
DataLoader batches: collect load(orderId) calls in one tick → single WHERE id IN (...). Also per-request caching.
Staff signal: mention DataLoader unprompted in GraphQL interviews.
Error model
Partial results: data + errors[] array. HTTP may still be 200 with errors in body — clients must check errors. Some APIs use 4xx for auth at HTTP layer.
BFF pattern
Backend-for-Frontend — GraphQL gateway aggregates microservices for mobile/web. Keeps domain services REST/gRPC; BFF owns graph shape and auth context. LLD helps you design a clean OrderService facade in-process; this topic places that facade behind HTTP as a GraphQL layer.
Avoids exposing entire microservice graph to clients.
vs REST (when to pick GraphQL)
GraphQL wins: multiple clients with different field needs, reducing over-fetch, rapid product iteration on UI.
REST wins: simple CRUD, heavy CDN caching, file upload standards, public API simplicity, strong HTTP semantics.
Neither replaces internal high-throughput RPC — see gRPC topic.
Further Reading
Hands-On Tasks (Optional)
API design drills and whiteboard exercises — protocol selection, contract design, and bulk-transfer architecture. Assumes Networking and sibling tracks on the hub page (Distributed Systems, Databases, Concurrency, LLD).
- Design a schema for e-commerce20m
Define types for Product, Variant, Inventory, and queries: productById, searchProducts. Identify which fields need separate resolvers vs can be resolved from parent object.