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.
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.
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.
Recommended by LinkedIn
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
Why to use GraphQL and when ?
Data Aggregation
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
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..!!!
Great article, Dablu! Very easy to understand!
Nice article. Way to go !!!
Very nicely documented and explained.Thank you for the nice article.
Nice article about GraphQL !!
Nice article, waiting for your next blog