From 38e7b4a018e42dae91f0496d99ffa06bf1e01cae Mon Sep 17 00:00:00 2001 From: Kiana Sheibani Date: Sat, 2 Sep 2023 02:05:45 -0400 Subject: [PATCH] Simplify API code using cynic flatten --- cli/src/queries/search_games.rs | 10 ++++----- cli/src/queries/tournament_sets.rs | 35 +++++++++++++++--------------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/cli/src/queries/search_games.rs b/cli/src/queries/search_games.rs index 0b0e931..8658aaf 100644 --- a/cli/src/queries/search_games.rs +++ b/cli/src/queries/search_games.rs @@ -20,7 +20,8 @@ pub struct VideogameSearch { #[derive(cynic::QueryFragment, Debug)] struct VideogameConnection { - nodes: Option>>, + #[cynic(flatten)] + nodes: Vec, } #[derive(cynic::QueryFragment, Debug)] @@ -51,13 +52,12 @@ impl<'a> QueryUnwrap> for VideogameSearch { response .data? .videogames? - .nodes? + .nodes .into_iter() .filter_map(|game| { - let game_ = game?; Some(VideogameResponse { - id: game_.id?, - name: game_.name?, + id: game.id?, + name: game.name?, }) }) .collect(), diff --git a/cli/src/queries/tournament_sets.rs b/cli/src/queries/tournament_sets.rs index 7d849a9..e8e63df 100644 --- a/cli/src/queries/tournament_sets.rs +++ b/cli/src/queries/tournament_sets.rs @@ -37,7 +37,8 @@ pub struct TournamentSets { #[derive(cynic::QueryFragment, Debug)] #[cynic(variables = "TournamentSetsVars")] struct TournamentConnection { - nodes: Option>>, + #[cynic(flatten)] + nodes: Vec, } #[derive(cynic::QueryFragment, Debug)] @@ -45,7 +46,8 @@ struct TournamentConnection { struct Tournament { name: Option, #[arguments(limit: 1000, filter: { videogameId: [$game_id] })] - events: Option>>, + #[cynic(flatten)] + events: Vec, } #[derive(cynic::QueryFragment, Debug)] @@ -56,13 +58,15 @@ struct Event { #[derive(cynic::QueryFragment, Debug)] struct SetConnection { - nodes: Option>>, + #[cynic(flatten)] + nodes: Vec, } #[derive(cynic::QueryFragment, Debug)] struct Set { #[arguments(includeByes: true)] - slots: Option>>, + #[cynic(flatten)] + slots: Vec, winner_id: Option, } @@ -105,26 +109,23 @@ impl<'a> QueryUnwrap> for TournamentSets { response .data? .tournaments? - .nodes? + .nodes .into_iter() .filter_map(|tour| { - let tour_ = tour?; - let sets = tour_ - .events? + let sets = tour + .events .into_iter() .filter_map(|event| { - let event_ = event?; Some( - event_ + event .sets? - .nodes? + .nodes .into_iter() .filter_map(|set| { - let set_ = set?; - let slots = set_.slots?; - 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; + let slots = set.slots; + let player1 = (&slots[0]).entrant.as_ref()?.id?; + let player2 = (&slots[0]).entrant.as_ref()?.id?; + let winner = set.winner_id? as u64; Some(SetResponse { player1, player2, @@ -137,7 +138,7 @@ impl<'a> QueryUnwrap> for TournamentSets { .flatten() .collect(); Some(TournamentResponse { - name: tour_.name?, + name: tour.name?, sets, }) })