I kept hearing about GraphQL and how great it is, but I knew nothing about it. I also kept hearing that the best way to learn something is to teach someone. So this is me teaching you, so that I can learn. Enjoy.
According the the GraphQL website:
GraphQL is a query language for your API, and a server-side runtime for executing queries by using a type system you define for your data. GraphQL isn't tied to any specific database or storage engine and is instead backed by your existing code and data. [1]
If you're like me, you just read the above text and thought to yourself… What did it all mean? What is GraphQL?
OK let's break it down and figure this out.
GraphQL is a query language for your API
a server-side runtime for executing queries by using a type system you define for your data.
Great, now that we have some of the technical mumbo jumbo defined, let’s try to put this into words that a noob[5] can understand.
GraphQL is a language that is used to communicate with an app and a computer that is hosting your data (database). It does not matter how or where you store your data or which language you use to code your project in. GraphQL sits in the middle, waiting for your data questions, which it passes on to your database, receives a response and passes it back to you.
Right!? That’s what I was thinking… If it just acts as a middle man passing data back and forth, why use it? Why not stick with just passing data back and forth the normal way? Let’s find out.
GraphQL allows you to write queries using an object structure rather than using a string. SQL and other typical database languages use string functions as a way to communicate with the database. View the example below.
A regular SQL query may look like this:
SELECT name, id, description FROM projects
A GraphQL query for the same request would look like this:
{
projects {
id
name
description
}
}
Hmmm, looks like JavaScript to me. Maybe that is why people are flocking to it? You don't need to learn to query in SQL, you just do it in a way that is familiar. But surely that cannot be the only reason that it is attracting so much BUZZ. Let's dig deeper!
In order to understand the benefits of GraphQL we must understand a little about REST endpoints.
REST
Endpoint
https://www.my_website.com/information/endpoint
Ok.... Why do I care about REST endpoints?
Typically when you are retrieving data or saving something using REST, you need to know which endpoint you will use.
In the example below, you will see a typical REST structure with multiple endpoints and the comparison using GraphQL with a single endpoint.
GET /posts/:id
GET /authors/:id
GET /posts/:id/comments
POST /posts/:id/comments
With GraphQL all of that is abstracted away, meaning you only need to deal with a single endpoint.
/graphql
Pretty neat! I think I am starting to see the draw to this GraphQL thing.
Here is what we have learned so far:
Due to the use of a single endpoint, GraphQL minimizes the amount of data that needs to be transferred over the network and thus majorly improves applications operating under these conditions.
In 2012 Facebook developed GraphQL and in 2015 they open sourced it. It was initially developed to adress increased mobile usage, low-powered devices and sloppy networks.
As I stated at the beginning of this article, GraphQL was an unknown topic to me. However, I now feel that I have a fairly good understanding of what it is and why it is gaining so much hype right now. My hope is that you have enjoyed reading this article and that you have learned something in the process. If you think that I should write more articles in the future, give this one an upvote and tell me in the comments.
Wow! You have decided that GraphQL is the way to go and now you are ready to start implementing it. Fantastic.
Head over to the official GraphQL website (https://graphql.org/learn/) and follow along with their tutorials. They are interactive and will give you the foundation that is need to implement GraphQL into your next project.
https://www.graph.cool/
https://engineering.fb.com/core-data/graphql-a-data-query-language/
[*]: Dictionary definition
[1]: "GraphQL | A query language for your API." https://graphql.org/. Accessed 8 Nov. 2019.
[2]: "Server-side - Wikipedia." https://en.wikipedia.org/wiki/Server-side. Accessed 8 Nov. 2019.
[3]: "Runtime system - Wikipedia." https://en.wikipedia.org/wiki/Runtime_system. Accessed 8 Nov. 2019.
[4]: "Type system - Wikipedia." https://en.wikipedia.org/wiki/Type_system. Accessed 8 Nov. 2019.
[5]: "Newbie - Wikipedia." https://en.wikipedia.org/wiki/Newbie. Accessed 8 Nov. 2019.