GraphQL Formatter
Format GraphQL operations and schema SDL locally, with readable or compact output, definition inventory and lexical checks for review.{{ values.formatted_document }}
{{ values.persisted_query }}
| Definition | Name | Kind | Signals | Copy |
|---|---|---|---|---|
{{ row.definition }} | {{ row.name }} | {{ row.kind }} | {{ definitionSignal(row) }} |
| Check | Status | Detail | Copy |
|---|---|---|---|
| {{ row.check }} | {{ row.status }} | {{ row.detail }} |
GraphQL documents carry more than one kind of text. An executable document may contain queries, mutations, subscriptions, and reusable fragments. Schema Definition Language (SDL) describes types, fields, inputs, enums, unions, and directives. Both use braces and names, but they serve different stages of an API workflow.
Formatting restores visible structure without needing to execute the document. Indentation shows which fields belong to a selection set, line breaks separate definitions, and retained comments can stay near the text they explain. Compact text has a different purpose: it removes ignored characters so an operation can be stored or transported with fewer bytes.
- Operation document
- Queries, mutations, subscriptions, variables, directives, and fragments. Review operation names, nesting, variable use, and the target schema.
- Schema SDL
- Types, fields, inputs, enums, interfaces, unions, scalars, and schema roots. Review definition boundaries and service compatibility.
- Mixed document
- Executable and schema definitions in one source. Confirm that the combination is intentional for the destination workflow.
- Persisted-query text
- A compact executable document, often with a stable operation name. Confirm the exact operation identity before registry or cache handoff.
GraphQL treats spaces, line terminators, commas, and comments as ignored text in defined contexts, but separators are still needed when adjacent tokens would otherwise merge. Strings and block strings are content, not formatting space. A safe transformation must distinguish ignored text from meaningful tokens before adding or removing whitespace.
Readable output is not proof of correctness. Balanced braces can surround a field that does not exist, a fragment can target the wrong type, a variable can have an incompatible value, and a directive can be invalid for its location. Those questions require the target schema and its validation rules.
A good handoff therefore has two checks. First, inspect the lexical structure and intended document role. Then validate the final text against the service schema or client build that will execute or publish it. Formatting reduces review effort; it does not replace the authority that knows the API’s types.
How to Use This Tool:
Choose the intended handoff, add the GraphQL source, and treat the local audit as a preparation step before schema-aware validation.
- Choose a Formatting profile. Use Review workspace for readable inspection, Persisted query handoff for compact executable text, or Schema SDL cleanup for schema definitions.
- Paste, drop, or browse one GraphQL, GQL, or text file in GraphQL document. Local files and editor content are limited to 1 MiB or 1,048,576 characters.
- Leave Document role on Auto detect for ordinary review, or pin operation, schema SDL, or mixed content when the destination contract is known. A mismatch becomes a warning rather than silently changing the source role.
- Set Indent width from 1 through 8 spaces. Open Advanced only when readable comments or the final newline need a specific policy.
- Inspect the warning state, Definition inventory, and Formatter audit. Compare directive-bearing or otherwise important output with the source, then run the formatted document through the target GraphQL schema before using it.
Interpreting Results:
Ready to review means the local pass found at least one top-level definition, closed strings, and balanced braces, parentheses, and brackets. Formatted with caveats means output exists but the requested role or persisted-query assumptions need attention.
- Definition inventory identifies operations, fragments, SDL definitions, extensions, and directives that begin at the top level.
- Payload footprint compares UTF-8 byte counts for the source, readable output, and compact output. A smaller compact count says nothing about schema validity.
- Schema-aware validation: Not run is a firm boundary. Field existence, type compatibility, fragment conditions, variables, and directive locations remain unchecked.
- For a persisted-query handoff, resolve warnings about missing executable operations, multiple operations, or anonymous operations before registry or cache use.
Technical Details:
The transformation is lexical. Source text is normalized to line-feed newlines, scanned into tokens, checked for paired delimiters, divided into top-level definitions, and written into separate readable and compact artifacts. It does not build or consult a GraphQL schema.
Transformation Core:
| Stage | Transformation | Failure or output |
|---|---|---|
| Normalize newlines | Convert carriage-return line endings to line feeds | One consistent line model for formatting and locations |
| Tokenize | Read comments, names, numbers, strings, block strings, spread markers, and supported punctuators | Unexpected characters and unclosed strings report a line and column |
| Check delimiters | Match { }, ( ), and [ ] with a stack |
An unexpected closer or unclosed opener blocks output |
| Write readable text | Indent brace blocks, space punctuation, and preserve or remove comments under the selected policy | Formatted document with an optional final newline |
| Write compact text | Remove comments and commas, omit optional whitespace, and keep separators where adjacent tokens require them | Persisted-query artifact with no trailing newline |
Rule Core:
Top-level role detection classifies queries, mutations, subscriptions, fragments, and anonymous selection documents as operation-side content. Schema, type, input, enum, interface, union, scalar, directive, and supported extension definitions are schema-side content. A document containing both categories resolves to mixed.
| Condition | Rule | Result |
|---|---|---|
| Pinned operation role with SDL definitions | Detected schema content conflicts with the requested role | Role warning |
| Pinned schema role with operations or fragments | Detected operation-side content conflicts with the requested role | Role warning |
| Pinned mixed role without both categories | The source does not contain the requested combination | Role warning |
| Persisted profile without an executable operation | Fragments or SDL alone do not satisfy the handoff | Persisted-query warning |
| Persisted profile with multiple or anonymous operations | The client needs a stable operation choice or name | Naming warning |
A comment policy applies only to the readable artifact. Compact output always removes hash comments and never adds a final newline. The byte measurements use UTF-8 encoding, so non-ASCII characters can occupy more than one byte even when the visible character count is unchanged.
Structural balance is necessary but incomplete. The local rules do not validate grammar productions beyond the supported lexical and delimiter checks, and they do not evaluate fields, arguments, variables, fragment applicability, directives, or values against a schema. Preserve the final service or build validation as a separate gate.
Privacy Notes:
The GraphQL source is treated as inert text in the browser. Pasted and selected-file content is not uploaded or executed for formatting. This makes local formatting suitable for sensitive drafts only when the browser, device, clipboard, downloads, and any later schema-validation service also meet the project’s handling requirements.
Remove live credentials, secrets, and personal data from example operations before copying or sharing them. Local processing does not make exported text safe for logs, tickets, or public repositories.
Worked Examples:
Readable named query
query Ping{ping} becomes a three-line readable document with a two-space field indent and final newline under the review profile. The compact artifact remains query Ping{ping}, and the inventory records one named operation.
Unclosed selection
query Broken{viewer{id} has no closing brace for the outer selection. The delimiter check reports the unclosed opener and withholds both artifacts until the source is corrected.
References:
- GraphQL Specification, September 2025 Edition, GraphQL Foundation, September 2025.
- How to scrape a GraphQL API with Scrapy, Simplified Guide.
- How to create a GraphQL request in JMeter, Simplified Guide.