How to Format GraphQL Queries - Step by Step Guide
Input Your GraphQL Query or Schema
Let's get your GraphQL into the formatter! Whether you're working with Apollo Client, need to validate schemas, or want to minify queries later, you have several convenient options:
Example: Unformatted GraphQL Query Input
Here's what messy, unformatted GraphQL looks like:
query{user(id:"123"){name email posts{title content comments{author message}}}}Automatic Formatting & Validation
About half a second after you stop typing, the formatter reads your GraphQL and:
Example: Beautifully Formatted Output
The same query, now properly formatted and readable:
query { user(id: "123") { name email posts { title content comments { author message } } } }
Choose Format Options
There are two ways to look at the result:
Export Your Formatted GraphQL
When it looks right, take it with you:
Frequently Asked Questions
How do I format messy GraphQL queries?
Paste the query into the input editor. Once you stop typing, it comes back with two-space indentation, one field per line and long argument lists broken over several lines. Queries copied out of browser DevTools, logs or a minified bundle usually arrive on a single line, and this is the quickest way to read them again.
Can I format GraphQL schemas as well as queries?
Yes. Anything the GraphQL spec allows is accepted: queries, mutations, subscriptions, fragments, and schema definitions such as type, interface, union, enum, input, scalar, directive and schema blocks, including their descriptions and extend statements.
Will formatting change my comments, strings or numbers?
No. Unlike printers that rebuild the document from scratch, this one keeps your # comments, and strings, block-string descriptions, enum values and numbers stay exactly as typed, so a default like 8901240544102066246 or 14.50 is not rounded or re-spelled. Before showing anything, the formatter parses its own output and only uses it if it describes exactly the same document as your input. Comments may move to their own line, but they are never dropped.
What happens when my GraphQL has a syntax error?
You get the error message from the GraphQL parser together with the line and column where it stopped, for example a missing closing brace or a colon in the wrong place. Nothing is fixed for you, and no output is shown until the document parses. If you paste JSON by mistake, such as a request body with a "query" field, the page tells you so.
What does the Minify button do?
It removes every space, line break, comma and comment that GraphQL ignores, using the same rules as the graphql-js stripIgnoredCharacters function. The text inside strings and block strings is left alone. Comments are the one thing that does not survive minifying, so keep the formatted version as your source copy.
Does the formatter check my query against my schema?
No, it only checks syntax. A query that asks for a field your API does not have will still format fine. To check a schema file on its own, use the GraphQL Schema Validator.
Is my GraphQL uploaded anywhere?
No. Formatting runs in your browser, so the query or schema is not sent to our server, and nothing is kept: reloading the page starts with an empty editor.
Related: check a schema file with the GraphQL Schema Validator, squeeze a query down with the GraphQL Minifier, or turn a schema into TypeScript types.
Related Tools
GraphQL Schema Validator
Validate GraphQL schemas with syntax and structure checking
GraphQL Fixer
Fix broken GraphQL queries and schemas automatically, repair syntax errors and malformed types
GraphQL to TypeScript
Generate TypeScript types from GraphQL schemas and queries
GraphQL Minifier
Compress and optimize GraphQL queries for production
GraphQL to JSON Schema
Convert GraphQL schemas to JSON Schema format
GraphQL to JSON
Convert GraphQL schemas and queries to JSON format