1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-07-03 14:06:40 -04:00

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

View File

@ -19,19 +19,19 @@
</div>`;
}
if (repo.stargazers > 0) {
if (repo.stargazerCount > 0) {
html += `
<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>
<span>${repo.stargazers_pretty}</span>
<span>${repo.stargazerCount_pretty}</span>
</div>`;
}
if (repo.forks > 0) {
if (repo.forkCount > 0) {
html += `
<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>
<span>${repo.forks_pretty}</span>
<span>${repo.forkCount_pretty}</span>
</div>`;
}

View File

@ -1,32 +1,35 @@
"use strict";
const { GraphQLClient } = require("graphql-request");
const { GraphQLClient, gql } = require("graphql-request");
const { escape } = require("html-escaper");
const numeral = require("numeral");
const { DateTime } = require("luxon");
const username = "jakejarvis";
const endpoint = "https://api.github.com/graphql";
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: {
Authorization: `Bearer ${process.env.GH_PUBLIC_TOKEN}`,
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) {
user(login: "${username}") {
repositories(
last: $limit,
first: $limit,
isLocked: false,
isFork: false,
ownerAffiliations: OWNER,
privacy: PUBLIC,
orderBy: {
field: $sort,
direction: ASC
direction: DESC
}
) {
edges {
@ -35,12 +38,8 @@ async function fetchRepos(sort, limit) {
url
description
pushedAt
stargazers {
totalCount
}
forks {
totalCount
}
stargazerCount
forkCount
primaryLanguage {
name
color
@ -52,23 +51,17 @@ async function fetchRepos(sort, limit) {
}
`;
const response = await client.request(query, {
sort: sort,
limit: limit,
});
const response = await client.request(query, { sort, limit });
const currentRepos = response.user.repositories.edges.map(({ node: repo }) => ({
...repo,
description: escape(repo.description),
stargazers: repo.stargazers.totalCount,
stargazers_pretty: numeral(repo.stargazers.totalCount).format("0,0"),
forks: repo.forks.totalCount,
forks_pretty: numeral(repo.forks.totalCount).format("0,0"),
stargazerCount_pretty: numeral(repo.stargazerCount).format("0,0"),
forkCount_pretty: numeral(repo.forkCount).format("0,0"),
pushedAt_relative: DateTime.fromISO(repo.pushedAt).toRelative({ locale: "en" }),
}));
// reverse hack to get expected sorting
return currentRepos.reverse();
return currentRepos;
}
exports.handler = async (event) => {