Files
TheGame/packages/web/graphql/getPlayers.ts
δυς 2921a47ef0 Review: meTokens ₍wᵢₜₕ gᵣₐₙᵤₗₐᵣ 𝄴ₒₘₘᵢₜₛ₎ (#1552)
* linting 🃡

* removing MetaMask specificity 🃢

* moving `StrictMode` to Next.js 🃣

* compressing & commafying 🃤

* upgrading eslint 👘

* removing preface from guild name 🃥

* removing unnecessary ESLint `no-console` directives 🌂

* fixing a typo in a comment 🃦

* updating GraphQL codegen for paid subgraph 🦏

* replacing Discord invite link 📌

* passing through The Graph API token to Docker ♾

* setting Docker ARG to set ENV 📟

* missed a file rename in frontend Docker config 🦀

* adding ts-node to fix Docker build issue ⸙

* trying to narrow down the 500 error's source in the test instance 

* exposing The Graph API token on Cloud Run 🦃

* more logging to try & find server error 🐠

* more logging 🧱

* trying to run Node.js in development mode on Cloud Run 🎁

* reconfiguring frontend Dockerfile to also run the dev environment 🌿

* dev mode seems to function 🧨

* 768MiB wasn't enough memory 🍁

* 1GiB wasn't enough memory 🔱

* 1.5GiB was interpreted as 1GiB 🥃

* 1536MiB wasn't enough memory 👾

* 2GiB wasn't enough memory 🧲

* 3GiB wasn't enough memory 🆎

* 4GiB might have been enough, but it still doesn't load 🧻

* 5GiB requires two CPUs 📝

* giving up on dev server; unexplained HTTP 429s 🎨

* disabling Honeybadger in test instances 📮

* trying an `ErrorBoundary` to gather more info 🕷

* setting GraphQL endpoint 🇲🇰

* exposing environment variables ⛈

* trying to expose `` 📻

* the Next compiled version still references `node_modules` 🦢

* removing Alchemy API key from sources 

* trying a different Docker build action 💉

* removing logging 🍿

* switching to Docker Buildx 👠

* missed an escaped newline 🗿

* trying a newer gcloud setup action 🦝

* hopefully fixing authentication 📴

* bunch of changes to the meTokens profile section 🦜

* need credentials file 🐆

* hunting for layout load error & pushing debug statements to testing 🥁

* updating eslint 💓

* trying to debug the missing Breadchain Coop 🧀

* apparently chose the wrong changeset 🐚

* removing logging 🥀
2023-05-15 09:12:03 -06:00

195 lines
4.4 KiB
TypeScript

import {
GetPlayerFiltersDocument,
GetPlayerFiltersQuery,
GetPlayerFiltersQueryVariables,
GetPlayersDocument,
GetPlayersQuery,
GetPlayersQueryVariables,
GetPlayerUsernamesQuery,
GetPlayerUsernamesQueryVariables,
Maybe,
Order_By,
Player,
Player_Bool_Exp,
} from 'graphql/autogen/types';
import { client as defaultClient } from 'graphql/client';
import { PlayerFragment, PlayerSkillFragment } from 'graphql/fragments';
import { Client } from 'urql';
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
/* GraphQL */ `
query GetPlayers(
$orderBy: player_order_by!
$offset: Int
$limit: Int
$where: player_bool_exp
) {
player(
# players were appearing multiple times when orderBy was the
# same for many entries; i.e. availability = 0
order_by: [$orderBy, { ethereumAddress: desc }]
offset: $offset
limit: $limit
where: $where
) {
...PlayerFragment
}
player_aggregate(where: $where) {
aggregate {
count
}
}
}
${PlayerFragment}
`;
export const PLAYER_LIMIT = 9;
export const defaultQueryVariables: PlayersQueryVariables = {
offset: 0,
limit: PLAYER_LIMIT,
search: '%%',
orderBy: {
seasonXP: 'desc' as Order_By,
},
};
export type PlayersQueryVariables = {
search: string;
offset: number;
limit: number;
orderBy: {
seasonXP?: Maybe<Order_By>;
};
availability?: number;
skillIds?: Array<string>;
explorerTypeTitles?: Array<string>;
timeZones?: Array<string>;
};
export const transformToGraphQLVariables = (
query: PlayersQueryVariables,
): GetPlayersQueryVariables => {
const where: Player_Bool_Exp = {
_or: [
{ profile: { username: { _ilike: query.search } } },
{ ethereumAddress: { _ilike: query.search } },
],
};
if (query.availability != null) {
where.profile ??= {};
where.profile.availableHours = {
_gte: query.availability,
};
}
if (query.timeZones?.length) {
where.profile ??= {};
where.profile.timeZone = { _in: query.timeZones };
}
if (query.explorerTypeTitles?.length) {
where.profile ??= {};
where.profile.explorerTypeTitle = { _in: query.explorerTypeTitles };
}
if (query.skillIds?.length) {
where.skills = {
Skill: { id: { _in: query.skillIds } },
};
}
return {
orderBy: query.orderBy,
offset: query.offset,
limit: query.limit,
where,
};
};
export type PlayersResponse = {
error?: Error;
count: number;
players: Array<Player>;
};
export const getPlayersWithCount = async (
queryVariables = defaultQueryVariables,
client: Client = defaultClient,
): Promise<PlayersResponse> => {
const { data, error } = await client
.query<GetPlayersQuery, GetPlayersQueryVariables>(
GetPlayersDocument,
transformToGraphQLVariables(queryVariables),
)
.toPromise();
return {
players: (data?.player as Array<Player>) ?? [],
count: data?.player_aggregate.aggregate?.count ?? 0,
error,
};
};
const playerUsernamesQuery = /* GraphQL */ `
query GetPlayerUsernames($limit: Int) {
player(order_by: { totalXP: desc }, limit: $limit) {
ethereumAddress
profile {
username
}
}
}
`;
export const getPlayerUsernames = async (
limit = 150,
): Promise<Array<{ address: string; username: Maybe<string> }>> => {
const { data, error } = await defaultClient
.query<GetPlayerUsernamesQuery, GetPlayerUsernamesQueryVariables>(
playerUsernamesQuery,
{ limit },
)
.toPromise();
if (error) throw error;
return (data?.player ?? []).map(({ ethereumAddress: address, profile }) => ({
address,
username: profile?.username ?? null,
}));
};
export const getTopPlayerUsernames = getPlayerUsernames;
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
/* GraphQL */ `
query GetPlayerFilters {
skill_aggregate(distinct_on: category) {
nodes {
name: category
}
}
skill(
order_by: { Player_Skills_aggregate: { count: desc }, category: asc }
) {
...PlayerSkillFragment
}
ExplorerType(distinct_on: id) {
value: id
label: title
}
}
${PlayerSkillFragment}
`;
export const getPlayerFilters = async (client: Client = defaultClient) => {
const { data, error } = await client
.query<GetPlayerFiltersQuery, GetPlayerFiltersQueryVariables>(
GetPlayerFiltersDocument,
)
.toPromise();
if (error) throw new Error(error.message);
return data;
};