Skip to content
GraphQL

Variables

Parameterize operations dynamically.

By EZ4Code Team
variableparameter

Code

query Search(
  $keyword: String!
  $limit: Int = 20
  $genre: Genre
) {
  search(keyword: $keyword, limit: $limit, genre: $genre) {
    id
    title
  }
}

# Variables sent alongside the document:
# { "keyword": "graphql", "limit": 5, "genre": "SCIENCE" }

Explanation

Variables decouple dynamic values from the operation text so the same document can be cached and reused. Each variable is declared with a type and may carry a default value, and required variables use the ! suffix. Variables are sent as a separate JSON object, preventing injection and enabling persisted queries.

More GraphQL Snippets