Pull data into Gatsby from Github API v4
Clone or download
Pull request Compare This branch is 12 commits behind ldd:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
.eslintrc
.gitignore
LICENSE
README.md
gatsby-node.js
index.js
package.json
yarn.lock

README.md

gatsby-source-github

Source plugin for pulling data into Gatsby from the official Github v4 graphQL API.

Install

npm install --save gatsby-source-github

How to use

Follow Github's guide to generate a token

Once you are done, either create a gatsby-config.js file or open the one you already have.

In there, you want to add this plugin and at least add the token in the options object:

// In your gatsby-config.js
plugins: [
  {
    resolve: `gatsby-source-github`,
    options: {
      // token required by the Github API
      token: someString = undefined, //required
      // graphQL query
      // defaults to a search query. See below
      graphQLQuery: anotherString,
      // graphQL variables
      // defaults to variables needed for a
      // search query. See below
      variables: someObject
    }
  }
]

Examples (How to use)

Simple search query

// In your gatsby-config.js
plugins: [
  {
    resolve: `gatsby-source-github`,
    options: {
      token: 'hunter2',
      variables: {
        q: "author:ldd state:closed type:pr sort:comments",
        nFirst: 2
      }
    }
  }
]

resulting API call:

  query ($nFirst: Int, $q: String) {
    search(query: "${q}", type: ISSUE, first: ${nFirst}){
      edges{
        node{
          ... on PullRequest{
            title
          }
        }
      }
    }
  }

generic graphQL query

// In your gatsby-config.js
plugins: [
  {
    resolve: `gatsby-source-github`,
    options: {
      token: 'hunter2',
      variables: {},
      graphQLQuery: `
        query {
          repository(owner:"torvalds",name:"linux"){
            description
          }
        }
        `
    }
  }
]

resulting API call:

  query {
    repository(owner:"torvalds", name:"linux"){
      description
    }
  }

Tips and Tricks

You'll probably want to use valid graphQL queries. To help you, Github has a Query Explorer with auto-completion.

Query Explorer

Changelog

v0.0.3 Initial public release