Declaring Variables in GraphQL
To declare a variable in Graphql. Variables can be defined withing parentheses and are prefixed with a dollar sign (“$”). We can also specify Graphql type, ensuring a clear definition of the expected data.
Syntax:
query MyQuery($variableName: VariableType) {
// Your query or mutation goes here, using $variableName where needed
}
Syntax breakdown here:
- $variableName: This is the declaration of the variable. Variable must be starts with the ‘$’ symbol.
- Variable Type: This is the GraphQL type of the variable. It could be any built type of Graphpql like (String,Int, Boolean,Float) or a custom type defined in the your Graphql.
- field(argument:$variableName): It is the example of the declared variable in the query. It shows how we can use this variable in the query.
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.
Contact Us