Skip to content

Instantly share code, notes, and snippets.

@liuderchi
Last active September 18, 2017 07:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liuderchi/455aed077814dc533764a1860603ab25 to your computer and use it in GitHub Desktop.
Save liuderchi/455aed077814dc533764a1860603ab25 to your computer and use it in GitHub Desktop.
// 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