API ArchitectureUpdated: 2026-11-07

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.

GraphQL is an open-source query language and runtime for APIs that enables clients to request exactly the data they need, eliminating over-fetching and under-fetching. Keep this cheat sheet handy for drafting queries, mutations, and reusable fragments.

Core GraphQL Language Primitives

Essential syntax components for queries and arguments.

Syntax ConstructExampleDescription
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.

Parameterized Query with Fragmentgraphql
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
    }
  }
}

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": { ... } }`.