Skip to content

Beginning of the lesson

GraphQL APIs

One typed endpoint where the client asks for exactly the data it needs

Big idea: GraphQL is a query language for your API plus a runtime that answers those queries. Instead of many fixed REST routes that each return a fixed shape, you expose a single /graphql endpoint backed by a strongly typed schema. The client sends a query describing the exact fields it wants, and the server returns precisely that shape — no more, no less. You have already built REST APIs with Express; GraphQL is a different contract layered over the same data sources (SQL, MongoDB, other services) you already know.
By the end you will be able to:
  • write a GraphQL schema in SDL with object types, Query and Mutation;
  • implement resolvers and read their (parent, args, context, info) signature;
  • tell queries (reads) from mutations (writes) and shape each operation;
  • recognise the N+1 problem and explain how a DataLoader batches it away;
  • weigh the honest trade-offs of GraphQL vs REST and pick deliberately.

1. Why GraphQL exists: over- and under-fetching

In a REST API each endpoint returns a fixed payload. Consider a mobile screen that needs a user's name and the titles of their last three posts. With REST you often face two bad options:

  • Over-fetching. GET /users/42 returns the whole user object — email, address, settings, timestamps — when you only wanted the name. Bytes wasted on every request.
  • Under-fetching (the N+1 round-trip). That user endpoint does not include posts, so the client calls GET /users/42/posts as a second request. Two network round-trips to paint one screen; more screens mean more chatter.

GraphQL removes both problems for the client. The client sends one request describing exactly the tree of data it wants:

query {
  user(id: 42) {
    name
    posts(last: 3) { title }
  }
}

The server answers with a JSON object mirroring that exact shape: a user with a name and an array of posts each with a title — nothing else.

Analogy. REST is a fixed-menu restaurant: each dish (endpoint) arrives plated a set way, and if you want a side you order a second dish. GraphQL is a build-your-own bowl: you tick exactly the ingredients you want on one order slip, and the kitchen assembles that one bowl. The kitchen (your data sources) is unchanged; only the ordering contract differs.

Crucially, GraphQL does not replace your database or business logic. Under every field is ordinary code that reads from Postgres, MongoDB, a cache, or another HTTP service — the very skills from earlier courses.

Continue this lesson on YourAIZone

The full lesson has the complete explanation, worked examples, common mistakes, tests with instant checking and an AI tutor. Start right away as a guest, or create an account to keep your progress.

In the full lesson

  • The complete explanation
  • Worked examples
  • Common mistakes and how to avoid them
  • Test questions: 15
  • AI tutor that answers questions about the topic
  • Progress tracking for students, parents and teachers