I worked on GraphQL. Here’s what I learned

Ashish Bania
3 min readApr 5, 2022

--

If you work in software you must have heard of GraphQL. I have recently been interested in it more deeply and below we explore it further.

Why do we need modern APIs?

The ever-changing fabric of the web has guaranteed one thing! Applications need to keep changing and evolving over time to remain relevant and useful. First, we delivered content to desktop browsers, and then soon enough mobiles came around. So instead of just delivering data to desktops and mobiles, the IoT(Internet of Things) has made all applications into spaghetti. API mesh to be specific. We need modern APIs to stay instant, social and connected.

The de facto way to deliver data over API is now microservices. There are mostly two types of APIs - SOAP and Rest. SOAP is on the way out now and Rest is the cool kid on the block. But how long will Rest stay as the gold standard?

Companies like Netflix and Soundcloud deliver data through API and follow a unique pattern called BFF(Backend for Frontend). This means the backend is now uniquely aligned with the frontend. The backend developers have always been the king of data and write APIs as per their understanding. But as more and more data needs to be delivered to a plethora of devices, APIs need to serve the frontend efficiently but effectively.

api

What's next?

Rest is an awesome mechanism to deliver data fast over HTTP but it comes with a litany of woes.

  • Rest design can be confusing and wrong as it's all about interpretation. The developer/designer often creates designs that are incorrect and wrong APIs all the time. This leads to additional code debt in the long run.
  • Rest is restrictive in specifying the output of an API. The API is usually designed for a vast array of consumers. Different consumers have different use cases and use a delta amount of data. This leads to either over or under-performant return data in the API output.
  • For the most basic data, APIs will return data by default but for any extended data, more API calls are needed. In general, to display data in a shopping cart at least 3 APIs will be needed - Cart API, Inventory API, and Product API.

Below are the most salient differences between REST and GraphQL.

Data Fetching

REST — Over or under fetching depending on the system requirements

GraphQL — Specify the attributes required in the response. This is most efficient as only required data is returned.

API versioning

REST — Versioning is allowed in REST but it also leads to added complexity.

GraphQL — GraphQL doesn’t allow versioning which leads to simplicity and less maintenance overhead.

For another take on my guide to choosing the right type of APIs, please check out my other article — https://ashish-bania.medium.com/the-exhaustive-guide-to-choosing-between-grpc-graphql-and-rest-b7e4fd6d547e

References

  1. https://github.com/ashishbania/graphql-java-book-tutorial
  2. https://ashish-bania.medium.com/the-exhaustive-guide-to-choosing-between-grpc-graphql-and-rest-b7e4fd6d547e
  3. https://www.graphql-java.com/tutorials/getting-started-with-spring-boot/
  4. https://github.com/graphql/graphql-playground

--

--

No responses yet