GraphQL Cheat Sheet: Queries, Variables, Fragments and Schema SDL
Checked against GraphQL GraphQL spec September 2025 edition; examples run on graphql-js 17.1.0 on 28 November 2026. Each section links to the official docs it was checked against.
This sheet is for developers calling or building a GraphQL API who want the syntax without rereading the spec. It follows the September 2025 edition of the GraphQL specification and the graphql.org Learn pages. I ran the query, mutation, error and introspection examples against a small schema with the graphql npm package, version 17.1.0; subscription rows depend on a server transport and are marked untested.
When to use what
Always send operations with variables instead of pasting values into the query string. Variables are typed, validated before execution and keep the query text identical, which matters for caching and persisted queries. Name every operation too; anonymous queries make server logs and tracing much harder to read.
Use fragments when two parts of a UI need the same fields, and inline fragments (... on Type) when a field returns an interface or union. Aliases solve a different problem: asking for the same field twice with different arguments in one request.
On the schema side, make fields non-null (!) only when you can always return a value. GraphQL types are nullable by default for a reason: if a non-null field fails, the null spreads up to the nearest nullable parent and you lose more data than the one field. Prefer a single input type argument for mutations so you can add fields without breaking clients. The GraphQL vs REST post helps if you are still choosing. For day-to-day work, the GraphQL formatter cleans up queries, the query builder drafts them, and GraphQL to TypeScript turns SDL into types.
The grammar for all of these is in the Language section of the spec at https://spec.graphql.org/September2025/.
{ user(id: "u1") { name } }Shorthand query: no keyword or name; allowed only for a lone query with no variables or directives
query UserPage($id: ID!) { user(id: $id) { name } }Named operation with a required variable; pass {"id": "u1"} as the variables JSON
query Q($withPosts: Boolean = false) { ... }Variable with a default used when the client does not send it
me: user(id: $id) { name }Alias: the result key becomes me instead of user
latest: posts(first: 1) { title } all: posts { title }Query the same field twice with different arguments, told apart by aliases
{ node(id: "p1") { __typename } }Ask which concrete type came back from an interface or union field
fragment UserBits on User { id name }Define a reusable set of fields for a type
user(id: $id) { ...UserBits }Spread a named fragment into a selection
node(id: $id) { ... on Post { title } ... on User { name } }Inline fragments: pick fields depending on the runtime type
posts @include(if: $withPosts) { title }Only fetch the field when the variable is true
email @skip(if: $public)Leave the field out when the variable is true
Subscriptions: https://graphql.org/learn/subscriptions/. The spec doesn't fix a transport; WebSockets and server-sent events are the usual choices.
mutation Add($in: NewPost!) { createPost(input: $in) { id title status } }Call a mutation with an input object and select fields from the result
mutation { a: deletePost(id: "1") { id } b: deletePost(id: "2") { id } }Top-level mutation fields run one after another, not in parallel, but this is not a transaction
subscription OnComment($postId: ID!) { commentAdded(postId: $postId) { id body } }Receive an event each time a comment is added; a subscription must have exactly one root field
Types are nullable by default; ! makes them non-null and [T!]! is a non-null list of non-null items.
type User { id: ID! name: String! posts(first: Int = 10): [Post!]! }Object type with a field argument and a default value
enum Status { DRAFT PUBLISHED }Fixed set of allowed values
interface Node { id: ID! } type Post implements Node { id: ID! ... }Shared fields that several types promise to provide
union SearchResult = User | PostA field can return one of several object types with no shared fields required
input NewPost { title: String!, status: Status = DRAFT }Argument object for mutations; input fields can have defaults
oldTitle: String @deprecated(reason: "Use title")Mark a field deprecated; introspection hides it unless includeDeprecated is true
scalar DateTime @specifiedBy(url: "https://...")Custom scalar with a link to the spec that defines its format
input PetBy @oneOf { id: ID name: String }Input where exactly one field must be provided (September 2025 spec)
extend type Query { me: User }Add fields to an existing type, common when a schema is split across files
The graphql.org page has a section on introspection in production; many teams turn it off for public APIs.
{ __type(name: "Status") { kind enumValues { name } } }Inspect one type; for an enum this returns kind ENUM and its values
{ __type(name: "Post") { fields(includeDeprecated: true) { name isDeprecated } } }List fields including deprecated ones
{ __schema { queryType { name } types { name kind } } }Get the root query type and every type in the schema
Worked example: One request for a page, then two errors worth recognising
Against a small schema with User, Post and a Node interface, run with graphql-js 17.1.0:
query UserPage($id: ID!, $withPosts: Boolean = false) {
me: user(id: $id) {
...UserBits
latest: posts(first: 1) @include(if: $withPosts) { title }
}
}
fragment UserBits on User { id name }
variables: {"id": "u1", "withPosts": true}
{"data":{"me":{"id":"u1","name":"Ana","latest":[{"title":"Hello"}]}}}
The alias renamed user to me and posts to latest, the fragment supplied id and name, and @include kept latest because withPosts was true. With withPosts false the latest key is absent from the response, not null.
Sending the same kind of query without the id variable returned only an errors array, Variable "$id" has invalid value: Expected a value of non-null type "ID!" to be provided., with no data key, because validation failed before execution started. Asking for a field that doesn't exist (email on User) gave Cannot query field "email" on type "User". Both errors include a line and column you can map back to the query text.
Common mistakes
Building query strings by concatenating user input into arguments.
Declare variables in the operation and send values in the separate variables object.
Making every schema field non-null to save clients null checks.
A failing non-null field nulls its parent too. Keep fields nullable unless you can always resolve them.
Expecting two mutations in one request to roll back together if the second fails.
Top-level mutation fields run in order, but there is no transaction. Do the work in one mutation that the server wraps in a transaction.
Querying an interface field and only selecting fields of one implementation directly.
Use inline fragments (
... on Post { title }) for type-specific fields and add__typenameto tell results apart.Removing a field from the schema as soon as a replacement exists.
Mark it
@deprecated(reason: ...)first, watch usage, then remove it once clients have moved.Leaving the operation anonymous in production code.
Name every query and mutation so logs, metrics and persisted query lists can identify it.
Related cheat sheets
Sources
- https://spec.graphql.org/September2025/
- https://graphql.org/learn/queries/
- https://graphql.org/learn/schema/
- https://graphql.org/learn/mutations/
- https://graphql.org/learn/subscriptions/
- https://graphql.org/learn/introspection/
How we research: where our facts come from, how often we re-check them, and why no one can pay for a review or a verdict.