API Directory โ€บ Guides โ€บ What Is GraphQL? (And How It Differs From REST)

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

queryRead data. The equivalent of GET.
mutationChange data (create/update/delete). Runs serially. The equivalent of POST/PUT/DELETE.
subscriptionA long-lived connection (usually WebSocket) that pushes updates when data changes.

Why teams choose it

The trade-offs

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

Browse all 196 free APIs โ†’

๐Ÿ”Œ Explore free public APIs

Hundreds of free APIs, searchable by category, with auth & CORS at a glance.

Browse the directory โ†’