Why GraphQL?

in graphql •  5 years ago  (edited)

WhyGraphQL.png

Why GraphQL?

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.

What is GraphQL?

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

    • Query language [*]
      • a language for the specification of procedures for the retrieval (and sometimes also modification) of information from a database.
    • API [*]
      • a set of functions and procedures allowing the creation of applications that access the features or data of an operating system, application, or other service.
  • a server-side runtime for executing queries by using a type system you define for your data.

    • Server-side [2]
      • Server-side refers to operations that are performed by the server in a client–​server relationship in a computer network.
      • Runtime [3]
        • In computer science, run time, runtime or execution time is the time during which a program is running (executing)
      • Queries
        • A question
      • Type system [4]
        • In programming languages, a type system is a set of rules that assigns a property called type to the various constructs of a computer program, such as variables, expressions, functions or modules.

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.

Why use GraphQL?

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!

REST endpoints

In order to understand the benefits of GraphQL we must understand a little about REST endpoints.

  • REST

    • Representational State Transfer - A set of rules that a developer follows when they create their API. One of these rules states that you should be able to get a piece of data (called a resource) when you link to a specific URL.
  • Endpoint

    • Aka 'route', is the url you request
    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

REST_API.png

With GraphQL all of that is abstracted away, meaning you only need to deal with a single endpoint.

/graphql

GraphQL_API.png


Pretty neat! I think I am starting to see the draw to this GraphQL thing.

Here is what we have learned so far:

  • GraphQL is a new API standard that enables declarative data fetching from a single endpoint.

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.

Who created GraphQL?

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.

Conclusion

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.

How to use GraphQL?

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.

Resources

https://www.graph.cool/
https://engineering.fb.com/core-data/graphql-a-data-query-language/

Notes

[*]: 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.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Congratulations @digitalm0nkey! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

SteemFest Meet The Stemians Contest - The mysterious rule revealed
SteemFest⁴ - Meet the Steemians Contest
Vote for @Steemitboard as a witness to get one more award and increased upvotes!

@digitalm0nkey, thank you for supporting @steemitboard as a witness.

Here is a small present to show our gratitude
Click on the badge to view your Board of Honor.

Once again, thanks for your support!

Do not miss the last post from @steemitboard:

SteemFest Meet The Stemians Contest - The mysterious rule revealed
SteemFest⁴ - Meet the Steemians Contest