GraphQL Query Formatter
Format, minify and validate GraphQL queries, mutations, fragments and schema definitions.
What is GraphQL?
GraphQL is a query language for APIs developed by Meta. Instead of exposing many fixed endpoints, a GraphQL server exposes a single endpoint and a typed schema, and clients describe exactly the fields they need. The server returns only those fields, which avoids both over-fetching and the round trips that plague REST when a screen needs data from several resources.
A GraphQL document is hierarchical: selection sets nest with curly braces, arguments sit in parentheses, and variables are referenced with a leading $. Because whitespace is insignificant, a query that arrived minified from a build step or a network log can be hard to read. This formatter rewrites it with consistent indentation and line breaks so the structure is obvious at a glance.
Before formatting, the document is parsed with GraphQL.js, the official reference parser. It understands queries, mutations, subscriptions, fragments, variables, directives, aliases, inline fragments and string arguments, plus schema definition language (SDL) blocks such as type and input. It also accepts the # comment syntax.
Key features
- Format — re-indent a document with 2 or 4 spaces per level for readability.
- Minify — strip whitespace and comments to produce the smallest payload for production.
- Syntax validation — parse with GraphQL.js and report real syntax errors with line and column numbers before formatting.
- Keep commas — optionally retain GraphQL's insignificant commas in the output.
- Sort simple fields — alphabetize plain fields inside each selection set without moving arguments, directives, fragments or nested selections.
- Copy JSON body or curl — turn the result into a request body for your API client.
- Live stats — see character and line counts for both input and output.
- Private — parsing, validation and formatting happen in your browser; queries are never uploaded.
How to Use
- Paste the query — Paste or type a GraphQL document, or click Example to load a sample query, mutation, fragment and directive.
- Choose your options — Select 2 or 4 spaces per level, tick Keep commas if you want them, and enable Sort simple fields to alphabetize plain fields.
- Format or minify — Click Format to beautify, or Minify to compress. Ctrl/Cmd+Enter also runs Format. Syntax errors from the GraphQL parser are reported with line and column numbers.
- Use the result — Copy the query, copy a JSON request body or curl command, or save it as a
.graphqlfile.
The GraphQL.js parser catches syntax mistakes such as unexpected tokens, unbalanced braces and unterminated strings. It does not validate against your schema: unknown fields, wrong argument types and missing required variables still need a GraphQL validator or your server.
Format vs Minify
Format is for humans: it adds newlines and indentation so that nested selection sets are easy to follow, and it preserves comments. Minify is for machines: it removes every optional space and all comments, which shrinks the payload you send over the network.
A minified query and its formatted equivalent are semantically identical to a GraphQL server, which ignores insignificant whitespace. That makes minification a safe size optimisation, not a transformation of meaning.
# Formatted
query {
users {
id
name
}
}
# Minified
query{users{id name}}
Worked Example: Variables, Fragment and Directive
A query copied from a log is often one dense line. Here is the same document before and after formatting; the variables, fragment spread and @include directive keep their meaning.
Input:
query Dashboard($id: ID!, $withEmail: Boolean!) { user(id: $id) { ...UserFields email @include(if: $withEmail) } } fragment UserFields on User { id name }
Formatted with 2 spaces:
query Dashboard($id: ID! $withEmail: Boolean!) {
user(id: $id) {
...UserFields
email @include(if: $withEmail)
}
}
fragment UserFields on User {
id
name
}
Turn Keep commas on if your team writes them; minification drops them again because GraphQL treats them as whitespace.
Common GraphQL Formatting Mistakes
- Unbalanced braces or parentheses — a missing
}changes the whole selection set. The GraphQL.js parser reports the offending line and column before you format. - Missing
$on variables —user(id: id)looks like a field reference, whileuser(id: $id)uses a variable. Formatting cannot infer which one you meant. - Confusing commas inside strings — commas in
"Ada, Lovelace"are literal text and must never be removed; commas between fields are only decoration. - Comments inside minified queries — a
#comment runs to the end of the line, so a query collapsed onto one line can accidentally comment out the rest of the document. Format first, then minify.
Formatting GraphQL Schema (SDL) vs Queries
Queries describe what a client wants; SDL describes what a server offers. This tool lays out both: operations such as query and mutation, and SDL definitions such as type, input, enum, interface and union.
type User {
id: ID!
name: String!
posts: [Post!]!
}
input UserFilter {
name: String
active: Boolean = true
}
The GraphQL.js parser checks the syntax of either kind of document, from unexpected tokens to unterminated strings. It does not resolve type references or validate a query against a schema — use a GraphQL validator, a linter or your server for that.
How to Format GraphQL in Your Editor
- VS Code + Prettier — install the Prettier extension and add
"[graphql]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }to your settings, or runnpx prettier --write query.graphql. - JetBrains IDEs — WebStorm and IntelliJ format
.graphqlfiles with the built-in GraphQL plugin; useCtrl+Alt+Lto reformat. - graphql-eslint — add
@graphql-eslint/eslint-pluginwhen you want lint rules and schema-aware validation, then runeslint --fix '**/*.graphql'.
Use this page for a quick one-off cleanup; use an editor integration when formatting should happen automatically on every save.
Send a Minified Query with curl or fetch
A GraphQL request is usually a JSON body with a query string and optional variables. Minify the query first, then paste it into the query field. This keeps the string on one line, so you do not have to escape line breaks.
Query:
query($id: ID!) {
user(id: $id) {
id
name
}
}
Minified:
query($id:ID!){user(id:$id){id name}}
With curl:
curl -X POST https://api.example.com/graphql \
-H "Content-Type: application/json" \
-d '{"query":"query($id:ID!){user(id:$id){id name}}","variables":{"id":"42"}}'
With fetch:
fetch('https://api.example.com/graphql', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
query: 'query($id:ID!){user(id:$id){id name}}',
variables: { id: '42' },
}),
});
To try the request against a real endpoint, use the GraphQL Query Tester.
When to format a GraphQL query
- Debugging — reformat a query captured from a network tab or an error log so you can spot the missing field.
- Documentation — produce clean, readable examples for a README or an API guide.
- Code review — beautify a pasted query before discussing it with a teammate.
- Production payloads — minify a query string before embedding it in client code to save bytes.
- Learning — see how nesting, arguments, and fragments map to indentation.
Are Commas Required in GraphQL?
No. In GraphQL, commas are treated like whitespace, so these two queries mean the same thing:
{ user(id: 1, name: "Ava") { id, name } }
{ user(id: 1 name: "Ava") { id name } }
Many teams still write commas for readability, which is why you will see both styles. Minifying a query drops them, because they carry no meaning.
Frequently Asked Questions
What types of GraphQL operations are supported?
This tool supports queries, mutations, subscriptions, fragments and SDL definitions. Variables, directives, inline fragments, aliases and string arguments are preserved.
Is my GraphQL query data stored on a server?
No. Formatting, checking and copying happen entirely in your browser. Your queries never leave your device.
What is the difference between Format and Minify?
Format adds indentation and line breaks for readability. Minify removes optional whitespace and comments to produce the smallest query string for production use.
Does the formatter validate my query?
Yes for syntax: the document is parsed with GraphQL.js, so unexpected tokens, unbalanced delimiters and unterminated strings are reported with line and column numbers. It does not compare your document against a schema, so unknown fields, wrong argument types and missing required variables still need a GraphQL server or linter.
Are GraphQL comments preserved?
When you click Format, lines beginning with # are kept and placed on their own line. When you click Minify, comments are removed along with the extra whitespace, since comments are meaningless to the server.
Which indentation can I choose?
The Indent selector offers 2 spaces and 4 spaces per nesting level. Pick whichever matches your team style; the choice only affects readability, not the meaning of the document.
Can I format fragments and variables?
Yes. Named and inline fragments, fragment spreads written as ...Name, and variable definitions such as ($page: Int) are all tokenised and laid out correctly.
Can I save the result to a file?
Yes. The Download button saves the current output as a plain-text file named query.graphql, which you can drop straight into a project or commit to a repository.
How do I put a multi-line GraphQL query in a JSON request body?
Click Copy JSON body to get a ready-to-paste {"query": "..."} object. The tool escapes newlines and quotes for you; Copy curl wraps the same body in a curl command. See the curl and fetch examples above.
Can I format GraphQL schema definitions?
Yes. SDL blocks such as type, input, enum, interface and union are laid out with the same indentation rules as operations. Schema-aware validation is out of scope, so a field that refers to an undefined type will still be formatted.
How do I keep commas in the output?
Enable the Keep commas checkbox before clicking Format. Commas are insignificant to GraphQL, so Minify drops them again unless you leave the checkbox on.
How do I sort fields alphabetically?
Enable Sort simple fields before clicking Format. Plain fields are sorted by name inside each selection set; fields with arguments, directives, sub-selections, aliases, fragment spreads and comments keep their positions because moving them can change meaning.
Is there a keyboard shortcut for formatting?
Yes. Press Ctrl/Cmd+Enter to run Format with the current options.
How do I format GraphQL with Prettier?
Prettier supports GraphQL out of the box. Run npx prettier --write query.graphql in a project that has Prettier available, or use this tool for a quick one-off format in the browser.
Related Tools
SQL Formatter
Format and beautify SQL queries
JSON Formatter
Format and validate JSON data
JS Beautifier
Format JavaScript code
HTML Formatter
Format and beautify HTML code
CSS Beautifier
Format and beautify CSS code
XML Formatter
Format and validate XML documents
GraphQL Query Tester
Run a query against an endpoint
API Request Builder
Build and inspect API requests
cURL Builder
Generate cURL commands from a request
JSON Escape / Unescape
Escape query strings for a JSON request body
Code Formatters Hub
Browse JSON, HTML, CSS, JS, GraphQL, SQL and XML formatters