Aliases Renaming Fields in Responses
With the help of aliases, we can rename the fields on the client side to something other than that which is actually present in the Schema type. This gives us more flexibility to change the response data without changing the schema itself.
Example
In the below query, we have
- retrievePokemon – root field of the query that represents the data that the client wants to retrieve from the API.
- pikachu, and charizard – argument for the query passed that represents the specific criteria that the user wants the information about
- id – they represent the fields that are requested by the user to be returned in the response object.
- firstPokemon, and secondPokemon – they represent the aliases that are assigned to the pokemon field of the query, It implies that the data returned by the respective queries will be renamed as firstPokemon, and secondPokemon, respectively.
query retrievePokemon{
firstPokemon: pokemon(name:"pikachu") {
id
}
secondPokemon: pokemon(name:"charizard") {
id
}
}
Output:
The output of the above query call will look like below
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.