Nested Queries Retrieving Related Data
With the help of nested queries, we can pull all the related data from a single query, instead of doing multiple queries.
In the below query, we have
- pokemon – root field of the query that represents the data that the client wants to retrieve from the API.
- pikachu – argument for the query passed that represents the specific criteria that the user wants the information about
- name, and id – they represent the fields that are requested by the user to be returned in the response object.
- types – It represent the nested field that the user wants the information about in the query response object.
- slot – It represent the selection set, or field for the nested type “types”. This implies that the user only wants the information about the slot of the pokemon from its types.
Example
{
pokemon(name: "pikachu") {
name
id
types {
slot
}
}
}
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.