TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About ยท Labs ยท Cheat sheets ยท Learning paths

โ† Back to GraphQL

๐Ÿฑ GraphQL: cheat sheet

๐ŸŽจ The analogyOrdering a custom bento box on one form: tick exactly what you want, and kitchen stations fill each compartment.
๐ŸŽ“ The regular explanationA query language and runtime for APIs. A typed schema describes all the data, and each client sends one request, usually a POST to a single endpoint, naming exactly the fields it wants back.
One order instead of three queuesStep 5 of 5
๐Ÿ™‹Miaexactly 3 fields๐ŸชŸOrder window๐ŸšRice counter๐ŸŸFish counter๐ŸกSweets counter
  1. Mia only wants some rice. But the rice counter sells one fixed tray, so she gets soup, pickles and salad too.
  2. She still wants fish and a sweet, so she queues at two more counters. Three queues for one lunch.
  3. Now the bento shop opens one order window. Mia ticks rice, fish and a sweet on a single form and hands it in.
  4. Behind the window, helpers collect each item from its counter.
  5. One box comes back with exactly the three things she ticked. Nothing missing, nothing extra.
The schema decides what can be askedStep 4 of 4
kept open๐Ÿ™‹Mia๐ŸชŸOrder window๐Ÿ“œ Printed menu๐Ÿฅซ Storeroom
  1. Mia asks the window for a copy of the printed menu, so she can see every box and item that exists.
  2. She ticks "dragon roll", which isn't on the menu. The window hands the form straight back before anyone cooks.
  3. The menu also has a section for changes. Mia asks to save her box as a favourite, and the shop writes it down.
  4. Last, Mia asks to hear about every new special. Each time one comes out, the shop calls it out to her.
A resolver for every fieldStep 4 of 4
๐Ÿ™‹Mia๐Ÿง‘โ€๐ŸณHead cookname from parent๐Ÿ—‚๏ธAccount deskparent๐ŸŸFish station๐ŸกSweets station
  1. Mia's form arrives. The head cook reads it from the top and checks it against the menu.
  2. First, someone finds Mia's account. Everything else in the box depends on it.
  3. Each station is told "this is for Mia" and fills its own compartment, at the same time.
  4. Her name on the lid needs no station, it's already on her account card. The full box goes out.
From eleven trips to twoStep 4 of 4
๐Ÿ‘ฅTen customers๐ŸŸFish station๐ŸƒRunner10 keys๐Ÿฅซ Storeroom2 queries
  1. Ten customers each order a box with fish. Finding the ten orders takes one trip to the storeroom.
  2. A careless cook walks to the storeroom for each piece of fish, one at a time. Ten more trips.
  3. A smart shop has a runner. Each ticket tells the runner which fish it needs, and the runner waits to collect them all.
  4. The runner makes one trip with one list and hands each station its piece. Two trips instead of eleven.
One endpoint over HTTPStep 5 of 5
๐Ÿ™‹Mia๐Ÿ—„๏ธReady-made shelfkeeps box 12๐ŸชŸOrder windowtoo deep
  1. Mia's order is a folded form. The ready-made shelf can't see inside it, so it just passes it on to the window.
  2. There's no fish today. The box still comes, with that compartment empty and a note inside saying why.
  3. Next time she orders "the usual, number 12". A number on the outside is easy to recognise, so the shelf keeps a packed copy.
  4. Ordering number 12 again, she gets it straight off the shelf. The window isn't even bothered.
  5. Someone orders a box inside a box inside a box, a hundred times over. The window refuses it before any cooking starts.
Spring for GraphQL controllersStep 4 of 4
๐Ÿ™‹Mia๐ŸชŸOrder window๐Ÿ“œ Printed menu๐Ÿง‘โ€๐ŸณBox cook๐ŸŸFish cookcalled once
  1. The manager pins the printed menu on the wall and puts a label on each cook. Mia's order arrives at the window.
  2. The cook labelled "you make the boxes" makes three boxes.
  3. The cook labelled "all the fish at once" gets all three boxes together and fills every fish compartment in one go.
  4. The window packs the three boxes, each with its fish, and hands them to Mia.

๐Ÿงฉ Key questions

At the old food hall, Mia gets soup and salad she never asked for, then queues at two more counters. What does the bento form fix?
She ticks exactly what she wants and gets it all in one box. One form, one box, nothing missing and nothing extra. Keeping copies on the shelf actually gets harder.
Ten boxes each need fish, and the cook walks to the storeroom once per box. What's the fix?
A runner who collects every request and makes one trip with one list. One trip for the orders and one for all the fish: two trips instead of eleven.
Mia's box arrives, but one compartment is empty. What should she do?
Read the note inside, which says what went wrong. The shop sends what it could and tucks a note in for what it couldn't.
Mia ticks a dragon roll, which isn't on the printed menu. What happens?
The window hands the form back before anyone starts cooking. The menu lists everything that exists, so the window checks every form against it first.
Why does the window refuse a box inside a box inside a box, a hundred times over?
Because one silly form could keep every station busy all day. Customers write their own forms, so the shop must turn down the ones that would swamp the kitchen.