The Github GraphQL API provides a lot more capabilities than I can cover in one snack.
However, in searching for a way to share the small lessons that I pick up week in week out, I knew that publishing through Contentful, which it the CMS, I use for the main content in my blog site, was a little too much to share easily and quicly. Posting on Github and sharing that as gists seemed like I would be writing too much for a gist that is supposed to be a short code snippet - which was the original title of this section. Although MichaelCurrin proves me otherwise! His article is what got me goint in the first place!
In order to use the Github GraphQL API, you can either use the API explorer by logging in through your github account, use an api testing tool like Insomnia, or you can use it programmatically.
Since I was using the Apollo Graph QL library to query my backend - that helps me manage comments and likes, I began to do a little research on how to query the Github GraphQL API. I will link the articles below. This stack overflow answered how to add an authorization header to an Apollo Client Query.
We will be using writing our query in a node.js environment using both Apollo Client and Axios
The only permission you need for this task is public_repo
under repo
:
Give your token a name and will look something like this:
89fdd35bcd40787b519e97462cec0f9975a66a58
Note the token above is revoked and you'll need to generate yours. Once you're done, we're ready for the next step!
In my case, I will be looking for files in my repository called articles
. If you'd like to use your own repository, simply make note of your repository name.
Install the Apollo client
yarn add @apollo/client graphql
Working in your js file, import the Apollo client and a few methods that we will make use of:
import { ApolloClient, InMemoryCache, gql, ApolloLink, HttpLink } from '@apollo/client'
Initialize a new apollo client with the github graphQL endpoint and your token
const token = '89fdd35bcd40787b519e97462cec0f9975a66a58'
const endpoint = 'https://api.github.com/graphql'
// Add the toke to the header of your client for all your requestsconst githubLClient = new ApolloClient({ uri: endpoint, headers: { authorization: `Bearer ${token}` }, cache: new InMemoryCache({ addTypename: false })})
Make the query I referenced github user int128's gist for the structure of the graphQl query:
let request = await githubClient.query({ query: gql` { repository(owner: "laudebugs", name: "articles") { defaultBranchRef { target { ... on Commit { file(path: "/") { type object { ... on Tree { entries { name object { ... on Blob { text } } } } } } } } } } } `})
Parse your output to obtain the file's contents. By making this same query on Github's GraphQL explorer, the data returned, which is essentially a json object, at the time of writing this, looks like this:
{ "data": { "repository": { "defaultBranchRef": { "target": { "file": { "type": "tree", "object": { "entries": [ { "name": "QraphQL.md", "object": { "text": "# Exploring GraphQL\n" } }, { "name": "README.md", "object": { "text": "# Articles" } } ] } } } } } }}
So, if to obtain the entries, we would access them by:
let result = request.data.repository.defaultBranchRef.target.file.type.object.entries
Install the axios npm package
npm install axios
Import exios into your node project:
import axios from 'axios'
initialize an authentication object and the query string that will be attatched to your request
// The Authorization in the header of the requestconst oauth = { Authorization: 'bearer ' + token }
// The Query Stringconst query = ` { repository(owner: "laudebugs", name: "articles") { defaultBranchRef { target { ... on Commit { file(path: "/") { type object { ... on Tree { entries { name object { ... on Blob { text } } } } } } } } } } } `
Make the request, adding in the query and the header
let request = axios.post(githubUrl, { query: query }, { headers: oauth })
Parse your output as above:
let result = request.data.repository.defaultBranchRef.target.file.type.object.entries
Last modified on: Fri Jun 24 2022