Skip to content

Commit

Permalink
feat: add table backed skin module
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e committed Jan 9, 2025
1 parent 4d00a77 commit 376cde0
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/graphql/operations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import proposal from './proposal';
import proposals from './proposals';
import ranking from './ranking';
import roles from './roles';
import skin from './skin';
import skins from './skins';
import space from './space';
import spaces from './spaces';
Expand All @@ -35,6 +36,7 @@ export default {
aliases,
follows,
subscriptions,
skin,
skins,
networks,
options,
Expand Down
11 changes: 11 additions & 0 deletions src/graphql/operations/skin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import skins from './skins';

export default async function (parent, args) {
const results = await skins(parent, {
first: 1,
skip: 0,
where: { id: args.id }
});

return results[0] || null;
}
50 changes: 40 additions & 10 deletions src/graphql/operations/skins.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
import { spaces } from '../../helpers/spaces';
import { capture } from '@snapshot-labs/snapshot-sentry';
import log from '../../helpers/log';
import db from '../../helpers/mysql';
import { buildWhereQuery, checkLimits } from '../helpers';

export default function () {
const skins = {};
Object.values(spaces).forEach((space: any) => {
if (space.skin)
skins[space.skin] = skins[space.skin] ? skins[space.skin] + 1 : 1;
const COLORS = ['bg', 'link', 'text', 'border', 'heading', 'primary'];

function formatSkins(queryResults) {
return queryResults.map(skin => {
skin.colors = Object.fromEntries(
COLORS.map(color => [color, `#${skin[color]}`])
);
return skin;
});
return Object.entries(skins).map(skin => ({
id: skin[0],
spacesCount: skin[1]
}));
}

export default async function (parent, args) {
const { first, skip, where = {} } = args;

checkLimits(args, 'skins');

const fields = {
id: 'string'
};
const whereQuery = buildWhereQuery(fields, 's', where);
const queryStr = whereQuery.query;
const params: any[] = whereQuery.params;

const query = `
SELECT s.* FROM skins s
WHERE id IS NOT NULL ${queryStr}
ORDER BY id ASC LIMIT ?, ?
`;
params.push(skip, first);

try {
return formatSkins(await db.queryAsync(query, params));
} catch (e: any) {
log.error(`[graphql] skins, ${JSON.stringify(e)}`);
capture(e, { args });
return Promise.reject(new Error('request failed'));
}
}
27 changes: 26 additions & 1 deletion src/graphql/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,14 @@ type Query {

statement(id: String!): Statement

skins: [Item]

skin(id: String!): Skin

skins(
first: Int! = 20
skip: Int! = 0,
where: SkinWhere
): [Skin]

networks: [Item]

Expand Down Expand Up @@ -379,6 +386,10 @@ input LeaderboardsWhere {
vote_count_lte: [Int]
}

input SkinWhere {
id: String
}

enum OrderDirection {
asc
desc
Expand Down Expand Up @@ -651,3 +662,17 @@ type Option {
name: String
value: String
}

type SkinColors {
bg: String
link: String
text: String
border: String
heading: String
primary: String
}

type Skin {
id: String!
colors: SkinColors
}

0 comments on commit 376cde0

Please sign in to comment.