GraphQL, from zero to governance — part 3
You write the shape of the answer you want and the answer comes back in that shape.
This is for anyone who’s seen a query in a screenshot and wanted to know what they were looking at. This assumes you can read a schema, which is covered in part 2, the schema is the contract. It doesn’t cover mutations or subscriptions in depth, or how to run any of this in production. If you build APIs for a living, start at part 4, “Something has to actually go get it”.
The screen, restated as a request
Picture the product page from part 1 again. The photo, name, description, price, a discount badge, “3 left in stock,” a star rating with its 412 reviews, a delivery estimate, and a line promising it’ll ship to your saved address. Nine things on one screen.
The browser painting that page doesn’t want a catalog dump and it doesn’t want to parse through information that it doesn’t need. It only wants those nine values and nothing else, each one dropped into its slot. Behind the scenes those nine things probably belong to different parts of the company, but the browser doesn’t know that and doesn’t need to. It only needs the values.
This makes the task quite narrow: write down the things this screen needs, in a form a server can read back. That written-down request is a query.
The query, and the answer beside it
Here’s the query for part of that screen — the name, the price, the rating, and the review count:
{
product(id: "field-runner-3") {
name
price
rating
reviewCount
}
}
Ignore the (id: "field-runner-3") for now; we’ll come back to what it’s doing. Look at the rest. You’re naming fields, one per line, inside the thing they belong to. That’s all the request is. You provided the fields you want.
Now the answer:
{
"data": {
"product": {
"name": "Field Runner 3",
"price": 129,
"rating": 4.6,
"reviewCount": 412
}
}
}
You asked for name, price, rating, reviewCount. You got back those same four fields, each exactly once, wrapped in a data key. Nothing extra rode along, and nothing you skipped showed up anyway.
This is probably the single most useful thing to know about GraphQL. First: when nothing goes wrong, the response is the query with the blanks filled in. Same shape, same nesting, same field names, with the values dropped into the outline you drew. Second, the same fact from the other direction: to know what fields a response holds, you don’t guess. You read the query and it provides the shape of its own answer. (The types, and what can come back empty, still live in the schema.)
This is the part that pays off at work. Someone drops a query in a thread and asks why the discount badge came up blank on the page. You don’t need the server, the logs, or a running app to start. You look at the query and check whether it asked for the discount field at all. If it didn’t, the badge was never coming, whatever the server holds. (If it did ask, a blank badge might just mean there’s no discount, since that field is allowed to come back empty.) The request tells you what the response can and can’t contain, and you can say so before anyone runs a thing.
That exactness is what the title of this post is pointing at. The client asks for exactly what it wants, and the answer carries those fields and only those fields.
The word for the list of fields you asked for is a selection set. The thing you send is a query. (More precisely, a query is one kind of operation. There are others.)
Where the query actually goes
Where do you send it? The query travels as an ordinary web request, the same kind your browser makes for everything else. And in practice it goes to a single address for the whole API: one URL that every query for every screen is sent to.
Take note of that, because it will come back in part 5: when there’s one address and the query carries the intent, the address alone doesn’t say what’s being asked, so there’s usually no simple way at the front door to count, cache, or rate-limit per thing. For now, just consider it as plumbing. And it’s common practice, not a law. The core GraphQL spec says nothing about how a query gets from here to there. It’s just how it’s usually run.
[ screen ] ── query ──▶ schema ──── ??? ──── [ data ]
^ ^
you are here part 4
The series diagram, state 3. The query is the arrow from the screen to the schema: how you ask. The gap between the schema and the data is still open, and that’s part 4.
Nesting
Look again and notice you can go deeper. product isn’t a flat bag of values. One of its fields, reviews, is a list of whole objects with fields of their own. Ask for reviews inside product, name the fields you want from each one (rating and author), and that’s what each review brings back. The response nests to match: a reviews list under the product, each entry shaped like the sub-request you wrote. The shape follows the query all the way down.
Here’s the part that matters for everything after this post. You can nest that way because the schema in part 2 said reviews live under a product. The query isn’t inventing anything, it’s just moving through the contract that had already been drawn. Ask for a field the schema doesn’t put there (a reviewer’s home address hanging off a review, for example) and the whole request is rejected before anything runs, because the path doesn’t exist. You don’t get the other fields back either.
A query is free to ask for any subset of what the schema offers, in any combination the types allow. It is not free to ask for anything else. The schema draws the map; the query picks a route through it.
Which product?
Back to (id: "field-runner-3").
Every product-page query in this post starts from the same place: an entry point called product. Part 2 left it out, but the schema has a top-level type for entry points, and this one is a single line: type Query { product(id: ID!): Product }. It hands back a Product. A product page is about one specific product, so the query has to say which one. That’s what id is for: it’s a value handed to the product field to pin down what you’re asking about. If you change it, then you’re asking about something else:
{
product(id: "field-runner-2") {
name
price
}
}
It’s the same kind of request, but it returns a different product: the Field Runner 2’s name and price instead of the Field Runner 3’s. The word for a value you hand to a field like that is an argument.
Arguments turn up wherever the schema allows them. If the schema gave reviews an argument, you could use it to say “just the first 3” (ours doesn’t), or you might pass one to a search field to say what you’re searching for. But the one on product is asking for something specific: the product with this id. Every team that touches products has to agree on what that id means and that it points at the same thing. When several teams have to agree on one thing, that agreement gets a name and a lot of machinery in part 8.
The same question, a different product
You don’t want to rewrite the query every time the id changes. You want to write it once and feed it a different id each run. So you pull the value out and leave a labeled hole:
query GetProduct($id: ID!) {
product(id: $id) {
name
price
}
}
Now $id is a blank you fill in when you send the request. Pass { "id": "field-runner-2" } alongside it and you get the Field Runner 2; pass a different id and you get a different product, but the query itself never changes. That labeled blank is a variable. Notice it now starts with the word query. The bare { … } form is a shorthand, and the shorthand can’t declare variables. Naming the operation GetProduct is optional, but it’s what makes it, formally, a named operation.
Asking it to change something
Everything so far only reads. You ask for what the screen needs, that’s what comes back, and the store is exactly as you left it. But eventually you’ll probably want to change something: add an item to a cart, save an address, post a review. GraphQL has a separate kind of operation for that, a mutation.
We won’t get into mutations here, but there is one thing that should be mentioned now. Ideally, a mutation always belongs to one team. Reading, you can pull price from Pricing and rating from Reviews in a single breath and never feel the seam. But changing the price is Pricing’s to allow or refuse, changing a review is Reviews’, and in a well-run graph no other team writes across that line. Part 7 is where that turns out to matter.
What this leaves out
There’s a third kind of operation, the subscription, for when you want the server to push you updates as they happen instead of asking once and getting one answer. It’s a genuinely separate machine, usually running over a different, long-lived connection, with its own failure modes, and we’re not going to get into it right now. Here’s where to read about it if you need it.
What’s above is by no means the whole language, but it’s most of what you’ll ever read in a ticket.
What the query doesn’t tell you
You can read a query now. Which is a useful skill, and it’s most of what this page set out to give you.
But there is plenty you still can’t say. When the query asks for price, something somewhere has to actually go and get the price — run some code, reach some system, produce the number 129. The query says what you want. It says nothing about who’s on the hook to produce it, where they sit, or what happens when price and rating come from two different teams’ systems.
You know how to ask. You have no idea who answers. That’s next.
Read it in the wild
One rep before you go. Open a public GraphQL explorer — countries.trevorblades.com runs one right in the browser, and you don’t need a login or a key. Pick a country by its code (country(code: "BR")) and ask for its name and capital. Then add currency to your selection set and run it again, and watch the answer grow by exactly that one field: the outline you drew, filled in. Take a field away and it drops out of the response.


Leave a Reply