Search Tutorials


What is GraphQL? Need for it? | JavaInUse
     



   

What is GraphQL? Need for it?

In this tutorial we will have a look at what is GraphQL and the need for it. In the next tutorial we will be implementing Spring Boot + GraphQL Hello World Example.

Need for GraphQL

For majority of spring boot projects, we make use of REST API's to expose web services which are then consumed by the client. Similar to REST, GraphQL is nothing more than a specification that describes how client and servers interact.
Before we begin to look at what is GraphQL let us first understand what is the need for a new client server communication specification

REST is a popular specification used for client-server communication. Representational state transfer is a software architectural style that defines a set of constraints to be used for creating Web services.



Consider the scenario where we have exposed three REST GET API's as follows -
  • /getEmployees - This API will return the list of all the employees. So the employee details like employee name, employee id, address, age, employee office location etc will be returned to the client. So suppose there are 500 employees. Then this REST API will return employee data for all 500 employees which has all the employee fields.
  • /getEmployee/{empLocation} - This API will return list of all the employees based on the employee office location passed as a query parameter. So the details of the employees who work at suppose london location can retrieved using this API.
  • /getEmployeeBankDetails/{empId} - This API will return the bank details corresponding to the employee id passed as a query parameter.
With the above REST API's consider the following scenarios -
  • Scenario 1 - Now suppose the client application wants to retrieve the only the names and employee id's of all the employees. We have exposed the /getEmployees API. However this will return all the fields of the Employee and not just the required name and employee id field. When using REST if the user wants only the mentioned two fields, server will then need to expose a new API which return only these fields. However this is not at all a good solution. If in future some other fields are required then again a new API will need to be exposed. This issue is known as over fetching of data. As the client will now need to get all the employee fields even if they do not need it. This results in more network bandwidth and also results in more client overhead to filter out the given data. Using GraphQL this issue will not occur as the clients can ask for what they need accordingly in the request.
  • Scenario 2 - Suppose the client wants to retrieve the bank details of all the employees working at london location. For this we have the /getEmployeeBankDetails/{empId} API, which retrieved the bank details based on the employee id. However we will need the employee id's of all the employee working at london location. So using the /getEmployee/{empLocation} we will first get all the list of employees working at london location(this again will be data over fetching as we need only employee id but will get all the fields). Then we will be iterating over this list of employees and getting the bank details by calling the  /getEmployeeBankDetails/{empId} using the employee id's. So multiple round trips are need here to get the required data. And more bandwidth, data and time is being consumed. With GraphQL, clients can always fetch all the data required by a view with a single round-trip to the server.


What is GraphQL

In 2012 Facebook started the GraphQL project to overcome data fetching issues in their native mobile platform and in 2015 facebook open sourced GraphQL.

GraphQL is an open-source data query and manipulation language for APIs, and a runtime for fulfilling queries with existing data.

GraphQL came into existence due to need for better flexibility and efficiency in client server communication. GraphQl is Language Agnostic. You can use it in conjunction with any programming language, database, web framework, or UI library. GraphQL server libraries exist in a variety of different languages including C#, Clojure, Elixir, Erlang, Go, Groovy, Java, JavaScript, .NET, PHP, Python, Scala, and Ruby. GraphQL is a query language for our API.

The Simple GraphQL Request is as follows -

It consists of the 2 part
  • Operation
  • Fields to be returned in response
Sample GraphQL query
{
allEmployees(){
	employee{
		empId
		name
	}
}
We get the output as -
{
   "data":{
      "allEmployees":{
         "employee":[
            {
               "empId":"emp01",
               "name":"tom"
            },
            {
               "empId":"emp02",
               "name":"chan"
            },
            {
               "empId":"emp03",
               "name":"harish"
            }
         ]
      }
   }
}
GraphQL is a strictly schema based interface. So in the schema we define both the possible operations and the response details. So the user knows for sure which operations can be called and also the response types are also fully known to the user.
The GraphQL schema looks as follows-
schema{
query:{allEmployees{[employee]}}
}

type employee{
 empName: String!
 empId: String!
}
GraphQL queries can be classified as one of the following-
  • GraphQL queries for retrieval of data
    {
    allEmployees(last: 3){
    	employee{
    		empId
    		name
    	}
    }
    
  • GraphQL mutations are request that causes some kind of change in data state. So it is used to alter the data i.e create, update or delete data
    {addEmployee(empName: "tom" empId:"empId01"){}}
    
  • GraphQL subscriptions as the name suggests to subscribe to GraphQL servers on specific conditions. When the condition is satisfied a message is generated and sent to the client.