Get started with GitHub GraphQL API 👨‍🔬
Tags: graphql, node, webdev, javascript
Published: 2019-01-23 18:04:10

---
title: Get started with GitHub GraphQL API 👨‍🔬
published: true
description: A quick tutorial to query the GitHub GraphQL API
tags: #graphql #nodejs #webdev #javascript
---

[GraphQL](https://fr.wikipedia.org/wiki/GraphQL) APIs are since a few years more and more popular. The purpose of this tutorial is not to teach you the GraphQL concepts and techniques but to teach you how to query the GitHub GraphQL API.
I will use [Node.js](https://fr.wikipedia.org/wiki/Node.js) for the example, but you can achieve it with any programming language which allows you to perform [HTTP](https://fr.wikipedia.org/wiki/Hypertext_Transfer_Protocol) request.

## Setting up

First of all, you need to create a [personal access token](https://github.com/settings/tokens/new).

    github > settings > developper settings > personnal access token > generate new token


Github will ask you a token description and the scope for your application, select what you need.

Now that we have our token, it's query time 🎯

## Create Query

To test and create your queries, GitHub proposes a very powerful tool : [GraphQL API Explorer](https://developer.github.com/v4/explorer/).

For this example, I've created a very simple query to get my GitHub pinned repositories, which is not possible with the GitHub REST API.

```
query{
  repositoryOwner(login: "thomasaudo") {
    ... on User {
      pinnedRepositories(first: 6) {
        edges {
          node {
            name,
            description,
            url
          }
        }
      }
    }
  }
}
```

The [documentation](https://developer.github.com/v4/) is really complete 📒


## Query
To query the GitHub server, we will need to send a post request to the [unique endpoint](https://api.github.com/graphql)
Of course, we have to custom a little bit our request :
* A parameter: the query
* Bearer Authentication also known as token authentication: your personal access token ( in the header )

A CURL example from the GitHub official documentation :
```
curl -H "Authorization: bearer token" -X POST -d " \
 { \
   \"query\": \"query { viewer { login }}\" \
 } \
" https://api.github.com/graphql
```
I have created a really simple nodeJS example. To perform the query, I use the package 'axios'.
```
// Require axios to perform easy promise-based POST request
const axios = require('axios');
// Define constant
// Endpoint URL
const githubUrl = 'https://api.github.com/graphql'
// Your personal access token
const token = '7bacddc5d40dabfe6edca28b986a0247cfe3803b'
// The Authorization in the header of the request
const oauth = {Authorization: 'bearer ' + token}
// The GraphQL query, a string
const query = '{' +
                'repositoryOwner(login: "thomasaudo") { ' +
                  '... on User {' +
                    'pinnedRepositories(first: 6) {' +
                      'edges {' +
                        'node {' +
                          'name,' +
                          'description,' +
                          'url' +
                          '}' +
                        '}' +
                      '}' +
                    '}' +
                  '}' +
                '}'

// Post request, axios.post() return a Promise
axios.post(githubUrl, {query: query}, {headers: oauth})
  .then(function (response) {
    // On success, print the response
    console.log(response.data);
  })
  .catch(function (error) {
    On error, print the error
    console.log(error);
  });

```

__Here we are!__

I hope you have liked this post, my first one 🚀