Hitchhiker’s Guide To GraphQL
By: Salman Dabbakuti
Tags: API, Programming, Web Development, JavaScript, GraphQL

A photo by Sharon McCutcheon at Unsplash

Wait, What is GraphQL?

Is it a new query language? A new type of database? Some other JavaScript black magic? Nope, none of the above.

in a nutshell, GraphQL is a Query Language for APIs that describes how to ask for data and is generally used to load data from a server to a client. Firstly, it doesn’t have anything to do with databases. It is not an alternative to SQL. It is basically used to send/receive data between client and server using a minimum number of HTTP requests. In order to achieve this, you have to create relations between the entities programmatically. That’s how it is called as a query language.

“Ask for what you need, get exactly that.”

Who’s using GraphQL?

Is GraphQL named after Graph? Yes.

“A small, miniscule speck of dust in an endless ocean of massive stars, trillions upon trillions of planets and energy. That is Universe Graph. Every object in the universe is interconnected to each other. We are an intrinsic part of it. Believe it or not, we are Star’s Stuff.”

In computer science, the Graph represents structure used to model pairwise relations between various objects(or nodes).

Understanding, using and thinking in graphs makes us better programmers. At least that’s how we’re supposed to think. Graph-based APIs model the data in terms of nodes and edges (objects and relationships) and allows the client to interact with multiple nodes in a single request. With GraphQL, you model your business domain as a graph by defining a schema. within your schema, you define different types of nodes(data sources) and how they connect/relate to one another. That’s how it is called GraphQL.

Nodes and Relations in Graph — Source

Why GraphQL is a Better REST API?

When you need to build an API, your mind will likely jump to REST, the standard for API creation. However, this is changing with the increase in GraphQL popularity. let’s see why GraphQL became popular within no time and why it is better than REST.

Resources Handling:

The core idea of REST is that everything is a resource that’s identified by a URL. In its simplest form, you would retrieve a resource by putting through a GET request to the resource’s URL and get a JSON response.

Now let’s say later down the line you wanted to build a simple note maker application that consists of creating, reading, updating and deleting notes operations. So, in the scenario of REST API, you would end up writing different routes for these operations right? well, that makes sense to the application for now right? So, it does mean that number of resource endpoints in your API greatly increases with the number of operations your Application. Guess what happens in the GraphQL world? You write a query to ask for what you want(create, read, update, delete), and you get back exactly what you asked for with a single endpoint. It doesn’t matter how big is our application, GraphQL can handle all your application operations with a single endpoint. How cool is that?

REST vs GraphQL Architecture

2. Data Fetching:

REST APIs are usually a collection of endpoints, where each endpoint represents a resource. so when a client needs data from multiple resources, it needs to perform multiple requests to API to get the data needed for. This Problem in REST API is called under-fetching — 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. let’s have a look at a basic social media application example of retrieving user details and his first 100 friends. with the REST API, you need to do multiple round trips on two endpoints to achieve this. let’s see how we can use GraphQL to solve this under-fetching problem.

GraphQL query to solve an under-fetching issue in REST API

with the single query in GraphQL, you will get whatever you want. nothing more or nothing less. nevertheless, you can also filter how many records you want which is yet complex to achieve with REST API.

Another problem we face with the REST API is over-fetching. — fetching excessive data that we don’t need. This REST implementation represents a huge waste bandwidth per request. for example, let’s have a look at a basic social media application that displays registered users on the home page of application just like facebook’s Add friends page. with REST API, when we call /users/allUsers endpoint, it will return a bunch of user records that are registered on the platform with the following fields of each user as an example.

we might not use all these fields on the home page of the application to display users and also an excessive list of users (more than we required to display on the home page i.e. 20 users). So it is the waste bandwidth and network resources. With the GraphQL, we can fetch the fields that we need and list of records we’ve asked for with a simple query.