04 / 05

How to pass variables to query?

A GraphQL operation can be parameterized with variables, maximizing reuse, and avoiding costly string building in clients at runtime.

Operation Name:
  1. 1

    Declare $variableName as one of the variables accepted by the query

  2. 2

    Use the $variableName in the query

  3. 3

    Pass variableName: value in the separate, transport-specific (usually JSON) variables dictionary

  4. 4

    Values for those variables are provided to a GraphQL service as part of a request so they may be substituted in during execution.

javascript
Default values can also be assigned to the variables in the query by adding the default value after the type declaration: