documentation/getting-started/graphql/using-variables.mdx
Hoppscotch allows you to pass variables in the query to fetch data dynamically.
To demonstrate the use of variables, let's write a query to get countries by their name and continent. For example, we will fetch the details of "Bahrain" from the "Asia" continent.
Go to the variables section and define the variable.
{
"countryName": "Bahrain",
"continentCode": "AS"
}
Now create a query getCountries with variables as shown below:
query getCountries($countryName: String!, $continentCode: String!) {
countries(filter: {name: {eq: $countryName}, continent: {eq: $continentCode}}) {
name
continent {
name
}
code
emoji
currencies
}
}
Hoppscotch will retrieve the value of the variable and execute the query to get the below response.
{
"data": {
"countries": [
{
"name": "Bahrain",
"continent": {
"name": "Asia"
},
"code": "BH",
"emoji": "🇧🇭",
"currencies": [
"BHD"
]
}
]
}
}