Independent Tech & Cryptocurrency News
Bitcoin
$5,594.92
+34.41
(+0.62%)
Ethereum
$181.05
+3.79
(+2.14%)
Ripple
$0.47
+0.02
(+4.68%)
Litecoin
$44.19
+2.15
(+5.11%)
EOS
$4.71
+0.02
(+0.43%)
Cardano
$0.06
0
(+3.27%)
Stellar
$0.24
+0.01
(+5.71%)
NEO
$13.14
-0.51
(-3.74%)
NEM
$0.10
0
(+2.1%)
DigitalCash
$138.52
-9.28
(-6.28%)
Tether
$0.97
+0.02
(+2.45%)
Binance Coin
$8.11
QTUM
$3.16
0
(0%)
Verge
$0.01
-0
(-2.8%)
Ontology
$1.42
0
(0%)
ZCash
$110.03
+2.74
(+2.55%)
Steem
$0.60

Why I felt the need to use GraphQL! – Hacker Noon

3


In this article, we will discuss a scenario in which GitHub’s REST API isn’t efficient enough and how GitHub’s GraphQL API helps us in resolving the problems.

We used Octokit’s GitHub REST API Client extensively while working on Git Repository Analyzer project. The use of Node.js asynchronous calls, helped in making simultaneous HTTP calls but there were too many HTTP calls that were made to retrieve the required data. Let’s see how we can overcome the problems by using GitHub’s GraphQL API.

Data Requirements

  1. Search the GitHub repositories based on the search parameters; e.g. language:java.
  2. Sort these repositories in the descending order of the number of stars found on the repository.
  3. For each repository, get the top 30 pull requests.
  4. For each pull request, get the issue and patch/diff data. Issue data and Patch data can be obtained by making 2 separate HTTP calls. A patch is a small file that indicates what was changed in a repository.

Using the GitHub REST API v3

Based on the above requirements, we retrieve the data in the following manner.

Problems

Let’s look at the problems faced with the above approach:

Let’s say I want to get the required data from 5 GitHub repositories.

  1. A HTTP call for getting the repositories details (count: 1)
  2. For each repository, we get the 30 pull requests data (count: 5)
  3. For each pull request, we make 1 HTTP call for getting the Issue data for the pull request (count: 30*5 = 150)
  4. For each pull request, we make 1 HTTP call for getting the patch/diff data for the pull request (count: 30*5 = 150)

In total we make 306 HTTP calls for getting the Issue and Patch data for 5 repositories based on the search query.

After each HTTP call, only few fields from the response are used for the subsequent calls. Deletion of the unused data from the response becomes necessary so as to reduce the size of the final output.

What is GraphQL ?

From graphql.org :

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.

Why use GitHub’s GraphQL API ?

  • Drastic Reduction in HTTP calls

In the above scenario, when we use GitHub’s REST API, we made 306 HTTP calls to retrieve data. All this data can now be received with a very few HTTP calls. We will see the exact count of HTTP calls and the details about the implementation in the next article.

  • Only get the required fields

We only receive the data we ask for — nothing more and nothing less. We no longer have the need to delete the unused data.

We no longer need to use different URLs (as in case for REST API calls) for retrieving the data. GraphQL has a single endpoint which is independent of the required data.

Currently, GitHub’s GraphQL API doesn’t support the above use case. A request has been made for the same purpose.



https://hackernoon.com

Get real time updates directly on you device, subscribe now.

Leave A Reply

Your email address will not be published.