REST vs GraphQL: A Detailed Comparison
In this article, we will take a look at the most important differences between REST vs GraphQL.
1. Introduction
REST and GraphQL are web services technologies that are popular. This article talks about the differences between REST and GraphQL. REST APIs are simpler compared to SOAP. REST was a defacto standard till 2011. Facebook created GraphQL for mobile applications. GraphQL was open-sourced in 2015.
2.REST vs GraphQL
2.1 Prerequisites
Java 8 is required on the Linux, windows, or Mac operating systems. You can use the Spring REST framework for REST API. GraphQL can be used for GraphQL API.
2.2 Download
You can download Java 8 from the Oracle web site. Spring framework’s latest releases are available from the spring website. GraphQL can be accessed from this link.
2.3 History of GraphQL and REST
GraphQL has a graph that represents data schema. Graph nodes and edges are created based on the schema. A resolver helps in accessing the server’s data. GraphQL has a request/response mechanism to handle a nested set of objects in the schema. JSON data structure has the key-value map of the field’s name and value. GraphQL is a query language introduced by Facebook in 2011.
REST stands for Representational State Transfer. It was termed in 2000 by Roy Fielding in his Ph.D. Thesis. It is an architectural style. REST is based on HTTP protocol for information exchange. It supports XML and JSON. REST is adopted extensively in the API services space. It has an 83 % share in the software APIs market.
2.4 In which projects do we need GraphQL?
GraphQL helps in improving performance for your queries and service APIs. Developers need to put more effort to learn compared to REST. It is defined as the Query language which was created to solve the problems with REST. GraphQL helps in presenting structured data for client’s requests. Data is optimized for a request and presented as a response. No transformation of data is required.
For example, graphQL endpoint will be /graphql?query={employee{name}}
The query can be as shown below:
GraphQL Query
{ employees { id firstName } }
The response is as shown as below:
GraphQL Response
{ "data": { "employees": [ { "id": "3001", "firstName": "John" }, { "id": "4002", "firstName": "Thomas" } ] } }
2.5 In which projects do we need REST?
REST is a popular industry standard for software APIs. The endpoints architecture style for a resource using HTTP methods helps in quick service API creation. Developers can learn this technology faster and adopt. This is useful where data is created as in a resource view.
REST supports the following HTTP methods :
- GET (ex: /employees/id)
- POST (ex: /employees/id, /employee)
- DELETE (ex: /employees/id)
- PUT (ex: /employees/id)
The response for these methods can be xml or json as shown in the example below:
XML Response
<employee> <id>1</id> <name>John Smith</name> <department>Finance</department> </employee>
Json response will be as below:
XML Response
{ "id":1, "name":"John Smith", "department":"Finance" }
2.6 Major differences
REST APIs are simple to use and implement as a single endpoint performs one task. GraphQL has a query language to returns more than one object in a data structure. GraphQL is powerful as the response has the required information for a request. Developers require more time in learning GraphQL compared to REST.
3. Conclusion
Overall, REST has great benefits over GraphQL The comparison table below captures the differences between GraphQL and REST
Comparison Table
GraphQL | REST |
A query language | An architectural style |
Deployed over HTTP using a single endpoint t | Deployed over a set of URLs – multiple endpoints |
specific data support | results in over fetching of data |
based on the client-driven architecture | based on the server-driven architecture |
no automatic caching mechanism | has automatic cache |
No API versioning | has multiple API versions |
graphQL has resolvers | REST has route handlers |
JSON supports | Supports multiple data formats |
GraphiQL is used for documentation | Swagger, OpenAPI and API Blueprint are used for documentation |
complex error handling | has HTTP status error codes |
4. Download the Source Code
You can download the full source code of this example here: REST vs GraphQL: A Detailed Comparison