GraphQL is an alternative to REST?

Krushna Dash
3 min readMay 25, 2021

What is GraphQL ?
It is a query language for your API

Why GraphQL ?
GraphQL lets you ask for what you want in a single query, saving bandwidth and reducing waterfall requests. It also enables clients to request their own unique data specifications.
Unlike REST, where the server decides the response structure, In GraphQL the client will decide that.GraphQL also shifted its focus, from thinking of data in terms of resource URLs, into a graph of objects and the models used in their apps.

Let take an example let say

let say we have a simple app with Books and their Author as resources like below

Book {
id:
name:
pageCount:
author:
}
Author {
id: ID!
firstName: String!
lastName: String
books:
}

Now let say we need to show 10 recent books with their author details like below

"recentBooks": [
{
"name": "Book 0:0",
"author": {
"firstName": "Author 0",
"lastName": "Author Last0"
}
},
.......
]

With traditional REST we need to make at least 1 (to fetch recent 10 books)+ 10(for each book to get their Author) calls to display the same and with GraphQL the same can be achieved by one call.

{
recentBooks(count: 10){
name
author {
firstName
lastName
}
}
}

So GraphQL not only Saves the number of requests but also allows the client to decide the data structure and hence save the bandwidth also.

Who and Why GraphQL is developed?

The developers at Facebook were finding it difficult to implement the News Feed section for their mobile applications. During their development phase, they experienced a lot of bugs because there was no convention regarding the exchange of data between the front-ends and the back-ends. The shape of the data assumed by the front-end was different than the one being sent by the back-end APIs. There was a need to build something better!

Facebook solved this problem by building GraphQL.
More details can be found from their post https://engineering.fb.com/2015/09/14/core-data/graphql-a-data-query-language/

A working example of GraphQL with SpringBoot

As GraphQL specification made open source there are multiple implementations written in different languages. One of them is Java https://github.com/graphql-java/graphql-java

With the help of SpringBoot, it makes available GraphQL API over HTTP, We need to add below maven dependence and some code to any spring boot web project to process the GraphQL schema and make it available over HTTP.

         <dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-spring-boot-starter</artifactId>
<version>5.0.2</version>
</dependency>

<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java-tools</artifactId>
<version>5.2.4</version>
</dependency>

Spring boot GraphQL starter project automatically loads **/*.graphqls files from the classpath to create the GraphQL schema.

According to GraphQL schema, we should have the data bean with the same field and we can use the GraphQLResolver and we can provide the method which will be used to get the data for that field.
For each GraphQL query, we need to have the method with the same signature, which will be used to fetch data for the query.
Similarly, we need to have a mutation method with the same signature to update the data.

Here is a complete example code on GraphQL with Spring boot https://github.com/krushnaDash/spring-graphql

Conclusion :

GrpahQL never defines how and where the data should be access the data source of a GraphQL may be a database or maybe some API endpoint. So from my point of view
GraphQL is not a replacement for REST, rather it complements well with REST and can be used for specific use cases

--

--