Last active
August 23, 2019 11:02
-
-
Save kamiljozwik/bf648b29403e692e37b2c923349acb69 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require('path'); | |
const { GraphQLClient } = require(`graphql-request`); | |
const parseGHUrl = require(`parse-github-url`); | |
/** | |
* Used to gather repo details data | |
*/ | |
const githubApiClient = process.env.GITHUB_TOKEN | |
? new GraphQLClient(`https://api.github.com/graphql`, { | |
headers: { | |
authorization: `Bearer ${process.env.GITHUB_TOKEN}`, | |
}, | |
}) | |
: console.log('GITHUB TOKEN NOT FOUND!') | |
exports.onCreateNode = async ({ node, actions }) => { | |
const { createNodeField } = actions; | |
/** | |
* Fetch additional data from Github for repos fetch from CMS | |
*/ | |
switch (node.internal.type) { | |
case 'ContentfulRepoEntry': { | |
const repoMeta = node.github ? parseGHUrl(node.github) : null; // check if github url is provided | |
const repoData = await ( repoMeta | |
? githubApiClient.request(` | |
query { | |
repository(owner:"${repoMeta.owner}", name:"${repoMeta.name}") { | |
name | |
stargazers { | |
totalCount | |
} | |
createdAt | |
} | |
} | |
`) | |
: null | |
); | |
/** | |
* Add field with data to repo's Node | |
*/ | |
createNodeField({ | |
node, | |
name: 'githubData', | |
value: repoData, | |
}) | |
} | |
} | |
} | |
exports.createPages = async ({ graphql, actions }) => { | |
const { createPage } = actions; | |
// Create pages here using data fetched durin 'onCreateNode' | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment