api/projects: clean up GQL query

This commit is contained in:
2021-06-03 10:34:21 -04:00
parent e68aec80c7
commit 2a1a696b5b
2 changed files with 18 additions and 25 deletions
+4 -4
View File
@@ -19,19 +19,19 @@
</div>`; </div>`;
} }
if (repo.stargazers > 0) { if (repo.stargazerCount > 0) {
html += ` html += `
<div class="repo-meta"> <div class="repo-meta">
<svg viewBox="0 0 16 16" height="16" width="16"><path fill-rule="evenodd" d="M8 .25a.75.75 0 01.673.418l1.882 3.815 4.21.612a.75.75 0 01.416 1.279l-3.046 2.97.719 4.192a.75.75 0 01-1.088.791L8 12.347l-3.766 1.98a.75.75 0 01-1.088-.79l.72-4.194L.818 6.374a.75.75 0 01.416-1.28l4.21-.611L7.327.668A.75.75 0 018 .25zm0 2.445L6.615 5.5a.75.75 0 01-.564.41l-3.097.45 2.24 2.184a.75.75 0 01.216.664l-.528 3.084 2.769-1.456a.75.75 0 01.698 0l2.77 1.456-.53-3.084a.75.75 0 01.216-.664l2.24-2.183-3.096-.45a.75.75 0 01-.564-.41L8 2.694v.001z"></path></svg> <svg viewBox="0 0 16 16" height="16" width="16"><path fill-rule="evenodd" d="M8 .25a.75.75 0 01.673.418l1.882 3.815 4.21.612a.75.75 0 01.416 1.279l-3.046 2.97.719 4.192a.75.75 0 01-1.088.791L8 12.347l-3.766 1.98a.75.75 0 01-1.088-.79l.72-4.194L.818 6.374a.75.75 0 01.416-1.28l4.21-.611L7.327.668A.75.75 0 018 .25zm0 2.445L6.615 5.5a.75.75 0 01-.564.41l-3.097.45 2.24 2.184a.75.75 0 01.216.664l-.528 3.084 2.769-1.456a.75.75 0 01.698 0l2.77 1.456-.53-3.084a.75.75 0 01.216-.664l2.24-2.183-3.096-.45a.75.75 0 01-.564-.41L8 2.694v.001z"></path></svg>
<span>${repo.stargazers_pretty}</span> <span>${repo.stargazerCount_pretty}</span>
</div>`; </div>`;
} }
if (repo.forks > 0) { if (repo.forkCount > 0) {
html += ` html += `
<div class="repo-meta"> <div class="repo-meta">
<svg viewBox="0 0 16 16" height="16" width="16"><path fill-rule="evenodd" d="M5 3.25a.75.75 0 11-1.5 0 .75.75 0 011.5 0zm0 2.122a2.25 2.25 0 10-1.5 0v.878A2.25 2.25 0 005.75 8.5h1.5v2.128a2.251 2.251 0 101.5 0V8.5h1.5a2.25 2.25 0 002.25-2.25v-.878a2.25 2.25 0 10-1.5 0v.878a.75.75 0 01-.75.75h-4.5A.75.75 0 015 6.25v-.878zm3.75 7.378a.75.75 0 11-1.5 0 .75.75 0 011.5 0zm3-8.75a.75.75 0 100-1.5.75.75 0 000 1.5z"></path></svg> <svg viewBox="0 0 16 16" height="16" width="16"><path fill-rule="evenodd" d="M5 3.25a.75.75 0 11-1.5 0 .75.75 0 011.5 0zm0 2.122a2.25 2.25 0 10-1.5 0v.878A2.25 2.25 0 005.75 8.5h1.5v2.128a2.251 2.251 0 101.5 0V8.5h1.5a2.25 2.25 0 002.25-2.25v-.878a2.25 2.25 0 10-1.5 0v.878a.75.75 0 01-.75.75h-4.5A.75.75 0 015 6.25v-.878zm3.75 7.378a.75.75 0 11-1.5 0 .75.75 0 011.5 0zm3-8.75a.75.75 0 100-1.5.75.75 0 000 1.5z"></path></svg>
<span>${repo.forks_pretty}</span> <span>${repo.forkCount_pretty}</span>
</div>`; </div>`;
} }
+14 -21
View File
@@ -1,32 +1,35 @@
"use strict"; "use strict";
const { GraphQLClient } = require("graphql-request"); const { GraphQLClient, gql } = require("graphql-request");
const { escape } = require("html-escaper"); const { escape } = require("html-escaper");
const numeral = require("numeral"); const numeral = require("numeral");
const { DateTime } = require("luxon"); const { DateTime } = require("luxon");
const username = "jakejarvis"; const username = "jakejarvis";
const endpoint = "https://api.github.com/graphql";
async function fetchRepos(sort, limit) { async function fetchRepos(sort, limit) {
const client = new GraphQLClient("https://api.github.com/graphql", { // https://docs.github.com/en/graphql/guides/forming-calls-with-graphql
const client = new GraphQLClient(endpoint, {
headers: { headers: {
Authorization: `Bearer ${process.env.GH_PUBLIC_TOKEN}`, Authorization: `Bearer ${process.env.GH_PUBLIC_TOKEN}`,
Accept: "application/vnd.github.v3+json", Accept: "application/vnd.github.v3+json",
}, },
}); });
const query = ` // https://docs.github.com/en/graphql/reference/objects#repository
const query = gql`
query ($sort: String, $limit: Int) { query ($sort: String, $limit: Int) {
user(login: "${username}") { user(login: "${username}") {
repositories( repositories(
last: $limit, first: $limit,
isLocked: false, isLocked: false,
isFork: false, isFork: false,
ownerAffiliations: OWNER, ownerAffiliations: OWNER,
privacy: PUBLIC, privacy: PUBLIC,
orderBy: { orderBy: {
field: $sort, field: $sort,
direction: ASC direction: DESC
} }
) { ) {
edges { edges {
@@ -35,12 +38,8 @@ async function fetchRepos(sort, limit) {
url url
description description
pushedAt pushedAt
stargazers { stargazerCount
totalCount forkCount
}
forks {
totalCount
}
primaryLanguage { primaryLanguage {
name name
color color
@@ -52,23 +51,17 @@ async function fetchRepos(sort, limit) {
} }
`; `;
const response = await client.request(query, { const response = await client.request(query, { sort, limit });
sort: sort,
limit: limit,
});
const currentRepos = response.user.repositories.edges.map(({ node: repo }) => ({ const currentRepos = response.user.repositories.edges.map(({ node: repo }) => ({
...repo, ...repo,
description: escape(repo.description), description: escape(repo.description),
stargazers: repo.stargazers.totalCount, stargazerCount_pretty: numeral(repo.stargazerCount).format("0,0"),
stargazers_pretty: numeral(repo.stargazers.totalCount).format("0,0"), forkCount_pretty: numeral(repo.forkCount).format("0,0"),
forks: repo.forks.totalCount,
forks_pretty: numeral(repo.forks.totalCount).format("0,0"),
pushedAt_relative: DateTime.fromISO(repo.pushedAt).toRelative({ locale: "en" }), pushedAt_relative: DateTime.fromISO(repo.pushedAt).toRelative({ locale: "en" }),
})); }));
// reverse hack to get expected sorting return currentRepos;
return currentRepos.reverse();
} }
exports.handler = async (event) => { exports.handler = async (event) => {