Format or minify GraphQL queries and schemas: sort fields, add __typename, lay out arguments, format a request body’s variables
Lay out a query copied from a network tab or a log, one field per line, with its variables beside it, or minify a query to send it in a URL or a request body.
How to use it
Paste a query, a mutation or a schema, or open a .graphql file. A request body ({"query": …, "variables": …}) works too. Under Options: sort fields A to Z, add __typename, drop comments, put arguments on one line or one per line, and braces on their own line. Switch Mode to Minify for the shortest form. Copy or download the result. A field or brace that doesn’t fit is named with its line and column.
Examples
A query from the network tab
query User($id:ID!){user(id:$id){name posts(first:2){title}}}query User($id: ID!) {
user(id: $id) {
name
posts(first: 2) {
title
}
}
}A request body with its variables
{"query":"query User($id: ID!) { user(id: $id) { name } }","variables":{"id":"42"},"operationName":"User"}query User($id: ID!) {
user(id: $id) {
name
}
}
{
"id": "42"
}The query comes out of its JSON string and the variables follow as JSON; a note names the operation.
Fields sorted, with __typename
query Feed { feed { title author { name id } id } }query Feed {
feed {
__typename
author {
__typename
id
name
}
id
title
}
}Options: Sort fields and Add __typename. The operation’s top level gets none, as Apollo Client adds it.
Arguments one a line from 2
query { posts(first: 10, after: "Y3Vyc29y", orderBy: NEWEST) { title } }query {
posts(
first: 10
after: "Y3Vyc29y"
orderBy: NEWEST
) {
title
}
}Options: Arguments, One a line from 2. Fit the width (the default) breaks a list only when it’s longer than the line.
A schema with a description
type User{id:ID! name:String posts:[Post!]!}"""A blog post"""type Post{title:String}type User {
id: ID!
name: String
posts: [Post!]!
}
"""
A blog post
"""
type Post {
title: String
}Minified for a request
query User($id: ID!) {
# the profile
user(id: $id) {
name
}
}query User($id:ID!){user(id:$id){name}}Mode: Minify. Comments and every space a GraphQL server doesn’t need go.
Queries and schemas alike
Reading a syntax error
Expected Name, found the end of the text: a closing } is missing; count the braces from the top. Unexpected "}" at the end: one } too many. Unexpected ")": an argument with no value, like id: ). Unexpected single quote character: GraphQL strings take double quotes only, "a" not 'a'. A query that formats but fails on the server asks for a field or argument its schema doesn’t have; this page checks syntax only.
query { user(id: 1 { name } }
Line 1, column 20: Expected Name, found "{"
Fixed: query { user(id: 1) { name } }Variables
Sort, __typename and layout
Minify
Questions
- Why does a missing brace show on the last line?
A brace left open is only noticed when the text ends, so the error points there: “Expected Name, found the end of the text”. The open brace is above it. - Can I format the query inside a JSON request body?
Yes: paste the whole body. The query is taken out of its "query" string and formatted, and the "variables" object follows it as JSON. Other fields such as extensions are left out, and a note says so. - Does sorting fields change what the server returns?
The same data, in a different key order: a GraphQL server answers fields in the order the query lists them. Code that reads the response by key isn’t affected. - Is my query uploaded?
No. Everything runs in your browser, so your query stays on your device. Nothing you paste is uploaded unless you share a result, which is locked with your password first. Queries copied from a browser can hold tokens in their variables.