Know All About GraphQL - API Query Language

Know All About GraphQL - API Query Language

Introduction

Are you a developer using REST API pattern ? Then must know about these 2 major challenges of using REST.

  • Over fetching
  • Too many Redundant patterns for multiple endpoints need

 We will talk about one of the trending topic of software engineering in this article and will see how we can overcome the challenges of REST in the most easiest solution.

What is GraphQL ?

Lets looks at the official Definition of GraphQL and try to understand in simpler terms.

"GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools."

Lets break this into 2 parts:

Definition Part-1 : “GraphQL is a query language” = GraphQL is a language for querying data

GraphQL is a language for querying data for API. This means we can define graphql schema for the data entity in the API so that client can query it for what they need.

e.g. : Say you want to query the Student data and only wants to know the student name, then you can query something like this. Things to keep in mind here is Student data is consist of lot more than just name, however we are just fetching name attributes as per need.

No alt text provided for this image


Definition Part-2 : “GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more”

This is a very interesting part of GraphQL that unlike any REST apis, you will not flooded with the complete response of your API whether u need it or not, graphQL cares about this specific need and give client the power to just filter for what they need.

e.g. : In below scenario, we are querying only the name and location from the Student API, it might have 100s of attributes which will not bother my response.

No alt text provided for this image

How GraphQL is different from REST ?

Over-fetching: GraphQL calls are handled in a single round trip. You can ask for exactly what you need with no overfetching. In typical REST, overfetching is often happens which might throttle your network bandwidth. 

Compatibility: GraphQL does not dictate a specific application architecture. It can be introduced on top of an existing REST API and can work with existing API management tools.

Single GraphQL Schema: GraphQL gives you the capability to write one schema which can overcome the multiple REST endpoint, its more simplified and less work.

e.g. : If you want to filter a student info in 2 different format, one with just name and location response and another with just name , date of birth and email. Technically in REST it will be 2 different endpoint, where in graphql you can just build one schema and query for what you need in one version.

Versioning: If you want to add a new fields to your REST API, versioning is needed but in Graphql you can add new fields and types without impacting existing queries.

Why to use GraphQL and when ?

 Data Aggregation: If you have a need for data aggregation when there is a need of a single point of entry for all data sources and microservices, you can build GraphQL applications that rely on multiple and heterogeneous data sources faster.  

Performance: GraphQL optimizes network requests by minimizing the number of queries required to retrieve data from your backend. It also allows you to specify how to structure the data to be received so your application only receives the data it needs, resulting in better network utilization and performance.

Microservices Design: GraphQL gets fit into microservices design pattern easily by decoupling the dependency of backend teams for UI. UI developers can easily build various contracts based on GraphQL schema and deliver.

Conclusion

GraphQL can solve the problem of over fetching by filtering only the data you need

GraphQL gives you the capability to write one schema which can overcome the multiple REST endpoint, its more simplified and less work.

Follow me if you like this article and Stay Tuned..!!!

#JavaTechBooster #restapi #graphql


Great article, Dablu! Very easy to understand!

Like
Reply

Very nicely documented and explained.Thank you for the nice article.

Nice article, waiting for your next blog

To view or add a comment, sign in

More articles by Dablu Gupta

  • The Why Behind @EnableAutoConfiguration

    In the last post, we decoded @SpringBootApplication — the big umbrella annotation that kickstarts every Spring Boot…

    13 Comments
  • Your API Isn’t Broken — It’s Just Screaming in Silence

    Exception handling is like insurance. You don’t think about it when everything’s working.

    6 Comments
  • 🔍 The Why Behind @SpringBootApplication

    Spring Boot makes it incredibly easy to build production-ready applications — but beneath that simplicity lies a…

    4 Comments
  • Why Kafka is so incredibly fast?

    Apache Kafka is a powerful open-source tool designed to handle large streams of data in real time. Imagine a super-fast…

    6 Comments

Others also viewed

Explore content categories