Skip to the workspace
Pebellslearn by building
API Styles  ›  Lesson

GraphQL APIs

GraphQL APIs 14 minGraphQL exposes one endpoint with a typed schema and lets each client ask for exactly the fields it needs, trading away easy HTTP caching and plain status-code errors.

api design / GraphQL APIs

Explore each scene at your own pace.

01 · The rule

The rule

GraphQL APIs

The BookNook home screen needs a member's name, their three current loans, and each loaned book's title and cover. With REST that is one call for the member, one for the loans, then one per book, and every response carries fields the screen never shows. GraphQL flips this: the client describes exactly the shape it wants, and gets that shape back in one round trip.

The rule

One endpoint, one typed schema; the client picks the fields.

A GraphQL API publishes a schema: the types it knows and the fields on each, all strongly typed.

type Book   { id: ID!  title: String!  coverUrl: String }
type Loan   { id: ID!  due: String!  book: Book! }
type Member { id: ID!  name: String!  loans(active: Boolean): [Loan!]! }
type Query  { member(id: ID!): Member }

The client sends a query that mirrors the shape it needs, usually as POST /graphql:

query HomeScreen {
  member(id: 7) {
    name
    loans(active: true) { due  book { title coverUrl } }
  }
}

The response has the same shape, and nothing more:

{ "data": { "member": { "name": "Asha",
  "loans": [ { "due": "2026-10-09", "book": { "title": "The Hobbit", "coverUrl": "/c/42.jpg" } } ] } } }
  • Queries read data. Mutations change it, like borrowBook(bookId: 42). Subscriptions push updates over a long-lived connection.
  • This fixes over-fetching (getting fields you do not need) and under-fetching (needing several calls to build one screen).

It has costs too:

  • Caching is harder: everything is a POST to one URL, so ordinary HTTP caches cannot tell queries apart.
  • Errors look different: a response often comes back as 200 OK with an errors array beside data, so clients must check the body, not just the status.
  • Expensive queries: a client can ask for deeply nested data in one request, so servers need limits on depth and cost.
Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.