GraphQL Cheatsheet - GraphQL Query Language & API Reference

This reference is for developers designing a GraphQL schema or consuming one from Apollo/URQL/etc. It covers the client-facing vocabulary first — querying exactly the fields you want, mutating data, subscribing to updates, and reusing fragments — then the schema side from the type system and directives to pagination and the introspection __typename. Unlike a generic syntax sheet, items are grouped by the role you are playing in an API conversation. After reading you should be able to shape a precise query with arguments and fragments, declare a type with the right scalar/object fields, and add a connection-style paginated field.

Languages·42 commands·Last updated 2026-07-21
graphqlapiQuery Languages

Query 9

{ user { id name } }
Basic query
{ user(id: 1) { name } }
Query with arguments
{ users { id name posts { title } } }
Nested query
query GetUser($id: ID!) { user(id: $id) { name } }
Query with variables
{ user { name email } posts { title } }
Multi-field query
{ user(id: 1) { name @include(if: $withName) } }
Conditionally include a field
{ user(id: 1) { name @skip(if: $skipName) } }
Conditionally skip a field
{ me: user(id: 1) { name } you: user(id: 2) { name } }
Alias query for same type, multiple fields
{ users(first: 10) { edges { node { name } } } }
Connection-style pagination query

Mutation 5

mutation { createUser(name: "Alice") { id } }
Basic mutation
mutation CreateUser($input: CreateUserInput!) { createUser(input: $input) { id } }
Mutation with variables
mutation { updateUser(id: 1, name: "Bob") { id name } }
Update operation
mutation { deleteUser(id: 1) { id } }
Delete operation
mutation { bulkCreate(input: [{ name: "A" }, { name: "B" }]) { id } }
Bulk create operation

Subscription 3

subscription { newMessage { content } }
Subscribe to new messages
subscription { userUpdated(id: 1) { name } }
Subscribe to user updates
subscription($roomId: ID!) { messageAdded(roomId: $roomId) { id content } }
Subscription with variables

Schema (Type System) 10

type User { id: ID! name: String! email: String }
Define an object type
input CreateUserInput { name: String! email: String! }
Define an input type
enum Role { ADMIN USER GUEST }
Define an enum
union SearchResult = User | Post
Define a union type
interface Node { id: ID! }
Define an interface
type User implements Node { id: ID! name: String! }
Implement an interface
scalar DateTime
Custom scalar type
type Query { user(id: ID!): User }
Define the Query root
type Mutation { createUser(input: CreateUserInput!): User }
Define the Mutation root
directive @auth(requires: Role!) on FIELD_DEFINITION
Declare a custom directive

Fragments & Directives 5

fragment UserFields on User { id name email }
Define a fragment
{ user { ...UserFields } }
Use a fragment
{ user { ...UserFields ... on Admin { permissions } } }
Inline fragment for union types
@deprecated(reason: "Use newField")
Mark a field deprecated
@cacheControl(maxAge: 3600)
Cache control directive

Introspection & Validation 5

{ __schema { types { name } } }
Query all types
{ __type(name: "User") { fields { name type { name } } } }
Query a single type's fields
{ __schema { queryType { name } mutationType { name } } }
Query root types
{ __type(name: "User") { fields { name args { name type { name } } } } }
Query field arguments
{ __schema { directives { name locations } } }
Query all directives

Advanced Features 5

query($id: ID!, $withPosts: Boolean!) { user(id: $id) { name posts @include(if: $withPosts) { title } } }
Combine variables and directives
{ users(first: 10, after: "cursor") { pageInfo { hasNextPage endCursor } } }
Relay-style pagination
{ user { name } } # expected user.name; errors returned alongside data
Errors returned alongside data
query @cost(complexity: 10) { user { name } }
Query complexity directive
extend type User { age: Int }
Extend an existing type

Tips

  • GraphQL returns only the fields the client asks for; the server need not return everything.
  • Variables use a $ prefix and are defined outside the query to avoid string concatenation.
  • The Schema Definition Language (SDL) is core to GraphQL: define types first, then implement resolvers.
  • Introspection queries (__schema/__type) can generate TypeScript types and editor tooling.
  • Prefer the Relay connection spec (edges/node/pageInfo) for pagination, supporting forward and backward paging.

Official References

Each command links to its official documentation below, so you can verify the latest usage and read deeper.

Maintained by LaoHand

Publicly updated on Jul 21, 2026, continuously proofread against official docs.

Contact Us

Wrong command or description? Send us corrections, business inquiries or product feedback by email.

Contact Us