A Quick Guide to GraphQL

Reading Time: 3 minutes

Introduction

You must have heard about GraphQL but maybe not be aware of what it is and why is it used. In this blog, we will be understanding what is graphQL and its need. So let’s get started.

What is GraphQL?

GraphQL is a query language that is used to load data from server to client that is you are fetching data from the API into your application. It is now being used by multiple leading organizations like Facebook, Shopify, Github,  Pinterest, Paypal, Coursera, etc. GraphQL is neither frontend nor backend. Rather it is a query language to exchange data between the two to exchange information. Unlike RestfulAPI, it allows the user to restrict fields that should be fetched. Thus reducing traffic over the network and response time.

Origin

GraphQL was developed by Facebook. So the case is that sometimes you might be fetching a lot of data that is not required or the data that is fetched is less than what is required. This case is not much of a big worry in a small application but an application like Facebook where billions of requests are being made every second. Then it can be expensive on the cost of the server.

So, GraphnQL allows an effective way to fetch data where only the relevant and necessary data is fetched.

What is GraphQL Query?

A query is used to fetch data where the server will respond with data in a specified format corresponding to the query. The popular response format is JSON. Let us see an example:

//query without name
{
    hello
}

//query with name myQuery
query myQuery{
   hello
}

Here query is the operation type and myQuery is the operation Name. The operation type is either query, mutation(to post values), or subscription(automatically receiving real-time data over time).

The response will be somewhat like this:

{
    “data”:{
        “Hello”:”hello world”
     }
}

What is GraphQL mutation?

A mutation is used to modify the data like insert, update or delete operation.

What is a GraphQL subscription?

Subscription is a special feature that allows the server to send data when a specific event occurs.

We will learn more about mutation and subscriptions in further blogs.

Advantages

  • It is faster than other APIs.
  • It fetches relevant data only in a single request. The data return by GraphQL while executing our query is very precise and accurate, and it depends on what we have requested for.
  • It is a strongly typed query language.All the types like string, int, boolean, float are defined in the schema.
  • It is not mandatory to update the GraphQL version to implement it. As we know, it was developed by Facebook and they are still using the same version and it is working very smoothly.
  • Apps using GraphQL can be fast even on slow mobile network connections.

Disadvantages

  • Every query that is executed and launched in GraphQL, whether the query is successful or not, will always return an HTTP status code of 200.
  •  It does not have built-in caching support.

Conclusion

GraphQL has proven to be an effective way to load only relevant and necessary data and its ecosystem is growing rapidly. It works best for the scenarios where bandwidth usage matters. To learn more about it refer to the official documentation here.

Also, Thank you for sticking to the end. If you like the blog, please don’t forget to give a thumbs up and share it. Also, you can comment and tell me how I can improve my content. Feel free to share your thoughts about this blog in the comments.

Written by 

Kiran Jeet Kaur is working as a Software Consultant in Knoldus. Her practice area is Front End. She is always open to learn new things. Her hobbies include watching movies and listening to music.

1 thought on “A Quick Guide to GraphQL3 min read

Comments are closed.

%d bloggers like this: