| |

GraphQL vs. REST API – comparison

Have you ever heard of API? API stands for Application Programming Interface. As mobile applications became popular nowadays, the use of API also became a standard. For example, if a company has a website and a mobile application (both Android and iOS), they are not going to create separate databases. Instead, they use API to fetch data from their database, so the website and the application is synchronous.

— Brief History —

According to Devathon, REST APIs or RESTful API which stands for REpresentational State Transfer API was invented in 2000 by Roy FIelding and is often used since its appearance. Protocols like RPC, SOAP, and CORBA were commonly used before it became a standard to use RESTful APIs. Using these protocols involved several complexities, e.g., if you used CORBA, then any update to the interface required changes to both the client and server.

Image : Devathon Team

In 2012, Facebook created GraphQL and made it open-source in 2015. According to Adarsh, a writer in Developer Community, it is neither an architectural pattern nor a web service. It acts as an intermediary that helps querying the data received from various data sources. These data sources can be either databases or web services.

— GraphQL vs REST —

REST has been extremely successful and popular among software engineers. How ever it has a number of drawbacks :

  1. A lot of endpoints

GraphQL only has a single endpoint which is /graphql. Meanwhile REST has several endpoints. The defining concept of REST is the resource, and it identifies resources as URLs.

Below is an example image of an issue Facebook had, which guided the invention of GraphQL.

Image : Introduction to GraphQL | ARC Weekly Webinar #7

We know that in the image, a user has a post in which it has comments. REST stores resources in different endpoints and GraphQL only has one endpoint but we can specify a resource that we want to return in its query.

Image : Introduction to GraphQL | ARC Weekly Webinar #7

  1. Over-fetching or under-fetching data

Using GraphQL is more efficient as it is able to give only requested resources. Below is a simple comparison :

Image : Introduction to GraphQL | ARC Weekly Webinar #7

Image : Introduction to GraphQL | ARC Weekly Webinar #7

In short, GraphQL enables developers to only fetch requested data.

For instance, we want to only fetch users’ names. Image below shows that REST is over-fetching data as it sends unnecessary data such as users’ age and sex. On the other hand, GraphQL successfully returns users’ names only.

Image : Introduction to GraphQL | ARC Weekly Webinar #7

REST can also be under-fetching data. Image below indicates that GraphQL successfully shows users’ names and their respective salaries. Nevertheless, REST needs different endpoints and returns users’ salaries in a different reply.

Image : Introduction to GraphQL | ARC Weekly Webinar #7

It is said that REST also has various disadvantages such as :

  • Issues in versioning API
  • Handling network / graph data
  • Difficulty in understanding API

Regardless of difficulties and problems in using REST, it also has advantages. At the same time, as long as I have known, GraphQL has issues in caching.

Thank you for reading this article! Please feel free to give any corrections if you have any 🙂

References :

  1. Introduction to GraphQL | ARC Weekly Webinar #7. May 2020. Available at : https://docs.google.com/presentation/d/1sFUs_MUnmtv8eSu6IzBlv4t-KqnNc1ZNxBEPgMkZg8o/edit#slide=id.g6da9b2d44a_0_156.
  2. Devathon. GraphQL vs REST in 2020: A Detailed Comparison. 30 September 2019. Available at :  https://devathon.com/blog/graphql-vs-or-rest/.
  3. Dev Community, Adarsh. GraphQL or REST? What should I use?. 15 November 2018. Available at : https://dev.to/sadarshannaiynar/graphql-or-rest-what-should-i-use-38mj.
  4. Thumbnail by Fonseka Innovations. Available at : https://fonsekainnovations.com/graphql-rest-api-basics-comparison/.

Similar Posts