Introduction to Graph QL


Query what you need! not everything, a better REST

GraphQL is a query language for APIs and a run time for fulfilling those queries with your existing data. GraphQL is the better REST.



No-more Over fetching and Under fetching…


GraphQL (Courtesy:

A scenario

In your app, you need to display logged in user name and full name on the right top corner. But you want only user name and full name.




Now you can achieve this in the following ways.



  1. You will design your Employee object to have username, first name, last name, address etc.
  2. your server will return Employee json response with all details of the user object
  3. In your client side of your app, You fetch User by calling /getLoggedUser you will use username and full name(first name, last name) of Employee object and display in your page.

Over fetching

A client downloads more information than is actually required in the app. Imagine the above example screen that needs to display full name and user name of a logged in employee, only full name(first name + last name) and username.

In a REST API, this app would usually hit the /loggedInUser/endpoint and receive a JSON with employee data. This response however might contain more info about the employee that are returned, e.g. their birthday or address – information that is useless for the client because it only needs to display the employee’s full name and username.




Over fetching

You only need first name , last name, user name but your rest returns other information which is not useful for the current scenario.



Under fetching, the n+1 problem

The n+1 requests problem. Under-fetching generally means that a specific endpoint doesn’t provide enough of the required information. The client will have to make additional requests to fetch everything it needs.

This can escalate to a situation where a client needs to first download a list of elements, but then needs to make one additional request per element to fetch the required data.




Under fetching , the n+1 problem

Above example, you will need to make another REST call to fetch the employee address. This is called n+1 problem.



GraphQL: Ask for what you need, get exactly that


GraphQL (Courtesy:

Send a GraphQL query to your API and get exactly what you need, nothing more and nothing less.






Get many resources in a single request


GraphQL (Courtesy:

You can retrieve data from multiple resources using the references. while typical REST you need to make multiple calls.

Apps using GraphQL can be quick even on slow mobile network connections.






Describe what’s possible with a type system

GraphQL APIs are organized in terms of types and fields, not endpoints.



type Employee{
firstname: String
lastname: String
username: String
dob: Date
age: Number
department: String
address: [Address]

GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.







How do you play around with the possible queries and sub sets of data can be returned and queried.?

We have powerful tools to achieve that in graphql, it is called “GraphiQL




GraphQL (Courtesy:

Evolve your API freely! no versions!

  • Add new fields and types to your GraphQL API without impacting existing queries.
  • Deprecate aging fields and hide it from API.

Single evolving version of API

  • New features
  • cleaner code
  • More maintainable server code


GraphQL is a very exciting new technology that can potentially revolutionize the way that Web APIs are developed.

Before choosing this technology, rethink, brainstorm to check whether you need this in your application. While implementing 2 or more combination of REST can solve your problem, then you can simply live with REST.

When you are going to deal with different combination of data sets from one big Resource or multiple resources, then you can consider using GraphQL. Slow network connections and low bandwidth connectivity GraphQL can perform better.





You Might Also Like
Leave a Reply