← Cheatsheets
Tags: graphql, queries, mutations, subscriptions,
fragments, apollo, caching, api
Last updated: 2026-06-26
GraphQL Cheatsheet
Quick Reference
| Concept | Syntax |
| Query | query { users { id name } } |
| Mutation |
mutation { createUser(...) { id } } |
| Subscription |
subscription { messageAdded { text } } |
| Fragment |
fragment F on User { id name } |
| Variable |
query($id: ID!) { user(id: $id) } |
| Alias |
me: user(id: 42) { name } |
| Inline fragment |
... on Admin { permissions } |
Queries
query {
user(id: "42") {
id
name
email
posts(limit: 5) { title createdAt }
}
}
With Variables
query GetUser($id: ID!, $withPosts: Boolean!) {
user(id: $id) {
id
name
posts @include(if: $withPosts) { title }
}
}
# Variables: { "id": "42", "withPosts": true }
Aliases
query {
admin: user(id: "1") { name role }
viewer: user(id: "2") { name role }
}
Mutations
mutation {
createUser(input: {
name: "Max", email: "[email protected]"
}) {
id
name
createdAt
}
}
# With variables
mutation UpdateUser($id: ID!,
$input: UpdateUserInput!) {
updateUser(id: $id, input: $input) {
id
name
updatedAt
}
}
Subscriptions
subscription OnMessageAdded($roomId: ID!) {
messageAdded(roomId: $roomId) {
id
text
sender { name }
createdAt
}
}
Fragments
fragment UserFields on User {
id
name
email
}
query {
user(id: 42) { ...UserFields }
users { ...UserFields }
}
# Inline fragments (unions)
query Search($term: String!) {
search(term: $term) {
__typename
... on User { id name }
... on Post { id title body }
}
}
Apollo Client
Setup
const client = new ApolloClient({
link: new HttpLink({ uri: "/graphql" }),
cache: new InMemoryCache(),
});
useQuery
const { data, loading, refetch } =
useQuery(GET_USER, {
variables: { id: "42" },
pollInterval: 5000,
});
useMutation
const [createUser] = useMutation(
CREATE_USER, {
refetchQueries: ["GetUsers"],
});
createUser({
variables: { input: { name: "Max" } }
});
Cache Policies
const cache = new InMemoryCache({
typePolicies: {
User: {
keyFields: ["id"],
},
Query: {
fields: {
users: offsetLimitPagination(),
},
},
},
});
Tips
- Use aliases to request the same field with
different arguments.
- Fragments keep queries DRY — define once per
type.
- GraphQL returns 200 OK on application errors —
check
data.errors, not HTTP status.
- Use persisted queries (APQ) to send hashed
queries and improve performance.