GraphQL for building Github source

I was ignorant of this term, GraphQL. I was as new to it as some of you may be. I used Github v3 REST API at first to pull data and was figuring out ways to write queries. People have been using this REST API for long. I came across a problem, Github has a rate-limit on the number of API calls per hour, and with using REST, we needed ~10 calls to fetch just the metadata. The responses were huge, mostly containing data not needed by us, while still not providing all the data we need. I was perplexed here and wondered how to tackle this problem.

Going through the API documentation, I found the v4 of GitHub API, also known as GraphQL API. For those who are ignorant of this term, GraphQL is a query language for your API, for executing queries by using a type system you define for your data and gives the power to request exactly the data that they need.

Image credits: Google images

GraphQL provides a complete and understandable description of the data in your API and makes it easier to aggregate from multiple sources. What’s more? GraphQL requests are POST requests to the GraphQL endpoint and the response is returned as JSON(link) matching the query. Just the way I wanted it! Well, I was ignorant of all these features until I used it. I have cited an example below.

{
repository(owner: "OpenHumans", name: "open-humans") {
nameWithOwner name owner {
login avatarUrl
}
stargazers {
totalCount
}
forks {
totalCount
}
watchers {
totalCount
}
issues(states: [OPEN]) {
totalCount
}
description
}
}

On executing the above code in Github GraphQL explorer, the server responds with the following JSON:

{
"data": {
"repository": {
"nameWithOwner": "OpenHumans/open-humans",
"name": "open-humans",
"owner": {
"login": "OpenHumans",
"avatarUrl": "https://avatars3.githubusercontent.com/u/3341265?v=4"
},
"stargazers": {
"totalCount": 27
},
"forks": {
"totalCount": 16
},
"watchers": {
"totalCount": 12
},
"issues": {
"totalCount": 54
},
"description": "Powering openhumans.org"
}
}
}

Cool, isn’t it? I formed a similar query for the data I was in need of and lo and behold, everything seemed to fall into place. The moment I got the downloadable file on the dashboard page, I was ecstatic. A huge shout out to my mentor, Mike Escalante, for being the driving force and helping me debug when I wasn’t able to figure out my way.

Using GraphQL in Github API v4 has enabled us to easily extract required data in a single API call. My work on Github API is a part of a pull request to oh-github-source. You can check it on Github. The project still awaits some minor mutations to make sure that everything works up to the mark. I too have started working on the Twitter-source. I aim to use GraphQL for this project too 😉.

To err is human

Since I was new to this query language, it was tough making my way through the bugs at first and I used to give up at times. I realized that this phase defines the beauty of being a developer. With every new bug that I encountered, I grew as a developer. In a nutshell, this internship taught me a lot of patience. I am extremely privileged to be a part of Open Humans and happy that I am able to give back to this community.

Feel free to reach out to me in case of queries.

Keep learning and happy coding