$ curl -X POST /graphql -d '{"query":"{ user(id: 42) { name } }"}'

GraphQL

GraphQL is a query language for APIs. Instead of many fixed endpoints, the server publishes one typed schema, and each client sends a query that describes exactly the fields it wants, nested as deep as it needs. The response has the same shape as the query. Facebook built it in 2012 for its mobile apps and open-sourced it in 2015; it is now governed by the GraphQL Foundation. GitHub, Shopify and many internal “backend for frontend” layers use it.

REST vs GraphQL for one screen

$ show user 42 with last 3 order totals
Same screen, REST vs GraphQL
CLIENT REST REST API CLIENT GraphQL GRAPHQL API GET /users/42 user: 30 fields GET /users/42/orders 50 orders, all fields over-fetched + 2 trips POST /graphql { query } resolve user, orders exactly: name, 3 totals N+1 risk → DataLoader
  1. A mobile screen needs one user's name and the totals of their last 3 orders.
  2. REST: the user resource returns every field of the user, whether the screen needs it or not.
  3. A second round trip fetches the orders. The client receives far more than it shows: over-fetching and under-fetching at once.
  4. GraphQL: one request describes the exact shape. The server's resolvers fill it in and the response mirrors the query.
  5. The cost moves to the server: naive resolvers run one DB query per parent item, so a list of 100 users means 1 + 100 order queries (N+1). Batching with a DataLoader fixes it.

How it works

The schema declares types and the entry points (Query, Mutation, Subscription):

type User   { id: ID!  name: String!  orders(last: Int): [Order!]! }
type Order  { id: ID!  total: Float!  status: String! }
type Query  { user(id: ID!): User }
type Mutation { cancelOrder(id: ID!): Order }

A client query:

query {
  user(id: 42) {
    name
    orders(last: 3) { total }
  }
}

On the server, each field has a resolver: a function that fetches that piece. The engine walks the query, calls resolvers, and assembles the JSON. Mutations change data, and subscriptions push updates over a WebSocket or server-sent events.

Trade-offs

Strength Cost
Clients get exactly what they need in one round trip The server must protect itself from expensive queries (depth and complexity limits, persisted queries)
Strong typing and introspection: great tooling and autocompletion HTTP caching mostly doesn’t apply; caching moves to the client (Apollo, Relay) or per resolver
Evolve the API by adding fields, deprecating old ones Naive resolvers cause N+1 database queries; you need batching (DataLoader)
One schema can stitch many back-end services (federation) More moving parts than a simple REST API

When to use it

  • Good fit: UIs with many screens that each need different slices of related data; aggregating many microservices behind one API.
  • Poor fit: bulk data export, simple CRUD services, file uploads, or public APIs where HTTP caching matters. For moving large volumes into a warehouse, a REST bulk endpoint or a database replica is usually simpler.

For data engineers, the practical skill is consuming GraphQL APIs (GitHub, Shopify, Linear): paginate with cursors (first, after, pageInfo.endCursor) and respect the API’s query-cost rate limits.

Common mistakes

  • No query cost limits, so one deeply nested query takes the server down.
  • Exposing the database schema 1:1 instead of designing a schema around client needs.
  • Treating every error as an HTTP status. GraphQL usually returns 200 with an errors array; check it.

Learning Resources

See also: REST, gRPC.