Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Commit

Permalink
Fix stars when no github token is provided (#115)
Browse files Browse the repository at this point in the history
* Fix stars when no github token is provided

* Actually import GH graphql

* Use the right repo

* Remove console log
rogermparent authored Jun 28, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 25732f6 commit b1541dd
Showing 5 changed files with 63 additions and 239 deletions.
13 changes: 0 additions & 13 deletions gatsby-config.js
Original file line number Diff line number Diff line change
@@ -100,19 +100,6 @@ const plugins = [
'gatsby-plugin-meta-redirect'
]

process.env.GITHUB_TOKEN &&
plugins.push({
resolve: 'gatsby-source-graphql',
options: {
typeName: 'GitHub',
fieldName: 'github',
url: 'https://api.github.com/graphql',
headers: {
Authorization: `Bearer ${process.env.GITHUB_TOKEN}`
}
}
})

module.exports = {
plugins,
siteMetadata
46 changes: 46 additions & 0 deletions gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const Prism = require('prismjs')
const loadLanguages = require('prismjs/components/')
const { graphql } = require('@octokit/graphql')

const terminalSlides = require('./content/home-slides')

@@ -52,3 +53,48 @@ exports.sourceNodes = ({ actions, createNodeId, createContentDigest }) => {
createNode(node)
})
}

exports.createSchemaCustomization = async ({
actions: { createTypes },
schema
}) => {
createTypes(
schema.buildObjectType({
name: 'StaticGithubData',
fields: {
stars: 'String!'
}
})
)
}

exports.createResolvers = async ({ createResolvers }) => {
createResolvers({
Query: {
staticGithubData: {
type: 'StaticGithubData',
async resolve() {
const { GITHUB_TOKEN } = process.env
if (GITHUB_TOKEN) {
const query = await graphql(
`
{
repository(owner: "iterative", name: "mlem") {
stargazers {
totalCount
}
}
}
`,
{ headers: { authorization: `token ${GITHUB_TOKEN}` } }
)

const stars = query.repository.stargazers.totalCount
return { stars }
}
return { stars: 8888 }
}
}
}
})
}
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -39,7 +39,6 @@
"gatsby-remark-images": "^6.14.0",
"gatsby-remark-prismjs": "^6.12.1",
"gatsby-source-filesystem": "^4.1.0",
"gatsby-source-graphql": "^4.15.0",
"gatsby-transformer-sharp": "^4.1.0",
"node-cache": "^5.1.2",
"npm-run-all": "^4.1.5",
@@ -49,8 +48,8 @@
"react": "^18.0.0",
"react-dom": "^18.0.0",
"react-helmet": "^6.1.0",
"serve-handler": "^6.1.3",
"repo-link-check": "^0.11.0",
"serve-handler": "^6.1.3",
"tailwindcss": "3.0.23",
"typed.js": "^2.0.12"
},
27 changes: 12 additions & 15 deletions src/utils/hooks/useStars.tsx
Original file line number Diff line number Diff line change
@@ -3,33 +3,30 @@ import { useStaticQuery, graphql } from 'gatsby'
import fetch from 'isomorphic-fetch'

export default function useStars(): number | null {
const staticStars =
useStaticQuery(graphql`
query GithubStarsQuery {
github {
repository(name: "mlem", owner: "iterative") {
stargazerCount
}
}
const staticStars = useStaticQuery(graphql`
query GithubStarsQuery {
staticGithubData {
stars
}
`).github.repository.stargazerCount || 888
}
`).staticGithubData.stars

// Maintain an updatable state so we can update stars on delivery
const [stars, setStars] = useState(staticStars)

// Run an IIFE to update from the server on the client side.
// Update on the client side
useEffect(() => {
;(async (): Promise<void> => {
const res = await fetch(`/api/github/stars`)
fetch(`/api/github/stars`).then(res => {
if (res.status === 200) {
const json = await res.json()
setStars(json.stars)
res.json().then(json => {
setStars(json.stars)
})
} else {
console.warn(
`Stars update response status was ${res.status}! Using static value.`
)
}
})()
})
}, [])

return stars
Loading

0 comments on commit b1541dd

Please sign in to comment.