DEV Community

Cover image for Display your repository from GitHub using GraphQL API
Zakir Sajib
Zakir Sajib

Posted on

Display your repository from GitHub using GraphQL API

I am building a website using Gatsby and main focus of this website would be showcase of my skills, experiences. Basically a kind of visual resume.

As a developer, I want to showcase my coding quality to potential clients. Almost all my codes are saved into GitHub and there are many repositories but I want to display only the important ones. I used the pinned feature of GitHub where you can show only 6 repositories.

So I want to display 5 pinned repositories of my choice and display each repository’s name, url, description and home page url.

Alt Text
Alt Text

Final product of repository section my resume site where 5 repositories are displayed:

Alt Text

In this article, I will discuss how you can pull repository from GitHub using GraphQL API in Gatsby site.

First we need to find out an appropriate Gatsby plugin for GitHub. There are few plugins out there but I found the following one which works and do the job:

Lets install this plugin in our Gatsby project. In your Gatsby project’s root, type the following command in terminal:

npm install gatsby-source-github --save-dev

After successfully installing the plugin, lets open the gatsby-config.js file in editor.

But hang on, before doing that we need to create our personal access token for our GitHub account.

Personal access tokens function like ordinary OAuth access tokens. They can be used instead of a password for Git over HTTPS, or can be used to authenticate to the API over Basic Authentication.

Its very easy and straight forward.

Login to your GitHub account and go to settings and then Developer settings. In Developer settings page, click the generate new token button. In New personal access token page give a meaningful name of the token in note field and select correct scope(s).

Selecting only public_repo will do the job. And then click save token.

Alt Text

Now we have our token, copy it and paste in gatsby-config.js file.

   resolve: 'gatsby-source-github',
         options: {
           headers: {
             Authorization: `Bearer pasteyourpersonalaccesstokencodehere`,
           queries: [
             `{ viewer {                 
               pinnedItems(first: 5, types: REPOSITORY){
                nodes {
                  ... on Repository {
Enter fullscreen mode Exit fullscreen mode

The above code block will go within

plugins: [

Enter fullscreen mode Exit fullscreen mode

block in gatsby-config.js file. This is important.

Now our personal access token will go in options: headers: Authorization field after the word Bearer. Don’t remove the word Bearer. This is important.

Now the queries part. The query starts with { viewer { ... } }. This is the query I built using GitHub GraphQL API explorer which is found here:

You need to login to your GitHub account and go to to the link.

GitHub’s GraphQL API Explorer makes use of your real, live, production data.

Alt Text

Here I crafted the queries and then copy and paste it into gatsby-config.js file.

My goal was to find out all repository from pinned items so my query was like this:

After you copy the code and paste into gatsby-config.js file, save it.

One of the important parts is done.

Now lets move on to second part.

Run gatsby develop command in terminal.

Open browser and connect:


From here, we will build our custom query and will use it on our code later.

We will create a component for example Repository and copy the query and paste there. So lets build the query first.

Alt Text

so this is our query which shows all pinned repository items and their meta data such as name, url, description and home page url.

Keep the GraphQL window open and now create a component called Repository.js file inside src/components directory of your gatsby project.

This is my Repository.js file

import React from 'react'
import {useStaticQuery, graphql} from 'gatsby'

const Repository = () => {
const data = useStaticQuery(graphql`
   query MyQuery {
      githubViewer {
    pinnedItems {
    nodes {
{, i) => (
    <div className="pf-grid-item" key={i}>
      <a className="pf-project" 
         rel="noopener noreferrer">
         GitHub URL
export default Repository
Enter fullscreen mode Exit fullscreen mode

I used useStaticQuery hook to query the data, but you can choose either useStaticQuery or StaticQuery or Page query. See the screenshot below:

Alt Text

As you can see nodes data comes as an array so I used map method to loop through the data and accessed the data repository with dot operator.

Now you can call this Repository component from other component like this:

<Repository />

Enter fullscreen mode Exit fullscreen mode

This is an example of how we can use GraphQL to retrieve data from any third party application. This example could be a starting point to start with and sky is the limit to explore unknown!

Live example:

Top comments (0)