← devnestio

GraphQL Query Formatter & Minifier

Input GraphQL
Output
Formatted or minified output will appear here

What is GraphQL Formatter?

GraphQL Formatter formats and validates GraphQL queries, mutations, subscriptions, and schema definitions. Paste minified or inconsistently formatted GraphQL to get properly indented, readable output with consistent spacing and line breaks. It also validates syntax, highlights the operation type, and shows the query complexity score.

GraphQL is an API query language that lets clients request exactly the data they need. Unlike REST APIs that have fixed endpoints returning fixed data shapes, GraphQL has a single endpoint where clients specify their exact data requirements. A query specifies which fields to return (avoiding over-fetching), while mutations modify data. Subscriptions enable real-time data via WebSocket. The schema (SDL — Schema Definition Language) defines all available types, queries, and mutations.

GraphQL query best practices: use named operations (query GetUser instead of anonymous {}), use fragments for reusable field selections, avoid deeply nested queries (implement pagination with first/after cursors), and use variables instead of inline arguments for dynamic values. Query complexity scoring (total fields × nesting depth) helps identify expensive queries before they hit production.

How to Use

  1. Paste your GraphQL query, mutation, subscription, or schema SDL into the input.
  2. Click 'Format' to apply indentation (2-space) and consistent line breaks.
  3. Click 'Validate' to check the syntax — errors are highlighted with line and column numbers.
  4. View the query type badge (Query/Mutation/Subscription) and field count in the info panel.
  5. Toggle 'Minify' to compress the GraphQL to a single line for use in API calls or config files.

Examples

Format a nested query

Result: query{user(id:1){name email posts{title createdAt}}} → properly indented multi-line

Validate schema SDL

Result: type Query { users: [User] } type User { id: ID! name: String! } → Valid schema

Parse a mutation

Result: mutation CreatePost($title: String!, $body: String!) { createPost(title: $title, body: $body) { id } }

Frequently Asked Questions

What is the difference between GraphQL and REST?

REST has multiple endpoints (GET /users, POST /users, GET /users/1/posts), each returning a fixed data shape. GraphQL has one endpoint (/graphql) where the client specifies exactly what data to return. Advantages of GraphQL: no over-fetching (REST returns fields you don't need), no under-fetching (REST may need multiple requests for related data), strongly typed schema that's self-documenting, and a single request to get nested related data. Disadvantages: more complex caching, n+1 query problem, higher learning curve.

What is a GraphQL fragment?

A fragment is a reusable set of fields that can be included in multiple operations: fragment UserFields on User { id name email }. Use it in a query: query GetUser { user(id: 1) { ...UserFields } } or in a mutation: mutation UpdateUser { updateUser(id: 1, name: 'Alice') { ...UserFields } }. Fragments reduce duplication in complex queries and ensure consistency when the same fields are needed in multiple places.

What is the N+1 problem in GraphQL?

The N+1 problem occurs when a query fetches N parent objects and then makes N additional queries for each parent's related data. Example: query { posts { id title author { name } } } — this might fetch 1 query for posts, then N queries for each post's author. Solution: DataLoader (batches and caches N author lookups into 1 query). DataLoader is the standard solution in Node.js GraphQL servers (Apollo Server includes it by default when configured).

What is GraphQL schema introspection?

GraphQL schemas are self-describing — you can query the schema itself to discover all types, fields, mutations, and subscriptions. The introspection query is { __schema { types { name fields { name type { name } } } } }. Tools like GraphiQL, Apollo Studio, and Postman use introspection to provide autocomplete and documentation. For production APIs, consider disabling introspection to avoid leaking your API structure to attackers.

What are GraphQL subscriptions and how do they work?

Subscriptions enable real-time updates — the server pushes data to clients when events occur. Example: subscription OnNewMessage { newMessage { id text author { name } } }. They're implemented over WebSocket (or SSE). The client subscribes once; the server sends data whenever the subscribed event occurs. Unlike polling (client requests every N seconds), subscriptions are push-based, reducing unnecessary requests. Apollo Server and graphql-ws are common implementations.

Related Tools