Simplify API code using cynic flatten

This commit is contained in:
Kiana Sheibani 2023-09-02 02:05:45 -04:00
parent 66c6155a53
commit 38e7b4a018
Signed by: toki
GPG key ID: 6CB106C25E86A9F7
2 changed files with 23 additions and 22 deletions

View file

@ -20,7 +20,8 @@ pub struct VideogameSearch {
#[derive(cynic::QueryFragment, Debug)] #[derive(cynic::QueryFragment, Debug)]
struct VideogameConnection { struct VideogameConnection {
nodes: Option<Vec<Option<Videogame>>>, #[cynic(flatten)]
nodes: Vec<Videogame>,
} }
#[derive(cynic::QueryFragment, Debug)] #[derive(cynic::QueryFragment, Debug)]
@ -51,13 +52,12 @@ impl<'a> QueryUnwrap<VideogameSearchVars<'a>> for VideogameSearch {
response response
.data? .data?
.videogames? .videogames?
.nodes? .nodes
.into_iter() .into_iter()
.filter_map(|game| { .filter_map(|game| {
let game_ = game?;
Some(VideogameResponse { Some(VideogameResponse {
id: game_.id?, id: game.id?,
name: game_.name?, name: game.name?,
}) })
}) })
.collect(), .collect(),

View file

@ -37,7 +37,8 @@ pub struct TournamentSets {
#[derive(cynic::QueryFragment, Debug)] #[derive(cynic::QueryFragment, Debug)]
#[cynic(variables = "TournamentSetsVars")] #[cynic(variables = "TournamentSetsVars")]
struct TournamentConnection { struct TournamentConnection {
nodes: Option<Vec<Option<Tournament>>>, #[cynic(flatten)]
nodes: Vec<Tournament>,
} }
#[derive(cynic::QueryFragment, Debug)] #[derive(cynic::QueryFragment, Debug)]
@ -45,7 +46,8 @@ struct TournamentConnection {
struct Tournament { struct Tournament {
name: Option<String>, name: Option<String>,
#[arguments(limit: 1000, filter: { videogameId: [$game_id] })] #[arguments(limit: 1000, filter: { videogameId: [$game_id] })]
events: Option<Vec<Option<Event>>>, #[cynic(flatten)]
events: Vec<Event>,
} }
#[derive(cynic::QueryFragment, Debug)] #[derive(cynic::QueryFragment, Debug)]
@ -56,13 +58,15 @@ struct Event {
#[derive(cynic::QueryFragment, Debug)] #[derive(cynic::QueryFragment, Debug)]
struct SetConnection { struct SetConnection {
nodes: Option<Vec<Option<Set>>>, #[cynic(flatten)]
nodes: Vec<Set>,
} }
#[derive(cynic::QueryFragment, Debug)] #[derive(cynic::QueryFragment, Debug)]
struct Set { struct Set {
#[arguments(includeByes: true)] #[arguments(includeByes: true)]
slots: Option<Vec<Option<SetSlot>>>, #[cynic(flatten)]
slots: Vec<SetSlot>,
winner_id: Option<i32>, winner_id: Option<i32>,
} }
@ -105,26 +109,23 @@ impl<'a> QueryUnwrap<TournamentSetsVars<'a>> for TournamentSets {
response response
.data? .data?
.tournaments? .tournaments?
.nodes? .nodes
.into_iter() .into_iter()
.filter_map(|tour| { .filter_map(|tour| {
let tour_ = tour?; let sets = tour
let sets = tour_ .events
.events?
.into_iter() .into_iter()
.filter_map(|event| { .filter_map(|event| {
let event_ = event?;
Some( Some(
event_ event
.sets? .sets?
.nodes? .nodes
.into_iter() .into_iter()
.filter_map(|set| { .filter_map(|set| {
let set_ = set?; let slots = set.slots;
let slots = set_.slots?; let player1 = (&slots[0]).entrant.as_ref()?.id?;
let player1 = (&slots[0]).as_ref()?.entrant.as_ref()?.id?; let player2 = (&slots[0]).entrant.as_ref()?.id?;
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, player1,
player2, player2,
@ -137,7 +138,7 @@ impl<'a> QueryUnwrap<TournamentSetsVars<'a>> for TournamentSets {
.flatten() .flatten()
.collect(); .collect();
Some(TournamentResponse { Some(TournamentResponse {
name: tour_.name?, name: tour.name?,
sets, sets,
}) })
}) })