The Developer GraphQL Cheat Sheet: Queries, Mutations, Fragments & Directives
Quick reference for GraphQL query syntax: field selection, query variables, fragments, mutations, schema directives, and pagination.
Table of Contents
Core GraphQL Language Primitives
Essential syntax components for queries and arguments.
| Syntax Construct | Example | Description |
|---|---|---|
| Query Field | `user { id name }` | Requests specific properties from the user object. |
| Field Alias | `thumb: avatar(size: 64)` | Renames the returned JSON key to avoid collisions. |
| Directive @include | `bio @include(if: $withBio)` | Conditionally includes field only if boolean variable is true. |
| Directive @skip | `details @skip(if: $isSimple)` | Skips field if the provided boolean argument evaluates to true. |
Production Queries & Mutations
Standard parameterized query and mutation blocks.
fragment UserFields on User {
id
email
createdAt
}
query GetUserProfile($userId: ID!, $withAvatar: Boolean = true) {
user(id: $userId) {
...UserFields
avatarUrl @include(if: $withAvatar)
orders(first: 5) {
edges {
node {
id
totalAmount
}
}
}
}
}
mutation UpdateUserEmail($userId: ID!, $newEmail: String!) {
updateUser(input: { id: $userId, email: $newEmail }) {
user {
...UserFields
}
errors {
field
message
}
}
}Interactive Client-Side Tools for this Guide
Test and run these patterns directly inside your browser with zero server uploads:
cURL to Fetch, Axios & Python Code Converter
Paste raw cURL commands from browser DevTools and instantly convert them into clean JavaScript, Python, or Go code snippets.
JSON Formatter & Validator (Beautify & Minify)
Clean up messy API responses, fix syntax glitches, and compress JSON payloads directly in your browser.
Mock Data & Dummy JSON/CSV Generator
Generate realistic mock datasets for prototyping and testing. Customize schema fields and export instantly as JSON, CSV, or SQL INSERT queries.
Frequently Asked Questions
What is the difference between REST and GraphQL?
REST uses fixed endpoints returning fixed data structures; GraphQL uses a single endpoint where the client specifies the exact shape and fields of the response.
How do you send GraphQL requests via standard cURL or fetch()?
Send an HTTP POST request with `Content-Type: application/json` and a body containing `{ "query": "...", "variables": { ... } }`.