GitHub GraphQL API Guide
Nowadays many companies use GraphQl API instead of traditional REST API. This is the case due to the fact that GraphQL offers the ability to define precisely the data you want - and only the data you want, which is a powerful advantage over traditional REST API endpoints. GitHub offers one of the great examples of a good GraphQL API.
Why GitHub started to support GraphQL specification? There are two main reasons:
REST API wasn’t very flexible (it sometimes required two or three separate calls to assemble a complete view of a resource, moreover responses simultaneously sent too much data and didn’t include data that consumers needed).
GitHub has an extremely detailed requirements for it’s API, which includes: an access for GitHub documentation in requests, pagination of resources, assurances of type-safety for user-supplied parameters etc.
GraphQL became a great solution for GitHub to manage the above mentioned tasks. With this API you can learn GraphQL technology in practice, you’ll be able to collect GitHub repository statistics and use this information in your projects.
This guide will cover the following topics:
How to authenticate to the GitHub GraphQL API.
How to run GitHub GraphQL API requests in API Tester, specifically:
Sending GraphQL queries in the request body;
Setting arguments;
Accessing the docs;
Using GraphQL variables;
Inline Fragments.
How to authenticate to the GitHub GraphQL API?
The GitHub GraphQL API has certain limitations in order to protect GitHub's servers against excessive or abusive calls. To make requests, you need to generate a personal access token. Detailed instructions on how to generate a token are available in the GitHub official documentation:
Note that getting an access token is compulsory in order to make any GitHub GraphQL requests!
Run GitHub GraphQL API queries in API Tester
API Tester has built-in support for GraphQL. So with the API Tester app on your mobile device you can:
send GraphQL queries in the request body,
use GraphQL variables,
use GraphQL content type headers,
browse GraphQL API Docs.
To get started with constructing you GraphQL request in API Tester:
Launch API Tester and create a new request by tapping on the “Plus” icon.
Select GraphQL method. The GraphQL request creation screen will open.
The GraphQL API has a single endpoint, which remains constant no matter what operation you perform. Place GitHub GraphQL endpoint URL in the address field:
Open the Settings tab, select OAuth in the Auth section. Place your personal token in the Access Token field.
Now you can build the request body. Let's go to the Body tab and try to get an information about your login.
Enter the following code as the request body and tap the “Send” request button.
Make sure to replace XXX with your own bearer token!
curl -X POST 'https://api.github.com/graphql' -H 'Content-Type:application/json' -d '{ "query": "query { viewer { login } \n}", "variables": {} }' -H 'Authorization: Bearer XXX'
2. We received the response from the server which includes your login. Congratulations, you have generated your first GraphQL request in API Tester!
Setting arguments for GraphQL request
Now, let's try to set the argument for the request. As an argument, we use the "key : value" pair, where the value will be a certain login. Place this code as the request body to receive user’s avatar URL for a specified login:
Make sure to replace XXX with your own bearer token!
curl -X POST 'https://api.github.com/graphql' -H 'Content-Type:application/json' -d '{ "query": "query { \n user(login:\"Matthew3dg\") { \n avatarUrl\n } \n}", "variables": {} }' -H 'Authorization: Bearer XXX'
Tap the "Send" button and you will receive the following response:
Accessing the docs
The Docs tab allows you to browse documentation provided by API developers and to see exactly what arguments the server requires for a particular request. Let's go to the Docs tab and review the list of requests that can be sent to the server with their parameters.
We'll use the docs list form the previous request and find there a request with information about repositories sorted by their topics.
The screenshot above shows that the "name" argument of the "String!" type must be passed to the request to get the topic. Also, you can find a type details for this request and study them.
We can, as well, open documentation for a repository request, which will return an array of repositories with the topic specified in the argument. And now, we can form the following query:
Make sure to replace XXX with your own bearer token!
curl -X POST 'https://api.github.com/graphql' -H 'Content-Type:application/json' -d '{ "query": "query { topic(name:\"react\"){ repositories(first:10){ edges{ node{ description } } } } \n}", "variables": {} }' -H 'Authorization: Bearer XXX'
Send this request and the response would be the following:
Let's try to send a similar request and get the total number of repositories with a given topic and also get a list of related topics. Paste this code into the Body tab:
Make sure to replace XXX with your own bearer token!
curl -X POST 'https://api.github.com/graphql' -H 'Content-Type:application/json' -d '{ "query": "query { topic(name:\"react\"){ repositories{totalCount} relatedTopics(first:10){ name } } \n}", "variables": {} }' -H 'Authorization: Bearer XXX'
The response for this request would be:
Interesting statistics, isn't it? The value of the totalCount field is impressive! Such a number of repositories indicates the popularity of the React library.
Using GraphQL variables
Now let's form a request with using variables. To describe variables in the API Tester app, you should use a Variables tab.
Enter the code presented below in GraphQL Variables tab. As a "login" value you can place your personal data to make it more interesting to study GraphQL:
Now place the following code into the Body tab of request:
Make sure to replace XXX with your own bearer token!
curl -X POST 'https://api.github.com/graphql' -H 'Content-Type:application/json' -d '{ "query": "query($number_of_pull_requests:Int!, $login:String!) { \n user(login:$login) { \n pullRequests(first : $number_of_pull_requests ){ \n edges { \n node { \n title \n changedFiles \n } \n } \n } \n } \n}", "variables": { "number_of_pull_requests": 10, "login": "octocat" } }' -H 'Authorization: Bearer XXX '
To add parameters in query you need to specify them in the Variables tab in a key-value format. Then you should place the name of the variables into the query part of the request body. The server will return the following response:
As a response we get an array of pull requests data with the topic and the number of modified files for each element.
Inline Fragments
Consider a more complex query with Inline Fragments. In this example, we will get an array of repositories with a "name" field for each, a list of commits with the date, and message for each commit. You can read more about Inline fragments in the GraphQL documentation:
Paste this code into the Body tab:
Make sure to replace XXX with your own bearer token!
curl -X POST 'https://api.github.com/graphql' -H 'Content-Type:application/json' -d '{ "query": "query{ \n viewer { \n repositories(last: 5) { \n edges { \n node { \n name \n ... on Repository { \n defaultBranchRef { \n target { \n ... on Commit { \n history(first: 10) { \n edges { \n node { \n ... on Commit { \n committedDate \n message \n } \n } \n } \n } \n } \n } \n } \n } \n } \n } \n } \n } \n}", "variables": {} }' -H ' XXX '
The requested data will be received in the response below. As you can see from the screenshot, response shows name of repository and committed date.
Let's try to get more detailed data, such as an array of tags of a certain repository. To do that,we will specify "name" and "owner" in the arguments for the repository request. Also, for the refs request, specify the "refPrefix" argument, which should start with "refs/". The request body will look like this:
Make sure to replace XXX with your own bearer token!
curl -X POST 'https://api.github.com/graphql' -H 'Content-Type:application/json' -d '{ "query": "query{ \n repository(name: \"linguist\", owner: \"octocat\") { \n refs(refPrefix: \"refs/tags/\",last: 5) { \n nodes { \n name \n target { \n oid \n } \n } \n } \n } \n}", "variables": {} }' -H 'Authorization: Bearer XXX '
The response for the request above will be following:
Last updated