DEV Community

Cover image for What is GraphQL and why use it?
Harsh Singh
Harsh Singh

Posted on

What is GraphQL and why use it?

When I was starting out, I had heard of GraphQL, but didn't really know what it was. Hearing of it at first, I thought it was something related to SQL, so in this post I'll try my best to explain what GraphQL is, and why it was made.

Basic terms

API - An interface where people can interact with Data
REST - Representation State Transfer. Way of saying that a server responds to create, read, update, and delete requests in a standard way.
Query- A query is a request for data or information from a database table or combination of tables.

What is GraphQL?

When you search for the definition of GraphQL, you get the definition that "GraphQL is an open-source data query and manipulation language for APIs, and a runtime for fulfilling queries with existing data" - Which leaves many of us confused, so, let's break it and really explain it.

GraphQL is used so that the data which you fetch is just as much data as you need to save resources.

The main problem GraphQL aims to solve is over-fetching, and under fetching. When you get the data, sometimes it is more than what you need, and wastes resources. Sometimes, it's the exact opposite, and you get such little data that you need to make several queries.

It is nothing much of a worry. In fact, as a smaller developer myself, like an overwhelming amount of people who're reading this post, we don't really need to use GraphQL in our applications for the most part.

A brief background

GraphQL was developed internally by Facebook in 2012, and released to the public in 2015. In 2018, GraphQL was moved from Facebook to the newly established GraphQL Foundation hosted by the Linux Foundation.

Why use GraphQL

I will be honest, if you're just building a small application, with a few users or so a month, there isn't a need for GraphQL. I personally often times don't want to go an extra mile to add GraphQL, and am ok with overfetching and/or making several queries.

Nonetheless, say, if you're working at a large company, like, say Facebook which developed this app. Tens of millions of users make these queries every second, which causes the cost of resources to rise (for no particular reason, as you are fetching more data than you ultimately need). Say, we want to fetch information about a user. The only things you need is his name, his location, his bio, etc. Of course, here we can fetch the queries, but imagine the data which you will be fetching. It will surely be much more than what you need. If I just want to know the name, location, and bio of this user, don't need the data for his email address, work, his social accounts, his friends list, etc.

For a large company like Facebook, they can obviously cut down a large portion of their server costs by using GraphQL, though for most of us, it is really not that big of a deal.

I'm not saying that you shouldn't implement another resource like GraphQL, and if you do decide to go ahead with it, it is always going to be better. In all honesty, if you implement GraphQL you will probably not see much of a difference between that and the REST API, unless you're running something large, like Facebook. If you are getting millions or perhaps billions of queries each day then you should definitely use GraphQL to save you a good amount of resources.


Example of a query in GraphQL





Knowing GraphQL is a good thing, and I strongly encourage you to build out some cool projects with it, though this does not mean that the REST API is getting cancelled. Most applications still use it, and it will work just fine for most applications me and you build.

Important links

YouTube Tutorial (JavaScript)
Learn more about REST API

Discussion (0)