What is GraphQL? Need for it?
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.
- 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.