Define a Default Variable
In GraphQL, you can define default values for variables directly within the variable declaration. Default values are useful when you want to provide a fallback or default behaviour if a value is not explicity provided during the query execution.
To assign a default value to a variable in the query, add it after the type declaration, as follows:
query($id:ID=5){
person(id:$id){
name
address
}
}
In this example query
- ‘id’ is the variable declared within the parentheses of the query with its default value which is ‘5’.
- ‘ID’ is the type of the variable.
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.
Contact Us