What is GraphQL?

John Ex
3 min readJun 6, 2021

What is GraphQL and should you be using it? What does it replace or enhance? Is it actually better? All this is what I will be introducing to you and explaining. Many of the comparisons are to REST, so it is necessary to know that before reading this.

Introduction

GraphQL is developed by Facebook. They initially developed and used it for their own systems and eventually released it to the public for anyone to use.

It is a query language for your API and could replace the classic REST API that you might be used to. A common misconception is that it is only for React, however, it can be used with any Frontend language.

A core difference is that it is organized into types instead of endpoints, which means it only has 1 endpoint. It has a nodes and edges system that helps you better use your data, as well as show their relationships. The nodes are what represent your objects and those are connected with edges to show the relationships between those nodes( or objects).

Pros

A big pro is that you can use GraphQL to get ONLY the data that you need. You would not need to waste time and resources getting all the extra useless data. An example of this is if you have a huge database full of accounts that includes a username, password, email, phone number, and address but you only need to get an email, instead of the whole object. You can grab only the email from only the account you need with GraphQL, which may not seem like a big deal, but what if you need the emails of all the users? That could potentially save a huge amount of time depending on the amount of data.

Another feature that solves a surprisingly common issue, is the ability to make a single request that gets multiple resources! Gone are the days in which you need to make a request for /accounts and /products separately. With GraphQL you can just ask for both at once. What is also great about this is that you then get the data from both of the tables (or as many tables as you asked for) in a single object, which can simplify the process, and make scope issues less frequent.

The basic Operation Types are Query and Mutation. What do those mean? It is how you get or change data. However, an advantage of GraphQL is a third type, which is Subscription! This allows us to get updates when data is changed, which is not a feature of REST APIs.

Cons

One thing that may be a reason to focus on REST APIs over GraphQL is that REST APIs are way more common and if you’re in the professional world, over a personal project, it is a lot more likely that you will be using a REST API instead.

Another thing is that you have to learn a new query language, whereas using a REST API is generally a bit simpler. You can just ask for the data and use the object with your frontend language, whereas with GraphQL you actually need to learn its query systems. And while on the topic of accessibility, REST API doesn’t require extra packages like GraphQL does.

If you are just starting out, GraphQL is a bit more error-prone but also if you aren’t using it correctly or without the right tools, it can become messy really quick. In addition to that, GraphQL APIs are a bit harder to make, however, there are many tools and packages to make this a lot simpler.

Conclusion

The decision is up to you, and this blog outlines the basic differences between GraphQL and REST, however, the way I see it is that GraphQL is better but has a greater barrier to entry. Whether that learning curve is worth it or not, is up to you, and may not be worth it if you need to constantly fetch a lot of data, don’t care about too much optimization, or even because your job doesn’t use it. Happy coding!!!

--

--