Cheatsheets / GraphQL

GraphQL Cheatsheet

Complete GraphQL reference. Hit Ctrl+P to print.

Queries

{ user { id name } }Shorthand query - implicit query operation
query GetUser { user { id name } }Named query operation
query { user(id: 1) { name email } }Query with field argument
query { user { name address { city country } } }Nested field selection
query { user { name createdAt: created_at } }Field alias - rename in response
query { a: user(id: 1) { name } b: user(id: 2) { name } }Multiple aliased fields of same type
query { users { id } posts { id } }Multiple root fields in one query
query { user { friends { name } } }Nested object fields

Variables

query GetUser($id: ID!) { user(id: $id) { name } }Declare variable with ! = required
query Search($q: String, $limit: Int = 10) { ... }Optional variable with default value
{ "id": "123" }Pass variables as JSON alongside the query
query ($ids: [ID!]!) { users(ids: $ids) { name } }List variable
mutation CreateUser($input: UserInput!) { createUser(input: $input) { id } }Input object variable

Mutations

mutation { createUser(name: "Alice") { id name } }Basic mutation
mutation AddUser($name: String!) { createUser(name: $name) { id } }Mutation with variable
mutation { updateUser(id: 1, input: { name: "Bob" }) { id name } }Mutation with inline input
mutation { deleteUser(id: 1) }Mutation returning scalar
mutation { a: createUser(name: "A") { id } b: createUser(name: "B") { id } }Multiple mutations - execute sequentially

Fragments

fragment UserFields on User { id name email }Define a named fragment on a type
query { user { ...UserFields } }Spread fragment into a selection set
query { user { ...UserFields } post { author { ...UserFields } } }Reuse fragment across multiple places
... on User { name }Inline fragment - apply fields if type matches
{ search { ... on User { name } ... on Post { title } } }Inline fragments for union/interface types
fragment F on User { friends { ...F } }Recursive fragment (use with caution)

Directives

field @include(if: $show)Include field only if $show is true
field @skip(if: $hide)Skip field if $hide is true
query GetUser($withEmail: Boolean!) { user { name email @include(if: $withEmail) } }@include with variable
@deprecated(reason: "Use newField instead")Schema directive - mark field as deprecated
@specifiedBy(url: "...")Schema directive - custom scalar specification URL
directive @auth(role: String!) on FIELD_DEFINITIONCustom directive definition in schema

Schema & Types

type User { id: ID! name: String! email: String age: Int }Object type - ! means non-null
type Query { user(id: ID!): User users: [User!]! }Root Query type
type Mutation { createUser(name: String!): User }Root Mutation type
type Subscription { userCreated: User }Root Subscription type
scalar DateCustom scalar type
enum Role { ADMIN USER GUEST }Enum type
interface Node { id: ID! }Interface - shared fields across types
type User implements Node { id: ID! name: String! }Type implementing an interface
union SearchResult = User | Post | CommentUnion type - one of multiple types
input CreateUserInput { name: String! email: String! }Input object type - for mutation arguments
[String]Nullable list of nullable strings
[String!]!Non-null list of non-null strings
type User { posts(limit: Int = 10): [Post!]! }Field with argument and default
schema { query: Query mutation: Mutation subscription: Subscription }Explicit schema definition (optional if using default names)

Subscriptions

subscription { userCreated { id name } }Basic subscription
subscription OnMessage($roomId: ID!) { messageAdded(roomId: $roomId) { text author } }Subscription with variable
type Subscription { userCreated: User! }Subscription type in schema

Introspection

{ __schema { types { name } } }List all types in schema
{ __schema { queryType { name } mutationType { name } } }Show root operation types
{ __type(name: "User") { fields { name type { name } } } }Inspect fields of a specific type
{ __type(name: "User") { kind name description } }Inspect type kind and description
{ __schema { directives { name args { name } } } }List all directives
{ field { __typename } }__typename - returns the type name of the object
{ __typename }Query root type name

HTTP Transport

POST /graphql Content-Type: application/jsonStandard HTTP transport
{ "query": "{ user { name } }", "variables": {}, "operationName": "GetUser" }Request body shape
{ "data": { "user": { "name": "Alice" } } }Success response body
{ "data": null, "errors": [{ "message": "Not found", "locations": [...], "path": [...] }] }Error response body
GET /graphql?query={user{name}}GET request for queries (not mutations)
Authorization: Bearer tokenAuth header - same as REST
multipart/form-data + map + operationsFile upload via GraphQL multipart spec