Skip to main content

Variables

Important

This part of the GraphQL API is currently in Early Availability status. For more information, see GraphQL API policy.

You can use variables with queries and mutations to make it easier and more efficient to send the same request repeatedly. When you identify a variable in a query or mutation, you provide the variable values separately, which allows you to use the same request body and update the variable values instead of creating new request bodies repeatedly.

In GraphQL, variables are identified with the $variableName convention. You provide the variable values, typically in JSON format, outside of the request body. Different explorers and API clients have different areas in which you provide the variable values.

The following example creates a product, just as described in Mutations. However, in this case we use a variable ($myVariable) to replace the product input object (ProductCreateInput!):

Request body

mutation($myVariable:ProductCreateInput!){
productCreate(product:$myVariable)
{
product{
name
id
vendorId
}
}
}

Variable values

{
"myVariable": {
"type": "WEB_APP",
"name": "My other product",
"addon": false,
"usageType": "MULTI_USER",
"allowMultiplePurchases": true,
"referable": false
}
}

Response

    {
"data": {
"productCreate": {
"product": {
"name": "My other product",
"id": "26db9280-1c3e-4ee7-adbd-90b8655f5800",
"vendorId": "ce0a5582-2601-4080-b969-99f949492982"
}
}
}
}

Was this page helpful?