A Beginner’s Dive into GraphQL — Part 1

Hima Chitalia
Coffee and Codes
Published in
3 min readOct 23, 2017
GraphQL

After finishing Graduation from Flatiron School, NYC, I started applying for the work. Other things being fine, I saw one thing as preferences for the candidates in almost 70% of the job postings, “Knowledge of GraphQL”.

I had no idea what is GraphQL. But after reading this preference for the candidates in the majority of the job posting, it certainly made me very curious.

What is GraphQL?

Here, the “graph” stands for crawling across your graph API by using fields and sub-fields, while “QL” stands for “query language”.

According to graphQL.org, GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.

GraphQL is created by Facebook in 2012. The whole idea behind GraphQL is to avoid creating many useless API endpoints and to allow the client to retrieve required information in many different queries from GraphQL server. It may sound little complicated but it’s super simple and neat.

GraphQL provides a common interface between the client and the server for data fetching and manipulations. It’s not language specific, it’s just a specification between the client and the server. Any client should be able to communicate with any server if they speak the common query language GraphQL.

GraphQL works on the principal of client‐specified queries. The client asks for various data from the GraphQL server via queries. The response format is described in the query and defined by the client instead of the server. The structure of the data is not hard-coded as in traditional REST APIs — this makes retrieving data from the server more efficient for the client.

For example, the client can ask for linked resources without defining new API endpoints. With the following GraphQL query, we can ask for the author specific fields and the linked array type books resource as well. Keep in mind that we don’t need to put required parameters in quotes like JSON queries.

{
author(id: 1) {
name
books {
title
}
}
}

In a resource based REST API, you may need one or more REST endpoints to get this information:

GET /authors/1 or GET /authors/1/books

What can be done with GraphQL?

  • Describe your data: You can describe the schema of your data with GraphQL. In the following query, we are saying that for the author, the name should be of type string and books should be an array of Book module.
type Author{
name: String
books: [Book]
}
  • Ask for what you want: I believe that this point is good enough to turn developers to at least try GraphQL. Unlike traditional REST APIs with predefined results of your request, you can ask GraphQL server exactly what you need. Followed by avoiding unnecessary operations on data to extract what you need after you receive it.
{
author(name: "Krish Williams") {
age
}
}

If you also want books, you can just add the books field with required sub-fields to the same query

  • Get predictable results: Unlike REST, you will always receive results according to your query, which makes it highly predictable.
{
"author": {
"age": 35
}
}

You just define it once and re-use it as much as possible. Awesome, right?

A good way to practice GraphQL is to use GitHub’s GraphQL API Explorer. For example, give the following query a try:

query {
repository(owner: "graphql", name: "graphql.github.io"){
name
description
owner {
avatarUrl
}
}
}

Notice that as you try typing a new field name below description, the IDE will automatically offer possible field names directly auto-completed from the GraphQL API itself.

That’s it for now. I will cover more about GraphQL in next few posts. Any suggestion or question on this article, please feel free to write me at hima.chhag@gmail.com or in comment section below.

Happy Coding!

--

--