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
| Type | Description | Example |
|---|---|---|
Int | Signed 32-bit integer | 42 |
Float | Double-precision float | 3.14 |
String | UTF-8 character sequence | "hello" |
Boolean | true or false | true |
ID | Unique identifier (serialized as String) | "abc123" |
Type Modifiers
| Syntax | Meaning | Nullable? |
|---|---|---|
String | Nullable string | Yes |
String! | Non-null string | No |
[String] | Nullable list of nullable strings | List and items |
[String!] | Nullable list of non-null strings | List only |
[String!]! | Non-null list of non-null strings | Neither |
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
| Library | Language | Features |
|---|---|---|
Apollo Client | JavaScript/TypeScript | Caching, React integration, devtools |
urql | JavaScript/TypeScript | Lightweight, extensible, React/Vue/Svelte |
Relay | JavaScript | Facebook's client, compiler-driven, React |
graphql-request | JavaScript | Minimal, promise-based, no framework |
gql | Python | Async, type-safe, code generation |
graphql-client | Go | Type-safe, code generation |
Server Libraries
| Library | Language | Approach |
|---|---|---|
Apollo Server | Node.js | Schema-first, plugins, federation |
GraphQL Yoga | Node.js | Batteries-included, Envelop plugins |
Strawberry | Python | Code-first, type annotations |
Graphene | Python | Code-first, Django integration |
gqlgen | Go | Schema-first, code generation |
Juniper | Rust | Code-first, type-safe |