Open In App

What is GraphQL Queries

Last Updated : 27 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

GraphQL is a powerful open-source Query Language for APIs. It is most commonly known for its single endpoint query which allows the user to define a single endpoint to fetch all the information needed.

Queries in GraphQL allow us to retrieve the data from an API endpoint, and the data is what we specify in the query itself. In this article, we will learn about GraphQL Queries, and we will create some ourselves.

What is a GraphQL Query?

A GraphQL query is the request that we send to the server to get back only the required fields in the response. It allows us to fetch only the required data from the API, unlike the REST architecture, which gives us limited control over the data that we want to receive, and rather sends the entire data, regardless of its usage on the client side.

In GraphQL Queries, we specify exactly the data we need from the server, and the server fetches the data from the DB and returns exactly those fields that the client requested, hence saving on a lot of network bandwidth, and the response time.

In the below Query examples, we will use the below GraphQL endpoint in the GraphQL Playground –

https://studio.apollographql.com/public/countries/variant/current/explorer

A GraphQL Query consists of fields that the user wants to be present in the final data from the server. Let’s look at an example Query below.

Javascript




query {
  country(code: "BR") {
    name
    currency
    languages {
      code
      name
    }
  }
}


In the above query

  • country(code: “BR”) : It represents the root field of the query that requests the data belonging to a Country entity “BR” from the server.
  • name, currency : They represent the respective fields of the Country object that will be returned as the Query response.
  • code, and name : They represent the nested fields present in the languages entity of the Country object.

Output:

The output of the above query will look like below

file

Characteristics of GraphQL Queries

GraphQL queries have certain benefits to it, and are characterized by its strongly typed nature, and its declarative schema.

  • Declarative Schema: The queries have a declarative schema such that the end users know exactly which fields are supported in the API, and which fields they can get back in the response.
  • Strongly-typed nature: Queries have a strongly-typed nature where the shape and structure of the data returned by the API is predetermined.

GraphQL Query Operations

A GraphQL query support 3 major types of operations –

  • Query: It is used to fetch the data from the API server
  • Mutation: It is used to update, insert, remove the data from the backend database or the API server
  • Subscription: It is used to subscribe to the backend to get real-time updates about the data changes.

Advantages of Using GraphQL Queries

A GraphQL query’s strongly typed nature and its predetermined schema gives it an edge over the existing REST framework for APIs.

  • The end users can only send the data in the payload that they want to see in the response, hence saving the network bandwidth
  • With the limited data we send and receive in the API request-response cycle, we eventually decrease the response time of the API thus giving a better UX.
  • GraphQL APIs are strongly typed which makes its API contracts simple to read, and easy to follow
  • GraphQL API also allow version upgradation without any breaking changes.

How to Execute GraphQL Queries in Postman

We can execute GraphQL queries in Postman by writing the queries in the request body, selecting the HTTP verb as POST, and changing the body type of the HTTP request to GraphQL.

In the below Query examples, we will use the below GraphQL endpoint in the Postman URL –

https://countries.trevorblades.com/graphql

Example: In this example, we will query the country list, and fetch a country with code “BR”.

query {
country(code: "BR") {
name
currency
languages {
code
name
}
}
}

graphql

Output:

The output of the above postman call will look like below

file

Conclusion

In this article, we learned about the GraphQL Queries and its anatomy, as well as how to execute the query in the Postman. GraphQL queries allow us to control the amount of data that we want to receive from the server, which in turn saves a lot network bandwidth, and request-response time, unlike REST architecture which gives us little control over the data being sent from the server to client. We also looked at some Query examples to see its usage in action



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads