GraphQL is an API style where the client describes the exact data it wants, and the server returns that shape and nothing more. There is one endpoint, usually POST /graphql, and every request carries a query. It was built for apps with many screens and many client types, where a fixed REST response is either too big or too small.
The problem it solves#
With REST, the server decides what each endpoint returns. That causes two problems when clients differ:
- Over-fetching. The mobile app needs a restaurant's name and rating, but
GET /restaurants/{id}returns the full menu, opening hours and photos. The phone downloads data it throws away. - Under-fetching. A restaurant page needs the restaurant, its next free slots and its latest reviews. That's three REST calls, one after another, each adding a round trip.
GraphQL fixes both: the client asks for exactly the fields it needs, across related objects, in one request.
How it works#
Three pieces make up a GraphQL API.
| Piece | What it is |
|---|---|
| Schema | Typed description of every object, field and relationship the API exposes. It is the contract |
| Query / mutation | A query reads data. A mutation changes it. Subscriptions push updates over a long-lived connection |
| Resolvers | Server functions, one per field, that fetch the value from a database or another service |
A schema for restaurant reservations looks like this:
type Restaurant {
id: ID!
name: String!
rating: Float
reviews(last: Int): [Review!]!
availability(date: String!, party: Int!): [Slot!]!
}
type Query {
restaurant(id: ID!): Restaurant
}
type Mutation {
bookTable(restaurantId: ID!, slot: String!, party: Int!): Booking!
}
The restaurant page then fetches everything in one request, and the response mirrors the query:
query {
restaurant(id: "42") {
name
rating
availability(date: "2025-05-02", party: 4) { time }
reviews(last: 3) { stars text }
}
}
The mobile list view sends the same query with only name and rating. Same endpoint, same schema, different shape.
GraphQL vs REST#
Neither is better in general. They put the work in different places.
| REST | GraphQL | |
|---|---|---|
| Endpoints | Many, one per resource | One |
| Response shape | Fixed by the server | Chosen by the client |
| HTTP caching | Easy: each URL is cacheable by CDNs and browsers | Hard: everything is a POST to one URL, so caching moves to the client or the resolvers |
| Errors | HTTP status codes | Usually 200 with an errors list in the body |
| Versioning | /v1, /v2 |
Add new fields and deprecate old ones; no versions |
| Best for | Simple, resource-shaped APIs, public APIs | Many client types and screens that combine related data |
Costs and pitfalls#
GraphQL moves flexibility to the client, and the server pays for it.
- The N+1 problem. A query for 20 restaurants with their reviews can call the reviews resolver 20 times, one database query each. The fix is a batching layer (the DataLoader pattern) that collects the 20 ids and runs one query.
- Expensive queries. A client can ask for deeply nested data, like reviews of users who reviewed restaurants who…, and overload the server. Limit query depth and cost, and rate limit by query cost rather than request count.
- Caching is harder. CDNs can't cache POST bodies by URL. Common fixes are persisted queries (the client sends a hash of a known query, which can be a GET) and caching inside the resolvers.
- Authorization per field. One query can touch many objects, so access checks belong in the resolvers or a layer below them, not only at the endpoint.
In an interview#
Default to REST. Reach for GraphQL when the prompt mentions several client types with different data needs, a screen that combines many related objects, or slow mobile networks where round trips hurt. A common pattern is a GraphQL layer at the edge (sometimes called a backend-for-frontend) that calls REST or RPC services behind it.
A good way to say it: "The app has a lean mobile view and a rich web dashboard over the same data, so I'd put GraphQL at the edge. Each client asks for only the fields it shows, and the page loads in one round trip."
Key takeaways#
- GraphQL has one endpoint, and the client chooses the response shape with a query.
- It fixes over-fetching and under-fetching when clients need different slices of related data.
- A typed schema is the contract. Resolvers fetch each field.
- Watch for N+1 queries (batch them), expensive nested queries (limit depth and cost) and weaker HTTP caching.
- Default to REST. Use GraphQL for many client types or data-heavy screens, often as a layer in front of other services.