Get rid of generic ID scalar type

This commit is contained in:
Kiana Sheibani 2023-09-02 01:55:14 -04:00
parent 003fde0f1e
commit 66c6155a53
Signed by: toki
GPG key ID: 6CB106C25E86A9F7
3 changed files with 28 additions and 47 deletions

View file

@ -12,15 +12,15 @@ use schema::schema;
// Types // Types
// HACK: Unfortunately, start.gg seems to use integers for its ID type, whereas // HACK: Unfortunately, start.gg seems to use integers for its ID type, whereas
// cynic always assumes that IDs are strings. To get around that, we define a // cynic always assumes that IDs are strings. To get around that, we define new
// new scalar type that serializes to u64. // scalar types that deserialize to u64.
#[derive(cynic::Scalar, Debug, Copy, Clone)]
pub struct ID(pub u64);
// Wrapper types to differentiate between different types of IDs #[derive(cynic::Scalar, Debug, Copy, Clone)]
#[derive(Debug, Copy, Clone)] #[cynic(graphql_type = "ID")]
pub struct VideogameId(pub u64); pub struct VideogameId(pub u64);
#[derive(Debug, Copy, Clone)]
#[derive(cynic::Scalar, Debug, Copy, Clone)]
#[cynic(graphql_type = "ID")]
pub struct EntrantId(pub u64); pub struct EntrantId(pub u64);
#[derive(cynic::Scalar, Debug, Clone)] #[derive(cynic::Scalar, Debug, Clone)]

View file

@ -1,4 +1,4 @@
use super::{QueryUnwrap, VideogameId, ID}; use super::{QueryUnwrap, VideogameId};
use cynic::GraphQlResponse; use cynic::GraphQlResponse;
use schema::schema; use schema::schema;
@ -25,7 +25,7 @@ struct VideogameConnection {
#[derive(cynic::QueryFragment, Debug)] #[derive(cynic::QueryFragment, Debug)]
struct Videogame { struct Videogame {
id: Option<ID>, id: Option<VideogameId>,
name: Option<String>, name: Option<String>,
} }
@ -56,7 +56,7 @@ impl<'a> QueryUnwrap<VideogameSearchVars<'a>> for VideogameSearch {
.filter_map(|game| { .filter_map(|game| {
let game_ = game?; let game_ = game?;
Some(VideogameResponse { Some(VideogameResponse {
id: VideogameId(game_.id?.0), id: game_.id?,
name: game_.name?, name: game_.name?,
}) })
}) })

View file

@ -1,24 +1,24 @@
use super::{EntrantId, QueryUnwrap, Timestamp, VideogameId, ID}; use super::{EntrantId, QueryUnwrap, Timestamp, VideogameId};
use cynic::GraphQlResponse; use cynic::GraphQlResponse;
use schema::schema; use schema::schema;
// Variables // Variables
#[derive(cynic::QueryVariables, Debug)] #[derive(cynic::QueryVariables, Debug)]
pub struct TournamentSetsVarsRaw<'a> { pub struct TournamentSetsVars<'a> {
// HACK: This should really be an optional variable, but there seems to be a // HACK: This should really be an optional variable, but there seems to be a
// server-side bug that completely breaks everything when this isn't passed. // server-side bug that completely breaks everything when this isn't passed.
last_query: Timestamp, pub last_query: Timestamp,
country: Option<&'a str>, pub country: Option<&'a str>,
game_id: ID, pub game_id: VideogameId,
state: Option<&'a str>, pub state: Option<&'a str>,
} }
// Query // Query
#[derive(cynic::QueryFragment, Debug)] #[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Query", variables = "TournamentSetsVarsRaw")] #[cynic(graphql_type = "Query", variables = "TournamentSetsVars")]
pub struct TournamentSets { pub struct TournamentSets {
#[arguments(query: { #[arguments(query: {
page: 1, page: 1,
@ -35,13 +35,13 @@ pub struct TournamentSets {
} }
#[derive(cynic::QueryFragment, Debug)] #[derive(cynic::QueryFragment, Debug)]
#[cynic(variables = "TournamentSetsVarsRaw")] #[cynic(variables = "TournamentSetsVars")]
struct TournamentConnection { struct TournamentConnection {
nodes: Option<Vec<Option<Tournament>>>, nodes: Option<Vec<Option<Tournament>>>,
} }
#[derive(cynic::QueryFragment, Debug)] #[derive(cynic::QueryFragment, Debug)]
#[cynic(variables = "TournamentSetsVarsRaw")] #[cynic(variables = "TournamentSetsVars")]
struct Tournament { struct Tournament {
name: Option<String>, name: Option<String>,
#[arguments(limit: 1000, filter: { videogameId: [$game_id] })] #[arguments(limit: 1000, filter: { videogameId: [$game_id] })]
@ -73,18 +73,11 @@ struct SetSlot {
#[derive(cynic::QueryFragment, Debug)] #[derive(cynic::QueryFragment, Debug)]
struct Entrant { struct Entrant {
id: Option<ID>, id: Option<EntrantId>,
} }
// Unwrap // Unwrap
pub struct TournamentSetsVars<'a> {
pub last_query: Timestamp,
pub game_id: VideogameId,
pub country: Option<&'a str>,
pub state: Option<&'a str>,
}
pub struct TournamentResponse { pub struct TournamentResponse {
pub name: String, pub name: String,
pub sets: Vec<SetResponse>, pub sets: Vec<SetResponse>,
@ -96,24 +89,12 @@ pub struct SetResponse {
pub winner: bool, pub winner: bool,
} }
impl<'a> QueryUnwrap<TournamentSetsVarsRaw<'a>> for TournamentSets { impl<'a> QueryUnwrap<TournamentSetsVars<'a>> for TournamentSets {
type VarsUnwrapped = TournamentSetsVars<'a>; type VarsUnwrapped = TournamentSetsVars<'a>;
type Unwrapped = Vec<TournamentResponse>; type Unwrapped = Vec<TournamentResponse>;
fn wrap_vars( fn wrap_vars(vars: TournamentSetsVars) -> TournamentSetsVars {
TournamentSetsVars { vars
last_query,
game_id: VideogameId(game_id),
country,
state,
}: TournamentSetsVars,
) -> TournamentSetsVarsRaw {
TournamentSetsVarsRaw {
last_query,
game_id: ID(game_id),
country,
state,
}
} }
// This might be the most spaghetti code I've ever written // This might be the most spaghetti code I've ever written
@ -141,13 +122,13 @@ impl<'a> QueryUnwrap<TournamentSetsVarsRaw<'a>> for TournamentSets {
.filter_map(|set| { .filter_map(|set| {
let set_ = set?; let set_ = set?;
let slots = set_.slots?; let slots = set_.slots?;
let player1 = (&slots[0]).as_ref()?.entrant.as_ref()?.id?.0; let player1 = (&slots[0]).as_ref()?.entrant.as_ref()?.id?;
let player2 = (&slots[0]).as_ref()?.entrant.as_ref()?.id?.0; let player2 = (&slots[0]).as_ref()?.entrant.as_ref()?.id?;
let winner = set_.winner_id? as u64; let winner = set_.winner_id? as u64;
Some(SetResponse { Some(SetResponse {
player1: EntrantId(player1), player1,
player2: EntrantId(player2), player2,
winner: winner == player2, winner: winner == player2.0,
}) })
}) })
.collect::<Vec<_>>(), .collect::<Vec<_>>(),