GraphQL APIs
One typed endpoint where the client asks for exactly the data it needs
/graphql endpoint backed by a strongly typed schema. The client sends a query describing the exact fields it wants, and the server returns precisely that shape — no more, no less. You have already built REST APIs with Express; GraphQL is a different contract layered over the same data sources (SQL, MongoDB, other services) you already know.
- write a GraphQL schema in SDL with object types,
QueryandMutation; - implement resolvers and read their
(parent, args, context, info)signature; - tell queries (reads) from mutations (writes) and shape each operation;
- recognise the N+1 problem and explain how a DataLoader batches it away;
- weigh the honest trade-offs of GraphQL vs REST and pick deliberately.
1. Why GraphQL exists: over- and under-fetching
In a REST API each endpoint returns a fixed payload. Consider a mobile screen that needs a user's name and the titles of their last three posts. With REST you often face two bad options:
- Over-fetching.
GET /users/42returns the whole user object — email, address, settings, timestamps — when you only wanted the name. Bytes wasted on every request. - Under-fetching (the N+1 round-trip). That user endpoint does not include posts, so the client calls
GET /users/42/postsas a second request. Two network round-trips to paint one screen; more screens mean more chatter.
GraphQL removes both problems for the client. The client sends one request describing exactly the tree of data it wants:
query {
user(id: 42) {
name
posts(last: 3) { title }
}
}
The server answers with a JSON object mirroring that exact shape: a user with a name and an array of posts each with a title — nothing else.
Analogy. REST is a fixed-menu restaurant: each dish (endpoint) arrives plated a set way, and if you want a side you order a second dish. GraphQL is a build-your-own bowl: you tick exactly the ingredients you want on one order slip, and the kitchen assembles that one bowl. The kitchen (your data sources) is unchanged; only the ordering contract differs.
Crucially, GraphQL does not replace your database or business logic. Under every field is ordinary code that reads from Postgres, MongoDB, a cache, or another HTTP service — the very skills from earlier courses.