Part I β The Pipes Β· Lesson 1.6
GraphQL
In one lineOne endpoint, one round trip, and the client declares exactly which fields it needs.
Where you've seen itThe Facebook news feed β one query returns posts, their authors, and comment counts in a single response shaped like the UI.
Diagram
REST β 3 round trips, and each returns more than the screen shows
Client Server
βββ GET /user/42 βββββββββββββΊβ returns 24 fields, UI uses 2
βββββββββββββββββββββββββββ βββ
βββ GET /user/42/posts βββββββΊβ RTT 2
βββββββββββββββββββββββββββββ β
βββ GET /posts/*/comments ββββΊβ RTT 3
βββββββββββββββββββββββββββββ β
total: 3 Γ RTT + wasted bytes
GraphQL β 1 round trip, exactly the fields requested
Client Server
βββ POST /graphql ββββββββββββΊβ β resolver: user
β { user(id:42) { β β resolver: posts
β name β β resolver: comments
β posts(first:10) { β
β title β
β comments { count } β
β } } } β
ββββ exact shape requested ββββ
total: 1 Γ RTT, no over-fetchTHE COST THAT MOVED, NOT DISAPPEARED
REST : many endpoints Β· free HTTP cache Β· over-fetch
GraphQL : one endpoint Β· no HTTP cache Β· N+1 resolver risk
(you build a normalized client cache instead)How it works
- The server publishes a schema β types, fields, and their relationships.
- The client sends a query describing the exact tree of fields it wants.
- Resolvers fetch each field; nested fields resolve recursively.
- The response mirrors the query shape one-to-one β no guessing, no unused fields.
- Mutations write; subscriptions stream updates over WebSocket.
query Feed {
user(id: 42) {
name
posts(first: 10) { title comments { count } }
}
}Trade-offs
| Use when | Avoid when |
|---|---|
| Many clients need different field sets | One client, stable payloads |
| Deeply nested, relational UI data | You depend on CDN/HTTP caching |
| Mobile bandwidth actually matters | Small team, no resolver-perf ownership |
Two traps to name in an interview: the N+1 problem (fix with DataLoader batching) and unbounded query depth (fix with depth limits, complexity scoring, and persisted queries).
Interview angle
"GraphQL removed over-fetching. What did it cost you?"
- HTTP caching:
POST /graphqlis opaque to CDNs, so caching moves into the client (normalized store) or needs persisted queries +GET. - Server performance becomes the client's problem β one careless nested query can hammer the database.
- You now own schema versioning, deprecation, and query governance.
Recap
- Client declares the shape; server resolves it in one round trip.
- Over-fetching and round trips go away; caching and query governance arrive.
- Say "N+1" and "persisted queries" β that's the senior signal.