OPEN TO WORK

Available for new opportunities! Let's build something amazing together.

#DevHack: Fetching sponsors via the GitHub GraphQL API

Development GitHub GraphQL API
post

This post is over a year old, some of this information may be out of date.

To automate the roll-up of sponsors for Front Matter on the website. I started to look through the GitHub Rest API documentation to check it could receive this kind of information. Unfortunately, the Rest API does not provide you with this information, so I went to where the cool kids go these days, GraphQL. The GitHub GraphQL API delivers you more precise and flexible queries and is your go-to place these days.

GraphQL Explorer is excellent when you have never worked with the API. Via the explorer, I found the correct query to use.

Authentication

To retrieve your sponsors, you first need to have an OAuth Token. For this, you can create a personal access token. You can follow the steps in “Creating a personal access token” documentation.

Fetching the GraphQL data

The GraphQL query for retrieving your sponsors looks as follows:

query SponsorQuery {
viewer {
sponsors(first: 100) {
edges {
node {
... on User {
id
name
url
avatarUrl
}
}
}
}
}
}

On the Front Matter website, I retrieve this information via my custom API, which performs a POST request to the https://api.github.com/graphql API.

The code looks as follows:

const response = await fetch(`https://api.github.com/graphql`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': `token ${process.env.GITHUB_AUTH}`
},
body: JSON.stringify({
query: `query SponsorQuery {
viewer {
sponsors(first: 100) {
edges {
node {
... on User {
id
name
url
avatarUrl
}
}
}
}
}
}`
})
});
if (response && response.ok) {
const data = await response.json();
// Start working with the data
}

Related articles

Report issues or make changes on GitHub

Found a typo or issue in this article? Visit the GitHub repository to make changes or submit a bug report.

Comments

Elio Struyf

Solutions Architect & Developer Expert

Loading...

Let's build together

Manage content in VS Code

Present from VS Code