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)]
struct VideogameConnection {
nodes: Option<Vec<Option<Videogame>>>,
#[cynic(flatten)]
nodes: Vec<Videogame>,
}
#[derive(cynic::QueryFragment, Debug)]
@ -51,13 +52,12 @@ impl<'a> QueryUnwrap<VideogameSearchVars<'a>> 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(),

View file

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