Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// sample reference https://www.npmjs.com/package/graphql-client
// GitHub API v4 reference https://developer.github.com/v4/explorer/
const graphqlClient = require('graphql-client')
// GET your token https://github.com/settings/tokens
const TOKEN = 'YOUR-TOKEN-HERE'
// choose fields of user info https://developer.github.com/v4/reference/object/user/#fields
const USERFIELDS = [
'login',
'name',
'bio',
]
const followersQuery = (count = 5) => `
followers(last: ${count}) {
nodes {
login
}
}` // followerConnection info https://developer.github.com/v4/reference/object/followerconnection/
const starredRepoQuery = () => `
starredRepositories {
totalCount
}` // StarredRepositoryConnection https://developer.github.com/v4/reference/object/starredrepositoryconnection/
const client = graphqlClient({
url: 'https://api.github.com/graphql',
headers: {
Authorization: `Bearer ${TOKEN}`
}
})
const query = `query {
viewer {
${USERFIELDS.join(',')}
${starredRepoQuery()}
${followersQuery()}
}
}`
client.query(query, null, function optionalCallback(req, res) {
if(res.headers.get('x-powered-by') === 'Express') {
throw new Error("Don't want content served from express")
}
})
.then(({ data: { viewer } }) => {
USERFIELDS.forEach(field => console.log(`${field}: ${viewer[field]}\n`))
console.log(`you had starred ${viewer.starredRepositories.totalCount} repos\n`)
console.log(`my last 5 followers:\n${viewer.followers.nodes.map(user => user.login)}`)
})
.catch(err => {
console.log(err.message)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.