Introduction to GraphQL

Yashod Perera
3 min readApr 17, 2020

--

is it the next level of RESTful API ?

What are the drawbacks of RESTful APIs?

Let’s take a public RESTful API which gives data about anything. Think about this “Will it gives you the exact information you want?” or “Do you get more or less information?”.

Let’s take an example where there are a RESTful API which gives a selling item details and seller information via two APIs which are given in blue colour. There are two scenarios to consider.

  1. Mike needs only the item id, item name and the availability. He can take what he wants from one API call but he get unnecessary data such as no of items available and seller id.
  2. Peter needs item id, item name, no of items available, seller’s name and seller’s phone and for that he has to call two APIs.

Then we get a major drawback of RESTful APIs which is it has fixed structure of outputting information. In some cases we have to call more than One API to get data and some cases we get additional data or less data.

GraphQL solves it

In GraphQL it solves the problem that RESTful API gives the fixed structure by giving a dynamic structure.

WOW great! Then we can use RESTful APIs and use GraphQL to retrieve data. NO we can’t

We have to implement GraphQL APIs to retrieve data dynamically. It is like a graph as following example.

GraphQL is Graph Query Language

Graph structure

In GraphQL it has a graph structure as given in the above and it has a predefined structure as follows. Then user can ask for any data which is in the predefined structure and it only sends what user wants.

Predefined structure for the above graph

Let’s take the following example and further discuss what it can do and can’t.

In above example Peter asks for data which is in the GraphQL schema which will return data as he expected. But Mike asks for a data which is not in the GraphQL schema then it will give an error.

Even GraphQL support dynamic structure it cannot give data which is not defined.

Operations which GraphQL supports

In RESTFul API there are several methods such as GET, POST, PUT, DELETE can be used to do different type of operations such as retrieve data, update data, delete data etc. In GraphQL there are three types of operations.

  • Query — This is used to retrieve data
  • Mutation — This is used to update, post and delete data
  • Subscription — This is used to get information if an event occurred. As an example if we subscribe to check if user creates it will consecutively gives data when user is created.

You can create GraphQL APIs using most of the languages including Java, JavaScript, Ruby, Python, Go etc.

Let’s meet with another GraphQL tutorial.

If you have found this helpful please hit that 👏 and share it on social media :)

--

--

Yashod Perera
Yashod Perera

Written by Yashod Perera

Technical Writer | Tech Enthusiast | Open source contributor

No responses yet