Archives
- Newer posts
- November 2024
- April 2024
- November 2023
- October 2023
- August 2023
- May 2023
- February 2023
- October 2022
- August 2022
- July 2022
- May 2022
- April 2022
- March 2022
- February 2022
- June 2020
- March 2020
- February 2020
- January 2020
- December 2019
- November 2019
- October 2019
- September 2019
- August 2019
- July 2019
- June 2019
- May 2019
- April 2019
- March 2019
- February 2019
- January 2019
- December 2018
- November 2018
- October 2018
- September 2018
- August 2018
- July 2018
- June 2018
- May 2018
- April 2018
- March 2018
- February 2018
- January 2018
- December 2017
- November 2017
- October 2017
- September 2017
- August 2017
- July 2017
- June 2017
- May 2017
- April 2017
- March 2017
- February 2017
- January 2017
- August 2016
- June 2016
- April 2016
- March 2016
- February 2016
- January 2016
- July 2015
- June 2015
- Older posts
GraphQL vs Rest API
Introduction to GraphQL
GraphQL is a data query language architectured and developed by Facebook. It was developed for as an alternative to REST API. It gives a total and reasonable depiction of the information in your API, enables customers to request precisely what they need and nothing more makes it less demanding to advance APIs after some time and empowers ground-breaking developer tools.
Introduction to REST API
For the past decade or so we have been using REST API. It is the most popular API. Most of the businesses have adopted the trend of REST API. This can be used over nearly any HTTP methods. As it works on HTTP as well as HTTP2 protocol, this enables developers to reduce the dependency over additional software.
Similarities between Rest and GraphQL
We come across a new problem with every evolving technology and hence we come up with a new technology that intends to solve those problems. But there lie some similarities between them and this is also true with these two APIs. So what could these two share for all intents and purpose? Indeed, one clear thing they share for all intents and purpose is that they are the two details for building and expending APIs. Additionally, REST and GraphQL can both be worked over HTTP, however, GraphQL is convention skeptic. GraphQL Endpoint is mostly called using an HTTP post Method.
Differences between Rest and GraphQL
So now we can come to the differences and see exactly what problems these two API’s were intended to resolve.
Data Fetching
GraphQL has a query based data fetching mechanism. In case of REST API, several requests are made to single or multiple endpoints for data retrieval purpose.
Network Requests
It might take REST to make 10 requests to get a particular data but using GrapgQL, a significant advantage is that can now be done in one request itself.
Over/UnderFetching
In GraphQL we can request only for the data we want and ignore fetching of unwanted data.
But the same isn’t true for REST, as it fetches everything, from which unwanted data need to be discarded by the endpoint consumer.
Error Handling
Error handling in REST is pretty clear, we essentially check the HTTP headers to get the status of a response. Depending on the HTTP status code (404, 503, 500, etc.) we get, we can without much of a stretch tell what the Error is and how to approach settling it. GraphQL then again, when worked over HTTP, we will always get a 200 OK reaction status.
Caching
As REST is implemented using Http which already has caching baked in, it avoids refetching of data. However, GraphQL has no caching functionality baked in for caching.
Tools Used for Graphql
• Graphiql
• Apollo Optics
• Graphcool
• GraphCMS
Tools for RestAPI
• Postman
• Insomnia
• Rest Client
Conclusion
Based on my experience, I feel GraphQL reduced my coding time by considerably reducing the code that I had to write as compared to what I had to write for the REST API. There is no perfect fit for all situations and conditions and so first you will need to check your situation and need, then based on that, you will have to make your decision.