Query variables and directives in GraphQL

From the GraphQL cheat sheet · Queries · verified Jul 2026

Query variables and directives

graphql
# Query with variables
query GetPosts($limit: Int!, $offset: Int = 0, $published: Boolean) {
  posts(limit: $limit, offset: $offset, published: $published) {
    id
    title
    content
    author {
      name
    }
  }
}

# Variables object (sent separately)
{
  "limit": 10,
  "offset": 20,
  "published": true
}

# Directives
query GetUser($userId: ID!, $includeDetails: Boolean!, $skipPosts: Boolean!) {
  user(id: $userId) {
    id
    name
    
    # Include directive
    email @include(if: $includeDetails)
    phone @include(if: $includeDetails)
    address @include(if: $includeDetails)
    
    # Skip directive
    posts @skip(if: $skipPosts) {
      id
      title
    }
  }
}

# Custom directives
query {
  users {
    id
    name
    email @lowercase
    createdAt @formatDate(format: "MM/DD/YYYY")
    password @deprecated(reason: "Use auth service")
  }
}
💡 Variables make queries reusable
⚡ Directives modify query execution
📌 @include and @skip for conditional fields
🔥 Custom directives for advanced features

More GraphQL tasks

Back to the full GraphQL cheat sheet