Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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.

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.

The shortcut

Choose by asking who knows the shape of the data needed? If many different clients each need a different slice of connected data, GraphQL lets them say so. If most calls are simple reads of one resource, REST with HTTP caching is simpler.

Should BookNook add GraphQL?Step 1 of 4
  1. List the clients

    A phone app, a website and a librarian dashboard, each showing different combinations of members, loans and books.

Drills

Answer fast — trust the rule.

quick drill

In GraphQL, who decides which fields appear in the response?

quick drill

Which GraphQL operation should BookNook use to create a loan?

quick drill

A GraphQL response arrives as 200 OK. Can the client assume everything worked?

quick drill

Which problem is GraphQL mainly designed to solve?

Remember

  • One endpoint, one typed schema; the client picks the fields.
  • Shortcut: many clients needing different slices of connected data → GraphQL; simple cacheable reads → REST.
  • Trap: trusting a 200 status, and forgetting limits on costly nested queries.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainGraphQL trade-offs

The BookNook home screen needs a member's name plus the title of each book they have on loan. Compare how REST and GraphQL would fetch this, and name two costs of choosing GraphQL.