Format GraphQL queries, mutations, subscriptions, and schema definitions (SDL) with syntax highlighting and validation.
GraphQL is an open-source data query and manipulation language for APIs, as well as a runtime for fulfilling queries with existing data. Developed by Meta and maintained by the GraphQL Foundation, it allows clients to request exactly the data they need and nothing more.
Unlike traditional REST APIs that require multiple round-trips to disparate endpoints (leading to over-fetching or under-fetching), GraphQL provides a single endpoint where clients request specific fields across connected entity graphs. Constructing, formatting, and inspecting GraphQL queries, mutations, and Schema Definition Language (SDL) types helps developers debug resolvers and optimize payload size.
A GraphQL document consists of operations (query, mutation, subscription) and fragments. The query AST (Abstract Syntax Tree) is validated against the server's type system. Variables are strongly typed (e.g., `$id: ID!`), and directives (such as `@include` and `@skip`) enable conditional field selection. Client-side formatting indents nested selection sets and cleans whitespace.
query GetUserProfile($userId: ID!, $includeReviews: Boolean = false) {
user(id: $userId) {
id
username
email
profile {
avatarUrl
bio
}
reviews @include(if: $includeReviews) {
id
rating
comment
createdAt
}
}
}
Queries are read-only operations executed in parallel by the server. Mutations are write operations (create, update, delete) executed sequentially to avoid race conditions.
Using variables prevents GraphQL injection attacks, allows the server to cache and reuse prepared query execution plans, and makes client-side code much cleaner.