Enterprise Java

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.

REST vs GraphQL - Evolution
Evolution

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 vs GraphQL
GraphQL

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.

REST vs GraphQL
REST

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

GraphQLREST
A query languageAn architectural style
Deployed over HTTP using a single endpoint tDeployed over a set of URLs – multiple endpoints
specific data supportresults in over fetching of data
based on the client-driven architecturebased on the server-driven architecture
no automatic caching mechanismhas automatic cache
No API versioninghas multiple API versions
graphQL has resolversREST has route handlers
JSON supportsSupports multiple data formats
GraphiQL is used for documentationSwagger, OpenAPI and API Blueprint are used for documentation
complex error handlinghas HTTP status error codes

4. Download the Source Code

Download
You can download the full source code of this example here: REST vs GraphQL: A Detailed Comparison

Bhagvan Kommadi

Bhagvan Kommadi is the Founder of Architect Corner & has around 20 years’ experience in the industry, ranging from large scale enterprise development to helping incubate software product start-ups. He has done Masters in Industrial Systems Engineering at Georgia Institute of Technology (1997) and Bachelors in Aerospace Engineering from Indian Institute of Technology, Madras (1993). He is member of IFX forum,Oracle JCP and participant in Java Community Process. He founded Quantica Computacao, the first quantum computing startup in India. Markets and Markets have positioned Quantica Computacao in ‘Emerging Companies’ section of Quantum Computing quadrants. Bhagvan has engineered and developed simulators and tools in the area of quantum technology using IBM Q, Microsoft Q# and Google QScript. He has reviewed the Manning book titled : "Machine Learning with TensorFlow”. He is also the author of Packt Publishing book - "Hands-On Data Structures and Algorithms with Go".He is member of IFX forum,Oracle JCP and participant in Java Community Process. He is member of the MIT Technology Review Global Panel.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button