Traditional REST API vs GraphQL: What you need to know
Traditional Rest API — Introduction
Traditional REST API provides an easier way for interaction between the client and the server via HTTP protocols with predefined data formats (json, xml, text, html). This is client-server architecture emerged in 2000 the user interface will be away from the data warehouse to make the user interface support across various platforms.
GraphQL — Introduction
GraphQL was developed by Facebook in 2012 and it became open-sourced in 2015. Unlike REST, GraphQL is a query language and the client sends requests like a query format with required data, and GraphQL returns responses in json format. GraphQL schema has Query and Mutation, Query is to fetch the data like select query in SQL and Mutation is to modify data like Insert/Update/Delete query in SQL.
Click here to know GraphQL supported languages and libraries.
1.Overfetching & Underfetching
The general concern in API is over-fetching and under-fetching problems. What does it mean? For example, let’s have a database called user and it has 25 columns (id, name, email, age, phone, etc…). To display user lists with name and email, in API response it is sufficient to return name and email list alone, rather than sending all 25 columns. In Rest API, the client doesn’t have provision to select name and email alone, API sends all 25 columns. This is called over-fetching.
Here in GraphQL, the client itself can select whichever data is needed for the screen. The client can select name and email alone in GraphQL request and pass it to API and API will return response only the data which is explicitly requested. To request additional columns like phone, the client can manage and there is no code change in API. This solves over fetching. In case of under fetching, in a single API if required data is not sufficient then another API is required to fulfill the requirement. Multiple APIs may be required. Like overfetching, GraphQL plays a crucial role in solving under fetching with the minimum request.
2. Number of requests
In the REST approach, the number of requests between client and server is more. For example, in a web application to show user details, along with their posts and comments data, one API is needed to return the user list and one more API to return posts, and one more to return comments. So here 3 requests need to be sent to fetch all data.
But in GraphQL, with a single request, it is possible to fetch all 3 data (users, blogs, and comments). So the number of requests can be reduced.
3. Routing
In the REST approach, each and every API has a unique endpoint. So in an application to use 20 APIs (for example), there will be 20 different endpoints. In the above point 2 (Number of requests), in the REST approach there will be 3 APIs. Sometimes it is difficult to maintain all the endpoints. But in GraphQL throughout the application, there will be only one endpoint. Now the question is then how API is different from each other like REST?
In GraphQL while passing a request, there will be a unique name for each request query. But the API endpoint will be only one. With that unique name in the query, API will find the corresponding method the same way in routing concepts.
4. Community Support
REST API has a larger community than GraphQL. The reason is REST API is used everywhere while GraphQL is used less. Many people have contributed to the REST API community and the GraphQL community is growing faster in recent years.
5. Performance
The most important factor in all applications is performance. As GraphQL resolves the fetching problem and reduces the number of requests as mentioned above in points (1,2) the performance will be faster when compared to the REST API. But at the same time in the case of requests with multiple levels (nested queries) performance will be slow in GraphQL. For those cases, REST will be a better choice.
Which is best?
Conclusively, which is best: GraphQL or REST API? It depends. Both have pros and cons. One should select REST API or GraphQL predicated on the requisites of our project and API complexities. The suggested way is to go with GraphQL, but in the case of a nested level of data fetch, the REST approach is best. In recent months many companies started building GraphQL APIs considering its advantages. Click here to see the top companies using GraphQL.
Author: Vignesh Rajasekaran