Introduction To GraphQL

When we talk about API design, the first thing that comes to our mind is REST (Representational State Transfer). In REST, data can be accessed from the server using url.
Back in the early 2000s, REST was best fit for most of the applications due to relatively simple client application,  and the development pace wasn’t really where it is today. However, nowadays, API’s have become more complex due to the below reasons:
  • Fast pace of development process: Frequent changes on the client side has impacted on the server side REST API, which slows down the Dev iteration.
  • Increased mobile usage created a need for efficient data loading.
  • Incorporating different client application using REST makes it difficult to build API’s
These shortcomings can be solved using GraphQL, an alternative to REST based architecture.
In this series of articles, we’ll discuss:
  • Introduction to GraphQL(Current article)
  • Query and Mutation in GraphQL
  • Consuming GraphQL

What is GraphQL?

GraphQL is a technology developed by Facebook and open sourced to the world in 2015.
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.
So, I’ll reiterate saying GraphQL is not any specific framework or database but it’s a specification that describes the capability and requirement of the data model.

Pros and Cons

Just like any architecture, GraphQL also comes with a few cons. To decide if GraphQL suits your needs, it’s important to understand Pros and Cons of GraphQL
  • A Single Endpoint:Unlike REST API, GraphQL API gets all your data in a single request.
  • Version Free: Client dictates the shape of response it expects from the API. As long as you don’t remove fields from the types, the client won’t break.
  • Overfecting/Underfecting of data: As REST API being a rigid data structure, it hard to define flexible API’s. Overfecting is something that client ends up retrieving more than necessary. On the flipside, client will not get expected data and will be forced to make additional request to the API. In GraphQL, you will get the exact data you needed.
  • Self-Documentation: Like Swagger, GraphQL has built-in mechanism to generate documentation and it also has playground to test your apps.
  • Performance/ Optimization Issues: Client has the flexibility to fetch what is needed. API’s might get lot of different requests, it’s more difficult to optimize the performance of the application.
  • No support for caching: Unlike REST API, GraphQL doesn’t support caching because all the request to the GraphQL API is only through Post method.
I hope you like the article. If you found the article interesting then kindly like and share it.