When we talk about API design, first thing that comes to our mind is REST(Representational State Transfer). In REST, data can be accessed from server using url.
Back in early 2000s, REST was best fit for most of the applications due to relatively simple client application and development pace wasn’t really where it is today. However, now a days, API’s became more complex due to below reasons
- Fast pace of development process: Frequent changes on the client side has impact 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, alternate to REST based architecture.
In this series of articles, we’ll discuss about
- 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.Sourced from graphql.org
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 data model.
Pros and Cons
Just like any architecture, GraphQL also comes with 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 request, 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. In case, you find the article as interesting then kindly like and share it.