At the bookshop counter there is a small form. You write "title, price, author name" for book number 1. The clerk fills in only those three boxes, in the same order, and gives the form back. Nothing else comes with it, and you don't queue at a second counter.
GraphQL works like that counter, but between two programs. You at the counter are the client: the program that asks for data, such as a web page. The clerk is the server: the computer that keeps the data. The counter itself, where the server serves other programs, is called an API. GraphQL is a language for asking an API for data (a query language), and also the engine on the server that runs those questions. The form is the client's question, and each box is one field (one named piece of data, such as the title). The client writes which fields it needs. The server answers with JSON (data written as text, much like a JavaScript object) shaped exactly like the question.
The problem: a book detail page
The example throughout the GraphQL track is the Bookshop, the same shop as in the API track: three books and four authors.
| id | title | Author | price | stock |
|---|---|---|---|---|
| 1 | The Rainbow Troops | Andrea Hirata | 89000 | 12 |
| 2 | This Earth of Mankind | Pramoedya Ananta Toer | 125000 | 0 |
| 3 | Beauty Is a Wound | Eka Kurniawan | 110000 | 5 |
The fourth author, Ahmad Tohari, has no book in the shop yet.
A book detail page needs only three things: the title, the price and the author's name. In REST, the most common way to design an API, every kind of data has its own URL. So the client sends two requests to two URLs. Below, a GET line is a request, and a line with an arrow is a response (the server's answer) with its size:
GET https://api.bookshop.test/books/1
→ {"id":1,"title":"The Rainbow Troops","author_id":1,"price":89000,"stock":12} (76 bytes)
GET https://api.bookshop.test/authors/1
→ {"id":1,"name":"Andrea Hirata"} (31 bytes)
- Over-fetching (too much data):
/books/1also sendsid,author_idandstock, which this page doesn't use. The server decides the shape of a REST response, and it is the same for every page. - Under-fetching (too little data):
/books/1gives onlyauthor_id(the author's number), not the author's name. The client learns the author's number only from the first response, so it has to wait for it before it can ask for/authors/1. That makes two round trips to the server.
In total: 2 requests one after the other, and the client receives 107 bytes. Only the body (the content of a message, here its JSON) is counted, not the HTTP headers (extra information sent with every message; HTTP is the set of rules for these messages).
Your first query
With GraphQL, the client writes what it needs in one query (one question for the server):
query {
book(id: 1) {
title
price
author {
name
}
}
}
The server answers:
{
"data": {
"book": {
"title": "The Rainbow Troops",
"price": 89000,
"author": {
"name": "Andrea Hirata"
}
}
}
}
- The word
querymeans this operation reads data. The word is optional:{ book(id: 1) { title } }still means reading data. book(id: 1)is a field with an argumentid(a detail in round brackets): "the book whose id is 1".- The braces after
book(id: 1)hold the fields asked for on that book.author { name }goes into the author's data and asks for the name, so the author's name comes in the same response. - The response, inside
data, follows the query's shape:bookholdstitle,priceandauthor, in the same order. There is noid,stockorauthor_id, because the query didn't ask for them.
One request, and the client receives 96 bytes. No extra fields, and no second request.
One endpoint, sent with POST
A GraphQL API usually has only one endpoint (an address on the server that a client can call), for example https://api.bookshop.test/graphql. Every query goes to that address. Requests differ in the query they carry, not in the URL. From JavaScript, all you need is fetch, the browser's built-in function for sending a request. await waits until the answer arrives:
const response = await fetch("https://api.bookshop.test/graphql", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ query: "{ book(id: 1) { title price author { name } } }" }),
});
const result = await response.json();
console.log(result.data.book.author.name); // "Andrea Hirata"
- The method is
POST, the kind of request that carries data. The body is JSON with aquerykey holding the query's text. For this query the body is 59 bytes. - You don't need a special library (ready-made code written by other people). Libraries like Apollo Client or urql only make the same thing easier.
- The result is always a JSON object with a
datakey, anerrorskey, or both.
When something is wrong: errors
A GraphQL response has two places: data for the result, and errors for the problems. Try asking for a field that doesn't exist, such as isbn:
query {
book(id: 1) {
title
isbn
}
}
Before you look at the answer, guess: is isbn set to null (an empty value) with title still sent, or is the whole query rejected?
{
"errors": [
{
"message": "Cannot query field \"isbn\" on type \"Book\".",
"locations": [{ "line": 4, "column": 5 }]
}
]
}
The answer: the whole query is rejected, with no data at all. The server checks the whole query before running it. This check is called validation. One unknown field is enough to reject the whole query, so not even title is sent. locations points at line 4, column 5, where isbn is written.
It's different when the query is valid but fails while running, for example book(id: 99) when there is no book 99. The query is the same as the one above, with id 99 and only the title field. The answer:
{
"errors": [
{
"message": "Book not found",
"locations": [{ "line": 2, "column": 3 }],
"path": ["book"]
}
],
"data": {
"book": null
}
}
This time data is there, holding book: null, and errors gives the reason. path points at the field that failed. The message "Book not found" is written by the Bookshop server itself. The HTTP status (a number code on every response) for these two cases is covered in the next lesson.
What decides which fields exist: the schema
A GraphQL server has a schema: the list of types (kinds of data, such as a book) and fields that may be asked for, with the type of each value. A piece of the Bookshop schema:
type Query {
book(id: ID!): Book
}
type Book {
title: String!
price: Int!
author: Author!
}
The ! mark means the value can't be null. In this schema a book has no isbn field, so the query that asked for it was rejected. Every field has a resolver: a function on the server that finds the field's value, for example by reading the database (where the data is stored). The full schema comes in the "Schema & Type System" lesson.
⚠️ Early misunderstandings
- Thinking GraphQL is a database. GraphQL doesn't store data. It is a layer in front of a database, other services, or an existing REST API.
- Asking for an object without picking its fields. An author is an object: data with fields of its own.
authoron its own is rejected withField "author" of type "Author!" must have a selection of subfields. Did you mean "author { ... }"?. An object always needs braces with fields inside. - Only checking the HTTP status. A response holding
errorscan arrive with status200(success). Always checkerrorsin the body. - Assuming GraphQL always saves bytes. The query text is sent too (59 bytes in the
fetchexample). What surely drops is the number of round trips. The next lesson measures it.
📌 The specification
GraphQL was created at Facebook in 2012 and became an open standard in 2015. The GraphQL Foundation now manages it. Its rules are written in the specification at spec.graphql.org. The latest release (as of November 2026) is the September 2026 edition, the ones before it the September 2025 and October 2021 editions, and there is a working draft for changes in progress. That specification deliberately says nothing about HTTP. How to send GraphQL over HTTP is written in a separate specification, GraphQL over HTTP, which is still a draft.
GraphQL servers exist in many programming languages: graphql-js (the reference implementation, in JavaScript), Apollo Server, GraphQL Yoga, Lighthouse for Laravel, and more. They all follow the same specification, so queries and responses look the same. The Lab on this page uses a small GraphQL engine whose error messages match graphql-js 17.0.2.
TL;DR: GraphQL is a query language for APIs: a language for asking a server for data, where one question is called a query. The client (the program that asks for data) writes the fields it needs (the names of the data), and the server answers with JSON (data written as text) of the same shape, no more and no less. This fixes over-fetching (too much data) and under-fetching (too little data, so more requests). Take the book detail page: with REST, which gives every kind of data its own URL, it needs 2 requests (107 bytes), while with GraphQL 1 query is enough (96 bytes). Every query is sent with POST (the kind of request that carries data) to one endpoint (one address on the server), with the body {"query": "..."}. The response (the server's answer) has data, errors, or both. An invalid query (such as one that asks for an unknown field) is rejected whole, with no data. A field that fails while running becomes null, with the reason in errors. The schema (the server's list of fields) decides which fields exist, and resolvers (functions on the server) find their values.
⚡ The client picks the fields
Write:
query {
book(id: 1) {
title
price
author {
name
}
}
}
Meaning: GraphQL is a query language for APIs: the language a client (the program that asks for data) uses to ask a server for data. The code above is one query (one question for the server). From book number 1, the client asks for three fields (named pieces of data): title, price and author, with name from author. The server answers with JSON (data written as text) of the same shape: book holds title, price and author, no more and no less.
⚡ Two REST requests become one query
Guess first: the book detail page needs the title, the price and the author's name. In REST, every kind of data has its own URL: /books/1 gives the book's data and its author's number (author_id), /authors/1 gives the author's name. How many requests to the server does it take?
Two, one after the other: /books/1 first, then /authors/1 once author_id is known. 107 bytes in total. With GraphQL one query is enough, 96 bytes.
Meaning: /books/1 sends fields the page doesn't use (over-fetching), yet not the author's name (under-fetching). One GraphQL query fixes both problems, because the client picks the fields.
⚡ One wrong field rejects the whole query
Write:
query {
book(id: 1) {
title
isbn
}
}
Guess first: the schema is the server's list of fields you may ask for, and isbn is not on that list. Is title still sent, or is the whole query rejected?
The whole query is rejected. The response (the server's answer) holds only errors (the list of problems), with no data (the result) at all, and locations points at line 4, column 5, where isbn is written.
Meaning: The server checks the whole query before running it (validation). A valid query that fails while running is different, such as book(id: 99) when there is no book 99: data is still there with book: null, and errors points at the field that failed through path.