Query Variables Parameterizing Queries
With the help of variables, we can parameterize our queries and declare or initialize them in our queries to be later used as a placeholder for dynamic values inside the query body.
Example
In the below query, we have
- id, and name – they represent the fields that are present in the fragment that is defined on the Pokemon type.
- getPokemon – root field of the query that represents the data that the client wants to retrieve from the API.
- pokemonId – It represents a Query variable that imply that pokemonId will be passed in the query, and then we can reuse the pokemonId as the query argument to fetch and filter the results on the basis of it.
query ($pokemonId: ID!) {
getPokemon(id: $pokemonId) {
id
name
}
}
Querying Data with GraphQL
GraphQL is an open-source technology that allows us to query only the data that we require, unlike the traditional REST architecture which returns us the entire resources and data with specific endpoints configured for the same. Using GraphQL, we specify using the query the data that we want, and it returns the specified data in the specified format. It helps reduce the amount of data transfer over the network and saves network bandwidth. In this article, we will learn how to Query data using GraphQL.