Blog feed
A paginated query with a variable for the page size, a directive on the author, and a fragment that three screens can share.
Pikkola Schema Lab, tool No. 54
Paste a query or a schema. Get it formatted to two or four spaces, compressed to a single line, turned into TypeScript types, and answered with a mock response shaped by the selection set. The same document, read four ways, in this tab.
The studio
The left box holds the document: a query, a mutation, a fragment or a whole schema. The right box answers. Every press reads the document again, so what you see is what the parser understood.
TypeScript and mock data read this box. Formatting and checking work without it.
Four starts
Each card loads a document and the schema it belongs to, so the TypeScript and the mock answer straight away. Open one, break it, and watch the error name a line.
A paginated query with a variable for the page size, a directive on the author, and a fragment that three screens can share.
A mutation that takes an input object, returns a cart and a list of user errors, and nests money three levels down.
An interface with two concrete types, split apart with inline fragments, next to a fragment on the interface itself.
A whole schema in the definition language: descriptions, a union, an enum, an input type, a directive and an extension.
Four readers
The document is parsed once, into one tree. Each reader walks that tree for its own purpose, which is why the four answers never disagree.
| Reader | What it does | File |
|---|---|---|
| Formatted | Writes the tree back with two or four space indents, one field per line, arguments on one line until they get long, and comments where they were. | .graphql |
| One line | The same tree with no whitespace it does not need: for a URL, a log line or a query string. Comments are dropped. | .graphql |
| TypeScript | Reads the schema and writes interfaces, unions and enums. A nullable field says so, an optional input says so. | .ts |
| Mock data | Walks the query and the schema together and writes the JSON a server would answer with, aliases and list sizes included. | .json |
How it reads
A formatter is only as good as its parser. These are the three decisions that decide whether the output feels right.
01
GraphQL ignores both, so the lexer throws them away and the printer decides them again. That is why a document typed on one line and the same document typed across twenty lines format to the same bytes.
02
Depth counts how many selection sets sit inside each other. Cost counts fields, and multiplies a list by its first or last argument. A shallow query on a list of ten is more expensive than a deep query on one object.
03
The mock follows the types: an object becomes an object, a list of three becomes three, an enum takes its first value. It says nothing about what a resolver will return, which is exactly what makes it useful in a test.
No. Nothing is sent anywhere. The checker compares the document with the schema you paste, so it can find a field the schema does not define, a selection set on a scalar, a variable that was never declared or a fragment nobody spreads. It cannot know what a resolver returns at run time.
One point per field. A field with a first, last or limit argument multiplies the fields under it by that number, capped at ten. It is the usual way to put a number on a query before it reaches a server, and the limit is a suggestion rather than a rule.
In GraphQL a named type with no exclamation mark may be null. TypeScript has no other way to say that, so the generator writes the union. A required field has no union, and an input field that is optional or has a default gets a question mark.
It can mock a shape from the selection set alone, guessing strings, numbers and booleans from field names, but it cannot know that a field returns an object rather than a scalar. Paste the schema for a mock that matches the types.
Queries, mutations, subscriptions, fragments, variables and directives, and the schema definition language with scalar, type, interface, union, enum, input, directive and extend. Comments are kept where the document allows them, and the formatter can be run on its own output again without changing it.
The document, the schema, the language and the theme are kept in this browser so the page opens where you left it. The content policy sets connect-src to none, so nothing leaves the tab.