Skip to content
Sealpaste

Delete all recent work?

This removes the 0 items saved in this browser. Your settings stay.

Stop saving recent work?

New work won't be saved in this browser. The 0 items already saved can stay or go.

GraphQL Formatter

GraphQL

Paste GraphQL, or

CtrlV paste · Ctrl↵ run now

Formatted GraphQL
Ready
Ln 1, Col 1
Runs in your browser. Nothing you paste is uploaded unless you share it.

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

  1. Paste a query, a mutation or a schema, or open a .graphql file. A request body ({"query": …, "variables": …}) works too.
  2. 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.
  3. 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

Input
query User($id:ID!){user(id:$id){name posts(first:2){title}}}
Result
query User($id: ID!) {
  user(id: $id) {
    name
    posts(first: 2) {
      title
    }
  }
}

A request body with its variables

Input
{"query":"query User($id: ID!) { user(id: $id) { name } }","variables":{"id":"42"},"operationName":"User"}
Result
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

Input
query Feed { feed { title author { name id } id } }
Result
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

Input
query { posts(first: 10, after: "Y3Vyc29y", orderBy: NEWEST) { title } }
Result
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

Input
type User{id:ID! name:String posts:[Post!]!}"""A blog post"""type Post{title:String}
Result
type User {
  id: ID!
  name: String
  posts: [Post!]!
}
"""
A blog post
"""
type Post {
  title: String
}

Minified for a request

Input
query User($id: ID!) {
  # the profile
  user(id: $id) {
    name
  }
}
Result
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

Operations (query, mutation, subscription), fragments, variables with defaults, directives like @include, and schema definitions with """descriptions""" are all read by the same rules as the GraphQL spec.

Formatting checks that the text is valid GraphQL, not that it matches your server’s schema: a field your API doesn’t have still formats.

Reading a syntax error

The error names the first place the text stops being GraphQL, which can be after the real mistake. Here the ) after the argument is missing, so the parser reaches { while it still expects another argument name:

  • 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

Paste a request body from the network tab and the query is taken out of its JSON string; the variables follow it as JSON. A JSON object after the last definition is read as the variables too.

A note names a variable the query needs (Type! with no default) that the JSON lacks, and a key no operation declares. Numbers keep every digit, as a 64-bit ID needs.

Sort, __typename and layout

Sort fields puts fields A to Z in every selection and in object, interface and input types; fragment spreads follow the fields, inline fragments keep their place. A server answers in the order a query asks, so the keys of the response follow.

Add __typename adds the field to every selection except an operation’s top level, where a client cache doesn’t need it, and skips a selection that already has it.

Both rebuild the query with graphql-js, so comments are left out; Drop comments removes them without changing anything else. Arguments and braces only move lines: what the query asks stays the same.

Minify

Minify uses graphql-js, the reference implementation, to drop comments and spaces while keeping the query the same to any server. A big schema is minified a definition at a time, so memory stays low.

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.

Related tools