Web and APIs
What is GraphQL?
Definition
GraphQL is a query language for APIs that lets a client ask a single endpoint for exactly the fields it needs and receive them in one response.
GraphQL in more detail
A GraphQL server publishes a schema: a typed description of every object it can return and how those objects connect. Clients send a query that mirrors the shape of the data they want, and the response comes back in that same shape. Where a REST API decides in advance what each endpoint returns, GraphQL moves that decision to the caller, which is why one mobile screen can fetch a user, their last five orders and each order's line items in a single round trip.
The trade is real. Because everything goes to one URL — usually a POST to /graphql — ordinary HTTP caching by URL no longer applies, and you need persisted queries or a client-side normalised cache instead. A naive resolver implementation issues one database query per field per parent row, the N+1 problem, which is why production servers use batching loaders. And because a client can ask for a very deep or very wide query, servers have to enforce query depth and cost limits that a REST endpoint gets for free.
GraphQL servers conventionally return HTTP 200 for any query they could parse, even when individual fields failed; the failures arrive in an errors array alongside whatever data did resolve. That surprises people who expect the status code to carry the outcome, and it means client error handling has to read the body rather than the status line.
GraphQL example
query {
user(id: "12") {
name
orders(last: 2) {
id
total
items { sku quantity }
}
}
}
# Response mirrors the query exactly:
{
"data": {
"user": {
"name": "Ada",
"orders": [
{ "id": "8842", "total": 49.0, "items": [ ... ] }
]
}
}
}GraphQL — common questions
- Is GraphQL a database?
- No. It is an API layer. A GraphQL server resolves each field by calling whatever it likes underneath — SQL databases, other HTTP services, caches, files — so the schema is a facade over existing systems rather than a store of its own.
- Does GraphQL replace REST?
- Not automatically. Many teams run both: GraphQL for rich client-driven screens and REST for simple, cacheable, machine-to-machine endpoints. Migrating a well-cached public REST API to GraphQL often costs more in caching and rate limiting than it saves in round trips.
- Why does GraphQL use POST for reads?
- Because queries can be longer than a URL comfortably allows and are sent in the request body. Some servers also accept GET with the query in the query string, which restores URL-based HTTP caching for queries small enough to fit.
Keep reading
Every definition in the set is indexed A-Z in the developer glossary, and the cheat sheets cover the syntax once you know what you are looking for.
Browse the A-Z glossary