REST and GraphQL are two ways to design a web API. REST gives each resource its own URL and uses HTTP methods and status codes as the contract, while GraphQL puts one endpoint in front of a typed schema and lets the client write a query naming exactly the fields it wants back.
Neither is newer-and-better. REST works with HTTP caching, CDNs and every HTTP tool out of the box. GraphQL saves round trips and payload when different screens need different slices of the same data. The rest of this article shows what each looks like on the wire and where the trade-offs come from.
The same request in REST and GraphQL
Suppose a page needs a country's name, capital and currency. In a REST API you request the country resource and get back whatever fields the server's representation includes:
GET /countries/DE HTTP/1.1
Host: api.example.com
Accept: application/json
Example response (shortened; real ones often carry dozens of fields):
HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: max-age=3600
{
"code": "DE",
"name": "Germany",
"capital": "Berlin",
"currency": "EUR",
"population": 83000000,
"languages": ["de"],
"borders": ["AT", "BE", "CH", "CZ", "DK", "FR", "LU", "NL", "PL"],
"timezones": ["UTC+01:00"]
}
In GraphQL you send a query to a single endpoint, and the response has exactly the shape of the query. This one runs against a public demo API (the response is real output, as of October 2026):
curl -s -X POST https://countries.trevorblades.com/graphql \
-H "Content-Type: application/json" \
-d '{"query":"{ country(code: \"DE\") { name capital currency } }"}'
{"data":{"country":{"capital":"Berlin","currency":"EUR","name":"Germany"}}}
That response is 75 bytes. The GraphQL specification describes this property directly: "The request is shaped just like the data in its response."
REST vs GraphQL at a glance
| REST | GraphQL | |
|---|---|---|
| Endpoints | One URL per resource (/users/42, /users/42/orders) | Usually one URL, typically /graphql |
| Who picks the fields | The server (some APIs add a ?fields= parameter) | The client, field by field |
| Reads and writes | HTTP methods: GET, POST, PUT, PATCH, DELETE | query and mutation operations, sent over POST (queries may also use GET) |
| Errors | HTTP status codes (404, 403, 500) | An errors array in the body, often with a 2xx status |
| HTTP caching | Works by default for GET | Needs GET queries or persisted queries |
| Schema | Optional (OpenAPI is common) | Required; the type system is part of the spec |
| Discoverability | Documentation, OpenAPI files | Introspection: the schema can be queried with GraphQL itself |
Over-fetching and under-fetching
These two problems are the main reason GraphQL exists.
- Over-fetching: the REST response above sends population, borders and time zones to a page that only shows the capital. On a phone over a slow link, those bytes add up.
- Under-fetching: a page that shows a user, their last five orders and each order's items might need
GET /users/42, thenGET /users/42/orders?limit=5, then one request per order. Each request waits for the one before it.
In GraphQL, one query can walk those relationships, and the server returns the whole tree in one response. GitHub gives this as its reason for offering a GraphQL API: you can define "precisely the data you want" and "replace multiple REST requests with a single call".
REST APIs can narrow the gap. Field selection parameters, embedded resources (?include=orders) and purpose-built endpoints for specific screens all reduce round trips. The cost is more endpoint design work on the server for every new client need.
Caching: where REST has the advantage
HTTP was designed around cacheable GET requests. RFC 9110 says "The response to a GET request is cacheable", while "Responses to POST requests are only cacheable when they include explicit freshness information" plus a matching Content-Location header, which almost no API sends. So a REST GET /countries/DE with Cache-Control: max-age=3600 can be served by the browser cache, a corporate proxy or a CDN without touching your server.
Most GraphQL traffic is POST to one URL, so those layers can't cache it. graphql.org's HTTP guide says servers may accept GET for queries to "facilitate HTTP caching or edge caching in a content delivery network", but long queries can exceed URL length limits. The usual fix is persisted queries (also called trusted documents): the client sends a hash of a query the server already knows, instead of the full text. GraphQL clients also tend to cache on the client side, normalised by object ID, which is effective but is extra machinery you have to run.
Errors and status codes
A REST API maps outcomes to status codes: 404 for a missing resource, 401 or 403 for auth problems, 422 or 400 for bad input. Monitoring tools, retries and logs all understand those codes. See HTTP status codes explained for the ones you'll meet.
GraphQL returns a body with data and, when something failed, errors. Because one query can partly succeed, graphql.org's HTTP guidance says a response with non-null data should get a 2xx status "even if the response includes errors, since HTTP does not yet have a status code representing 'partial success'". Validation errors that stop the query running typically return 400. Example of a partial failure:
{
"data": { "user": { "name": "Alex", "orders": null } },
"errors": [
{ "message": "Orders service timed out", "path": ["user", "orders"] }
]
}
The practical consequence: a GraphQL client must always check errors, and an uptime monitor that only watches status codes will miss failures.
Server-side costs of GraphQL
Flexibility for the client moves work to the server.
- The N+1 problem. graphql.org's performance guide describes it: a request for a list leads to "N subsequent requests to resolve the data" for each item's nested fields. The standard fix is batching, for example with Facebook's DataLoader, which collects lookups made during one request and sends them to the database as one query.
- Expensive queries. A client can nest relationships deeply (friends of friends of friends). The same guide recommends "paginating list fields, limiting operation depth and breadth, and query complexity analysis". GitHub, for instance, limits its GraphQL API by points computed from each query's size (5,000 points per hour for an authenticated user, as of October 2026) rather than by counting requests.
- Authorisation per field. In REST you can protect a whole endpoint. In GraphQL, any field may be reachable through several paths in the graph, so access checks belong in the resolvers or the business logic below them.
When to use which
REST is the better default when
- The API is public and used by many unknown clients, who expect plain HTTP and
curl-friendly URLs. - Responses are cacheable and you want browsers and CDNs to do that work.
- Resources map cleanly to URLs and clients mostly want whole resources.
- You are building file uploads, downloads or streaming, which fit HTTP directly.
GraphQL earns its cost when
- Several clients (web, iOS, Android) need different slices of the same data, and you don't want an endpoint per screen.
- Pages need deeply related data, and the round trips of REST are the measured bottleneck.
- You want a strongly typed contract with generated client code, and teams that change front-end needs without waiting on back-end changes.
If you are new to the terms, what is an API covers the basics first. Both styles usually use the same authentication: an Authorization: Bearer header, often with a token from OAuth.
Frequently asked questions
- Is GraphQL faster than REST?
- Not inherently. GraphQL can cut the number of round trips, because one query can fetch data that would take several REST calls, and it avoids sending fields the client didn't ask for. But a naive GraphQL server can make many more database queries per request (the N+1 problem), and POST requests miss out on ordinary HTTP caching. Which is faster depends on the data shape and how each server is built.
- Can I use REST and GraphQL together?
- Yes, and many companies do. GitHub, for example, offers both a REST API and a GraphQL API over the same data. A common pattern is a GraphQL layer that calls existing REST services behind it, so front-end clients get one flexible endpoint while back-end services keep their REST interfaces.
- Does GraphQL replace a database query language like SQL?
- No. GraphQL queries an API, not a database. The server decides how each field is resolved, which might be a SQL query, a call to another service or a value computed in memory. The GraphQL specification says it does not mandate a particular programming language or storage system.
- Why does my GraphQL API return 200 OK for an error?
- Because the response can be a partial success: some fields resolved and others failed. graphql.org's HTTP guidance says that when the response contains non-null data the server should use a 2xx status even if it also includes errors, since HTTP has no status code for partial success. Always check the errors array in the body, not just the status code.