Last active
September 18, 2017 07:09
-
-
Save liuderchi/455aed077814dc533764a1860603ab25 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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