What Is GraphQL? (And How It Differs From REST)
GraphQL is an alternative to REST that lets you ask for exactly the data you want. Here's the idea, simply.
GraphQL is a query language for APIs. Instead of many URLs each returning a fixed shape, a GraphQL API exposes one endpoint and a schema of available data. The client sends a query describing exactly the fields it wants, nested however it needs them, and gets back JSON in that exact shape — nothing more, nothing less.
A query and its response
POST /graphql
{
user(id: "42") {
name
posts(last: 2) {
title
comments { author }
}
}
}
# response
{ "data": { "user": { "name": "Ada",
"posts": [ { "title": "...", "comments": [ { "author": "Bo" } ] } ] } } }With a REST API, getting that same data might take three or four requests (the user, then their posts, then each post’s comments). GraphQL resolves it in one round trip.
The three operation types
| query | Read data. The equivalent of GET. |
| mutation | Change data (create/update/delete). Runs serially. The equivalent of POST/PUT/DELETE. |
| subscription | A long-lived connection (usually WebSocket) that pushes updates when data changes. |
Why teams choose it
- No over-fetching — a mobile screen requests only the five fields it shows.
- No under-fetching — deeply related data comes back in one query.
- Strong typing — the schema is introspectable; tools give you autocomplete and validate queries before they run.
- One version — you add fields without breaking clients and deprecate old ones gradually, instead of shipping
/v2/.
The trade-offs
- Caching is harder — every query is a POST to the same URL, so HTTP caching doesn’t work out of the box; you need client-side or persisted-query caching.
- Rate limiting is harder — ‘requests per minute’ is meaningless when one query can be huge. APIs use query complexity or points budgets instead.
- The N+1 problem — naive resolvers can fire a database query per item; servers use batching (DataLoader) to avoid it.
- A malicious deeply-nested query can be expensive — servers enforce depth and complexity limits.
Calling a GraphQL API
import requests
q = '{ viewer { login repositories(first: 3) { nodes { name } } } }'
r = requests.post("https://api.github.com/graphql",
json={"query": q},
headers={"Authorization": f"Bearer {TOKEN}"})
print(r.json()["data"])Note: a GraphQL response can be HTTP 200 and still contain errors — check the top-level errors array as well as data.
FAQ
Is GraphQL better than REST?
It is better for clients that need flexible, nested data (mobile apps, complex dashboards) and for evolving an API without versioning. REST is simpler, caches naturally, and is fine for straightforward resource CRUD.
Does GraphQL replace the database?
No. It sits in front of your data sources (databases, other APIs, microservices). Resolvers on the server fetch each field from wherever it lives.
Why did my GraphQL request return 200 with no data?
GraphQL reports field-level and validation problems in an errors array while still returning HTTP 200. Always inspect errors before using data.
How do I explore a GraphQL API?
Most expose an in-browser IDE (GraphiQL / Apollo Sandbox) with schema autocomplete, or you can run an introspection query to download the full schema.
Keep going
Try it on a real API
๐ Explore free public APIs
Hundreds of free APIs, searchable by category, with auth & CORS at a glance.
Browse the directory โ