Wanting to build a metrics on private and public repo issues and pull requests I found the Gatsby plugin gatsby-source-github-api
.
Following the PAT docs I created a PAT token for my Github username.
In my gatsby-config.js I added:
require('dotenv').config({
path: `.env.${process.env.NODE_ENV}`,
})
const { githubApiQuery } = require('./src/utils/github-api')
{
resolve: `gatsby-source-github-api`,
options: {
// token: required by the GitHub API
token: process.env.GITHUB_TOKEN,
// GraphQLquery: defaults to a search query
graphQLQuery: githubApiQuery,
// variables: defaults to variables needed for a search query
variables: {
github_login: process.env.GITHUB_LOGIN,
repo_count: 100,
},
},
},
.env.development:
GITHUB_TOKEN=github-long-token-from-pat
GITHUB_LOGIN=org-name
Testing my query I went to Github's GraphQL API and built:
query($github_login: String!, $repo_count: Int!){
viewer {
organization(login: $github_login) {
login
id
location
name
url
repositories(first: $repo_count, privacy:PRIVATE) {
nodes {
id
name
openGraphImageUrl
createdAt
stargazerCount
url
description
}
}
}
}
Query variables:
{"github_login": "org-name", "repo_count": 100}
Built an API file, github-api.js:
exports.githubApiQuery = `
query($github_login: String!, $repo_count: Int!){
viewer {
organization(login: $github_login) {
login
id
location
name
url
repositories(first: $repo_count, privacy:PRIVATE) {
nodes {
id
name
openGraphImageUrl
createdAt
stargazerCount
url
description
}
}
}
}
`
Restarted my project with npm start
and navigated to my URL, http://localhost:8000/___graphql, and in GraphiQL tried to build a query with the data but data
is not present:
Seeing if this had been asked I read through:
- error "gatsby-source-github-api" threw an error while running the sourceNodes lifecycle: token is undefined
- Error when deploying the gatsby app with gatsby-source-buttercms on Zeit
- Populating Github GraphQL in React (Gatsby)
- Graphql query data from github api in Gatsby
- netlify deployment build is failing for gatsby-contentful site
Dependencies
"dependencies": {
"@popperjs/core": "^2.11.4",
"bootstrap": "^5.1.3",
"formik": "^2.2.9",
"gatsby": "^4.10.3",
"gatsby-plugin-feed": "^4.10.2",
"gatsby-plugin-gatsby-cloud": "^4.10.1",
"gatsby-plugin-google-analytics": "^4.10.0",
"gatsby-plugin-image": "^2.11.1",
"gatsby-plugin-manifest": "^4.10.2",
"gatsby-plugin-offline": "^5.10.2",
"gatsby-plugin-react-helmet": "^5.10.0",
"gatsby-plugin-robots-txt": "^1.7.0",
"gatsby-plugin-sharp": "^4.11.1",
"gatsby-plugin-sitemap": "^5.10.2",
"gatsby-remark-copy-linked-files": "^5.10.0",
"gatsby-remark-images": "^6.10.2",
"gatsby-remark-prismjs": "^6.10.0",
"gatsby-remark-responsive-iframe": "^5.10.0",
"gatsby-remark-smartypants": "^5.10.0",
"gatsby-source-contentful": "^7.9.1",
"gatsby-source-filesystem": "^4.11.1",
"gatsby-source-github-api": "^1.0.0",
"gatsby-transformer-remark": "^5.10.2",
"gatsby-transformer-sharp": "^4.11.0",
"prismjs": "^1.27.0",
"react": "^17.0.1",
"react-bootstrap": "^2.2.2",
"react-dom": "^17.0.1",
"react-helmet": "^6.1.0",
"react-hot-toast": "^2.2.0",
"react-icons": "^4.3.1",
"typeface-merriweather": "0.0.72",
"typeface-montserrat": "0.0.75"
},
Why am I unable to get any data
from an org with the gatsby-source-github-api
plugin or is there a particular way I get all my repository information? The above query in Github's GraphQL API works and I can get all my private repos but how can I do this in Gatsby?
Edit
Token Permissions under Settings > Developer Settings > Personal access tokens
:
Select scopes I've given full repo
permissions.
Further reading I found Cant get gatsby-source-github-api to load properly which had a comment that suggested:
change query with addition of isFork: false
:
exports.githubApiQuery = `
query($github_login: String!, $repo_count: Int!){
viewer {
organization(login: $github_login) {
login
id
location
name
url
repositories(first: $repo_count, privacy: PRIVATE, isFork: false) {
nodes {
id
name
openGraphImageUrl
createdAt
stargazerCount
url
description
}
}
}
}
`
changed in config with the addition of url
:
{
resolve: `gatsby-source-github-api`,
options: {
url: 'https://api.github.com/graphql',
// token: required by the GitHub API
token: process.env.GITHUB_TOKEN,
// GraphQLquery: defaults to a search query
graphQLQuery: githubApiQuery,
// variables: defaults to variables needed for a search query
variables: {
github_login: process.env.GITHUB_LOGIN,
repo_count: 100,
},
},
},
Edit 2
Trying different approaches I've tried:
query
query ($github_org: String!, $github_login: String!, $repo_count: Int!) {
user(login: $github_login) {
organization(login: $github_org) {
login
id
location
name
url
repositories(first: $repo_count, privacy: PRIVATE, isFork: false) {
nodes {
id
name
openGraphImageUrl
createdAt
stargazerCount
url
description
}
}
}
}
}
variables
{"github_login":"github-username","github_org": "github-org", "repo_count": 100}
added to config:
{
resolve: `gatsby-source-github-api`,
options: {
url: 'https://api.github.com/graphql',
// token: required by the GitHub API
token: process.env.GITHUB_TOKEN,
// GraphQLquery: defaults to a search query
graphQLQuery: githubApiQuery,
// variables: defaults to variables needed for a search query
variables: {
github_login: process.env.GITHUB_LOGIN,
github_org: process.env.GITHUB_ORG,
repo_count: 100,
},
},
}
added to api file:
exports.githubApiQuery = `
query($github_login: String!, $github_org: String!, $repo_count: Int!){
user(login: $github_login) {
organization(login: $github_org) {
login
id
location
name
url
repositories(first: $repo_count, privacy: PRIVATE, isFork: false) {
nodes {
id
name
openGraphImageUrl
createdAt
stargazerCount
url
description
}
}
}
}
`
Above approach still returns a result in Github's Explorer but when I try with GraphiQL I'm not able to get anything.