Demystifying GraphQL - A Comprehensive Guide

Published on
1239 words
7 mins read
--- views
Authors

If you are a developer, At some point you have heard the word GraphQL. At the time you may have lots of questions running in your mind like what is it? What is the different between REST and GraphQL? Why GraphQL? Which language support GraphQL and so on. You can get the answers for your questions here.

What is GraphQL

GraphQL is an acronym of “Graph Query Language”. It is a query language and server-side runtime for API’s. (Just as name suggests 😉).

We know that, SQL (Structured Query Language) is used to querying on Relational Databases. Similarly, GraphQL is a mechanism that allows you to retriving only the fields that you need. Once GraphQL is implemented, no need to create or modify everything in your backend. I undersand it bit confused. It is confusing only in the begginning and not if you complete this article. Knowing history is important. So Before we dive in, let’s understand, Five Ws of GraphQL.

Agárrate fuerte



Five Ws of GraphQL

We have already seen one of Five Ws, Which is nothing but What is GraphQL, Let see another Four Ws.

GraphQL was first built and used by facebook in 2012 internally and was released externally in 2015. Facebook identified themselves with an issue. Facebook started to suffer poor performances and frequently crashed as the become more complex. Facebook solved this issue by developing a technology called GraphQL. It is designed to make APIs fast, flexible and developer friendly. It cab be even deployed within an IDE known as GraphiQL.

How it is different from REST API?

  • In REST API, we usually use HTTP Methods like GET, POST, PUT, PATCH, DELETE whereas in GraphQl we use Query, Mutation and Subscription.
  • With Traditional REST API, we have to create seperate end point based on the request. When your app gets complicated, It is hard to scale because of hundrads of hundrads routers or more. GraphQL uses a single endpoint where everything is handled. Scaling an application is simple when compared to REST API,
  • GraphQL allows us to fetch the data that is required. It allows us to specify which data has to be return whereas in REST API, It return all the data from the backend.
  • GraphQL uses Type System for describing our backend. (Typescript hardcore fans will get this.)
  • GraphQL is self documented. GraphiQL will automatically document all your queries and mutataions. Also GraphiQL allows developer and tester to debug and test without using the apollo client.
  • GraphQL is way faster than some of the popular traditional API's.

Apart from this, GraphQL have other advantages as well as disadvantages.

Advantages

  • Strongly typed : Each layer of a GraphQL query corresponds to a particular type, and each type describes a set of available fields.
  • Defines a data shape : When GraphQL queries are requested to graphql server, it returns the response simple and required shape. So, it facilitates you to write a specific query according to your requirement.

Disadvantages

  • GraphQL Rate Limiting : In Rest API, we can limit the request whereas in GraphQL it is not possible.
  • GraphQL Caching : Implementing a cache is more complecated than REST API.


Example Scenario

Let say you went for a outing and decided to do several things like gave a college notes to your friend, purchasing shoes at mall, went to bank to withdraw, etc.

In REST model, you have to travel at different places to complete them. On the other hand, In GraphQL, you can tell your friend to assist to do everything for you. You’ll just travel to your friend house only.

Hope you get the idea of GraphQL. Cool! So how does it work? thats what we're about see.

Schemas, Resolvers and other common GraphQL terms

Schemas

Central to every GraphQL server is its schema. GraphQL schema describe the pattern or shape of the graph. It gives idea about the data which operations can perform on the server. It is written in easy and readable SDL or Schema Definition Language. It’s like a How the data can be requested from the server. Here’s a sample SDL

type Computer {
  name: String!
  modal: String!
    memory: String!
    processor: String!
}
type Human {
  name: String!
  age: Int!
}

If you look at the above schema, it clearly state that Computer type contains name,modal, memory,processor of string type and ! represent NOT NULL type and Human type contains name and age of string and not null type. Apart from int and string other data type like float,list, etc also supported in GraphQL. So this schema allows us to write a query and mutation in GraphQL.

Resolvers

Resolver is a function that's responsible for populating the data for a single field in your schema. It can populate that data in any way you define, such as by fetching data from a back-end database or a third-party API.

Here’s a example :

const resolvers = {
  Query: {
    computers() {
      return computer;
    },
    humans() {
      return human;
    }
  }
};

Query

Same as SQL, GraphQL Query is the way to retrive the data from the backend server. It is same as using GET Method in REST.

Let’s say you want to get the customer name and age from the backend stored in the database, you would have use the query like given below .

query{
  customer {
    name
    age
  }
}

Let’s say you want to get the specific customer information from the database, you would have use the query like this.

query{
  user(id: "23") {
    name
    age
  }
}

Here we have another main advantage of GraphQL is it support quering the nested information like we use JOIN in SQL. Now let’s assume you want to query customer information along with the order details, query structure look like this..

query{
  customer {
    name
    age
    orders {
    	id
    	amount
    }
  }
}

Mutation

Mutation is the way to add, update or delete the data. It is same as using POST, PUT, and DELETE Method in REST.

Using mutation we can perform create, modify or delete in the backend. Let say you want to create customer, mutation structure look like given below.

mutation{
  addCustomer(name: "Geeky GandS", age: 3){
    name 
    age
  }
}

Subscription

Subscription is a way to retrieve data just like query but the difference is that subscription continuously listen for updated values and send updated values to client that choose to listen to real-time message from the server. for that it establishes long lasting connection with server.

Basic subscription looks like this:

subscription {
  users {
    id
    name
    age
  }
}

Language Support


GraphQL Supported Languages


Conclusion

GraphQL makes a lot simpler to fetch the data from the backend when compared to REST, which is the main reason for GraphQL fast. But GraphQL does not support HTTP status code mechanism and error magement and monitoring. Both GraphQL and REST has its own advantages and disadvantages. REST is old traditional way and used by everyone and on the other hand GraphQL is also becoming popular in recent times. Based on your application usage you can choose whatever you want.

Bella Ciao