GraphQL Telegraph — GraphQL

Post office counter: 20 GraphQL levels to trace result JSON, write target queries and design schemas and resolvers, with N+1, DataLoader and pagination.

What you learn in this game

The schema (SDL) is the list on the counter's wall: which types there are and their fields. A query is a slip: which fields are asked for. A resolver is a function that fetches one field's value; at this counter a resolver opens a drawer of lemari, and every call to lemari counts as one trip to the filing cabinet. REST gives each resource its own address, and the server decides the shape of the answer. GraphQL has one address, and the slip decides which fields come back.

In the schema, a type marked ! (say String!) may not be null. A type without ! may. When a field marked ! turns out empty, graphql-js does not make a value up: it writes an error with that field's path, then turns the nearest parent that may be null into null. The part of the slip under that parent is torn off with it; the rest still comes.

A key in the answer is usually the field's name. An alias gives it another key: newKey: fieldName(arguments). With aliases one field can be asked for several times in one slip, each with its own arguments, without the answers colliding.

@include(if: …) keeps a field only when its condition is true; @skip(if: …) drops it when its condition is true. A dropped field does not appear in the answer at all, not even as null, and its resolver does not run. The condition can be written in place (true) or taken from a variable on the form ($rinci).

One slip can serve many forms: the value that changes is written as a variable. A variable is declared at the head of the operation with its type, query ($name: Type!) { … }, then used in an argument as $name. Its value comes separately from the slip. A variable's type must fit the argument it is used in, and a variable that is declared must be used.

A resolver for a field inside a list runs once for every row of the list. When that resolver opens a drawer of its own, one slip makes 1 trip for the list plus N trips, one per row: the N+1 problem. On small data nobody notices. On a list of a hundred rows, a hundred trips.