How to use the GraphQL formatter
- Paste, drop or open a query, a set of operations or an SDL schema in the left pane.
- Choose Indent (2 spaces, 4 spaces or Tabs) and Line width (80, 100 or 120).
- Copy or Download the result, for example as
query.graphql.
Formatting uses Prettier’s GraphQL printer, the same one editors use for .graphql files.
Queries, variables and fragments
query User($id:ID!){user(id:$id){...UserFields posts(first:2){title}}} fragment UserFields on User{id name}
becomes
query User($id: ID!) {
user(id: $id) {
...UserFields
posts(first: 2) {
title
}
}
}
fragment UserFields on User {
id
name
}
Each field in a selection set gets its own line, arguments and variable definitions get consistent spacing, and fragment spreads stay where you wrote them.
Schemas (SDL)
type User{id:ID! name:String posts(first:Int=10):[Post!]!}
becomes
type User {
id: ID!
name: String
posts(first: Int = 10): [Post!]!
}
Default values, non-null markers and list types are kept exactly; only whitespace changes.
Syntax errors
Invalid input shows GraphQL’s own error message with the position:
| Input | Error |
|---|---|
query { user(id: 1) { name } |
Syntax Error: Expected Name, found <EOF>. at line 1, column 30 |
The usual causes are a missing closing brace, a missing colon between an argument and its value, and pasting a whole JSON request body instead of the query string inside it. Commas are optional in GraphQL, so extra or missing commas between fields are not errors.
Comments
Line comments that start with # are kept, including comments at the end of a line:
# top
query {
user {
name # the name
}
}
Why format GraphQL?
GraphQL documents grow quickly: a dashboard query can select dozens of nested fields across several fragments. Consistent indentation makes the nesting visible, so you can see which fields belong to which object and spot a field selected twice. Formatted operations also produce small, readable diffs when they are stored in .graphql files or persisted-query registries.
Tips
- If you copied a request from the browser’s network tab, the query is a JSON string with escaped newlines. Unescape it with JSON Escape / Unescape first, then format it here.
- Variables are JSON: format them with the JSON Formatter.
- Responses are JSON too, and often deeply nested. Explore a large response in the JSON Viewer, where you can search fields and copy the path of any value.
- Keep formatted
.graphqlfiles in version control so changes show up clearly in diffs.
Doing it locally
npx prettier --write "src/**/*.graphql"
Prettier also formats GraphQL embedded in JavaScript template literals tagged with gql or graphql when you run it on the file. Here, paste the query text itself; for the surrounding code, use the JavaScript Formatter.
Frequently asked questions
Is my query or schema uploaded?
No. Formatting runs in your browser with Prettier and nothing is uploaded, so internal schemas and queries stay private.
Can it format schemas as well as queries?
Yes. Queries, mutations, subscriptions, fragments and schema definition language (types, inputs, enums, interfaces, directives) are all supported, in the same document if you like.
Does it check my query against a schema?
No. It checks GraphQL syntax only. A misspelled field or a wrong argument type formats fine; your server or a tool like GraphiQL validates queries against the schema.
What happens to comments?
Comments that start with # are kept. Prettier may move a comment next to the line it belongs to, but it does not delete it.
Why do I get Syntax Error: Expected Name, found String?
You most likely pasted a JSON request body such as {"query": "…", "variables": {…}} instead of the query itself. Copy the value of the query field, unescape it with JSON Escape / Unescape if it contains \n sequences, and paste the result here.
Last updated