From 66c6155a53192c7ff07a5e3fe3dac4645b5734d1 Mon Sep 17 00:00:00 2001 From: Kiana Sheibani Date: Sat, 2 Sep 2023 01:55:14 -0400 Subject: [PATCH] Get rid of generic ID scalar type --- cli/src/queries.rs | 14 ++++---- cli/src/queries/search_games.rs | 6 ++-- cli/src/queries/tournament_sets.rs | 55 ++++++++++-------------------- 3 files changed, 28 insertions(+), 47 deletions(-) diff --git a/cli/src/queries.rs b/cli/src/queries.rs index b2dd13a..1dd9eae 100644 --- a/cli/src/queries.rs +++ b/cli/src/queries.rs @@ -12,15 +12,15 @@ use schema::schema; // Types // 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 -// new scalar type that serializes to u64. -#[derive(cynic::Scalar, Debug, Copy, Clone)] -pub struct ID(pub u64); +// cynic always assumes that IDs are strings. To get around that, we define new +// scalar types that deserialize to u64. -// Wrapper types to differentiate between different types of IDs -#[derive(Debug, Copy, Clone)] +#[derive(cynic::Scalar, Debug, Copy, Clone)] +#[cynic(graphql_type = "ID")] pub struct VideogameId(pub u64); -#[derive(Debug, Copy, Clone)] + +#[derive(cynic::Scalar, Debug, Copy, Clone)] +#[cynic(graphql_type = "ID")] pub struct EntrantId(pub u64); #[derive(cynic::Scalar, Debug, Clone)] diff --git a/cli/src/queries/search_games.rs b/cli/src/queries/search_games.rs index b90fab4..0b0e931 100644 --- a/cli/src/queries/search_games.rs +++ b/cli/src/queries/search_games.rs @@ -1,4 +1,4 @@ -use super::{QueryUnwrap, VideogameId, ID}; +use super::{QueryUnwrap, VideogameId}; use cynic::GraphQlResponse; use schema::schema; @@ -25,7 +25,7 @@ struct VideogameConnection { #[derive(cynic::QueryFragment, Debug)] struct Videogame { - id: Option, + id: Option, name: Option, } @@ -56,7 +56,7 @@ impl<'a> QueryUnwrap> for VideogameSearch { .filter_map(|game| { let game_ = game?; Some(VideogameResponse { - id: VideogameId(game_.id?.0), + id: game_.id?, name: game_.name?, }) }) diff --git a/cli/src/queries/tournament_sets.rs b/cli/src/queries/tournament_sets.rs index 05ca4e1..7d849a9 100644 --- a/cli/src/queries/tournament_sets.rs +++ b/cli/src/queries/tournament_sets.rs @@ -1,24 +1,24 @@ -use super::{EntrantId, QueryUnwrap, Timestamp, VideogameId, ID}; +use super::{EntrantId, QueryUnwrap, Timestamp, VideogameId}; use cynic::GraphQlResponse; use schema::schema; // Variables #[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 // server-side bug that completely breaks everything when this isn't passed. - last_query: Timestamp, + pub last_query: Timestamp, - country: Option<&'a str>, - game_id: ID, - state: Option<&'a str>, + pub country: Option<&'a str>, + pub game_id: VideogameId, + pub state: Option<&'a str>, } // Query #[derive(cynic::QueryFragment, Debug)] -#[cynic(graphql_type = "Query", variables = "TournamentSetsVarsRaw")] +#[cynic(graphql_type = "Query", variables = "TournamentSetsVars")] pub struct TournamentSets { #[arguments(query: { page: 1, @@ -35,13 +35,13 @@ pub struct TournamentSets { } #[derive(cynic::QueryFragment, Debug)] -#[cynic(variables = "TournamentSetsVarsRaw")] +#[cynic(variables = "TournamentSetsVars")] struct TournamentConnection { nodes: Option>>, } #[derive(cynic::QueryFragment, Debug)] -#[cynic(variables = "TournamentSetsVarsRaw")] +#[cynic(variables = "TournamentSetsVars")] struct Tournament { name: Option, #[arguments(limit: 1000, filter: { videogameId: [$game_id] })] @@ -73,18 +73,11 @@ struct SetSlot { #[derive(cynic::QueryFragment, Debug)] struct Entrant { - id: Option, + id: Option, } // 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 name: String, pub sets: Vec, @@ -96,24 +89,12 @@ pub struct SetResponse { pub winner: bool, } -impl<'a> QueryUnwrap> for TournamentSets { +impl<'a> QueryUnwrap> for TournamentSets { type VarsUnwrapped = TournamentSetsVars<'a>; type Unwrapped = Vec; - fn wrap_vars( - TournamentSetsVars { - last_query, - game_id: VideogameId(game_id), - country, - state, - }: TournamentSetsVars, - ) -> TournamentSetsVarsRaw { - TournamentSetsVarsRaw { - last_query, - game_id: ID(game_id), - country, - state, - } + fn wrap_vars(vars: TournamentSetsVars) -> TournamentSetsVars { + vars } // This might be the most spaghetti code I've ever written @@ -141,13 +122,13 @@ impl<'a> QueryUnwrap> for TournamentSets { .filter_map(|set| { let set_ = set?; let slots = set_.slots?; - let player1 = (&slots[0]).as_ref()?.entrant.as_ref()?.id?.0; - let player2 = (&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?; let winner = set_.winner_id? as u64; Some(SetResponse { - player1: EntrantId(player1), - player2: EntrantId(player2), - winner: winner == player2, + player1, + player2, + winner: winner == player2.0, }) }) .collect::>(),