Declaring Multiple Variables
In GraphQL, you can declare mutliple variables in a query or mutation by including them in the parenteses following the query or mutation keyword. Each variable is declared with a name, a colon to indicate its type, and optionally an equals sign and a default value.
Query:
query MyComplexQuery($var1: String!, $var2: Int!, $var3: Boolean) {
firstData: getData(id: $var1) {
id
name
}
secondData: getMoreData(limit: $var2) {
id
description
}
optionalData: getOptionalData(flag: $var3) {
id
value
}
}
Variable Value:
{
“var1”: “abc123”,
“var2”: 5,
“var3”: true
}
Output:
Variables in GraphQL
GraphQL is a query language for API (Application Programming Interface). It was developed by Facebook in 2012 after they released it publicly in 2015. Graphql Provides a more efficient, powerful, and flexible alternative to traditional REST APIs. In this article, we will learn about variables in GraphQL.