GraphQL Cheat Sheet

Quick reference for GraphQL queries, mutations, schema definition, types, directives, and common patterns.

📚 Learn more: Read our GraphQL Complete Guide for in-depth explanations with examples.

Schema Definition (SDL)

Object Type

Define a type with fields

type User { id: ID! name: String! email: String age: Int posts: [Post!]! }

Input Type

Type for mutation arguments

input CreateUserInput { name: String! email: String! age: Int }

Enum Type

Set of allowed values

enum Role { ADMIN USER MODERATOR }

Interface

Abstract type with shared fields

interface Node { id: ID! } type User implements Node { id: ID! name: String! }

Union Type

One of several object types

union SearchResult = User | Post | Comment

Custom Scalar

Define custom data types

scalar DateTime scalar JSON scalar URL

Scalar Types

TypeDescriptionExample
IntSigned 32-bit integer42
FloatDouble-precision float3.14
StringUTF-8 character sequence"hello"
Booleantrue or falsetrue
IDUnique identifier (serialized as String)"abc123"

Type Modifiers

SyntaxMeaningNullable?
StringNullable stringYes
String!Non-null stringNo
[String]Nullable list of nullable stringsList and items
[String!]Nullable list of non-null stringsList only
[String!]!Non-null list of non-null stringsNeither

Queries

Basic Query

Fetch specific fields

query { user(id: "1") { name email } }

Nested Query

Follow relationships

query { user(id: "1") { name posts { title comments { text } } } }

Named Query with Variables

Reusable parameterized query

query GetUser($id: ID!) { user(id: $id) { name email } } # Variables: { "id": "1" }

Aliases

Rename fields in response

query { admin: user(id: "1") { name } editor: user(id: "2") { name } }

Fragments

Named Fragment

Reusable field selections

fragment UserFields on User { id name email } query { user(id: "1") { ...UserFields } }

Inline Fragment

Type-specific fields (unions/interfaces)

query { search(term: "hello") { ... on User { name } ... on Post { title } } }

Mutations

Create

Create new data

mutation { createUser(input: { name: "Alice" email: "alice@example.com" }) { id name } }

Update with Variables

Parameterized mutation

mutation UpdateUser( $id: ID! $input: UpdateUserInput! ) { updateUser(id: $id, input: $input) { id name email } }

Delete

Remove data

mutation { deleteUser(id: "1") { success message } }

Subscriptions

Basic Subscription

Real-time data via WebSocket

subscription { messageAdded(channelId: "1") { id text user { name } } }

Schema Definition

Root subscription type

type Subscription { messageAdded( channelId: ID! ): Message! userStatusChanged: User! }

Directives

@include / @skip

Conditionally include fields

query GetUser( $id: ID! $withPosts: Boolean! ) { user(id: $id) { name posts @include(if: $withPosts) { title } } }

@deprecated

Mark field as deprecated in schema

type User { id: ID! name: String! username: String @deprecated( reason: "Use name instead" ) }

Pagination Patterns

Offset-Based

Simple skip/take pagination

query { users(offset: 0, limit: 10) { id name } }

Cursor-Based (Relay)

Connections pattern for infinite scroll

query { users(first: 10, after: "cursor") { edges { node { id name } cursor } pageInfo { hasNextPage endCursor } } }

Root Schema Definition

schema { query: Query mutation: Mutation subscription: Subscription } type Query { user(id: ID!): User users(limit: Int, offset: Int): [User!]! search(term: String!): [SearchResult!]! } type Mutation { createUser(input: CreateUserInput!): User! updateUser(id: ID!, input: UpdateUserInput!): User! deleteUser(id: ID!): DeleteResult! }

Common HTTP Request

# POST /graphql # Content-Type: application/json { "query": "query GetUser($id: ID!) { user(id: $id) { name email } }", "variables": { "id": "1" }, "operationName": "GetUser" }

Error Response Format

{ "data": null, "errors": [ { "message": "User not found", "locations": [{ "line": 2, "column": 3 }], "path": ["user"], "extensions": { "code": "NOT_FOUND" } } ] }

Client Libraries

LibraryLanguageFeatures
Apollo ClientJavaScript/TypeScriptCaching, React integration, devtools
urqlJavaScript/TypeScriptLightweight, extensible, React/Vue/Svelte
RelayJavaScriptFacebook's client, compiler-driven, React
graphql-requestJavaScriptMinimal, promise-based, no framework
gqlPythonAsync, type-safe, code generation
graphql-clientGoType-safe, code generation

Server Libraries

LibraryLanguageApproach
Apollo ServerNode.jsSchema-first, plugins, federation
GraphQL YogaNode.jsBatteries-included, Envelop plugins
StrawberryPythonCode-first, type annotations
GraphenePythonCode-first, Django integration
gqlgenGoSchema-first, code generation
JuniperRustCode-first, type-safe

Related Resources

GraphQL Complete Guide
In-depth tutorial with examples and best practices
REST API Design Guide
Compare GraphQL with REST API design
JSON Formatter
Format and validate GraphQL JSON responses
REST API Cheat Sheet
Quick reference for REST API methods and status codes