Skip to content

Instantly share code, notes, and snippets.

@kamiljozwik
Last active August 23, 2019 11:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamiljozwik/bf648b29403e692e37b2c923349acb69 to your computer and use it in GitHub Desktop.
Save kamiljozwik/bf648b29403e692e37b2c923349acb69 to your computer and use it in GitHub Desktop.
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