After I Joined Nimi, I have been exposed to lots of new technologies. GraphQL is one such powerful technology. Although the learning curve for this technology is steep, it figured out an easy hack to learn the basics of GraphQL quickly.
The immediate benefit of GraphQL is having a single endpoint, which motivates me to design APIs with fewer server requests.
One of the popular terms used among software developers nowadays is API. Basically, API stands for an application programming interface. As its name implies, it provides an interface for users to interact with data. I have been using the REST (Representational State Transfer) architecture pattern to develop API over the past few years. During development, I found these three major problems associated with REST APIs.
- REST is having multiple endpoints. Therefore, multiple servers call to handle
- API is too complex to understand
- There can be over and under fetching information
I could address these issues by using GraphQL. GraphQL is a query language that can be used in the API. There are libraries for implementing GraphQL for different programming languages to help API developers to design their APIs.
REST vs GraphQL
- In GraphQL we can use a single endpoint for each data type with a single argument for this endpoint which is a query. The following diagram will illustrate this idea.
We can see to get the followers for the particular post we have to send three HTTP get requests using REST API, while GraphQL can fulfill the same requirement using a single endpoint.
- We can get output in JSON format for both REST and GraphQL.
- Both technologies are stateless.
- In GraphQL we need to know what kind of data needs to be fetched (to build GraphQL query) but using REST API we don’t need such information and just a few parameters are needed.
- On the other hand, REST due to the above reason REST lead to pull data which may be unnecessary for the client and GraphQL only pull the data that is requested by the client.
- GraphQL is strongly typed. This feature will allow you to validate the query within the GraphQL type system before it is sent for execution. This improves the network performance.
Due to the above reasons, my suggestion is to use GraphQL whenever the user has an idea about the data that need to be fetched from the database. As a developer, I would recommend it as a very useful and powerful tool that is worth trying out.