From 9d5ff8f125cb11967e32900159e5b2a01dd85664 Mon Sep 17 00:00:00 2001 From: Kiana Sheibani Date: Sat, 2 Sep 2023 20:46:23 -0400 Subject: [PATCH] Write player info query --- cli/src/queries.rs | 2 ++ cli/src/queries/player_info.rs | 49 ++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 cli/src/queries/player_info.rs diff --git a/cli/src/queries.rs b/cli/src/queries.rs index 1b52402..edea877 100644 --- a/cli/src/queries.rs +++ b/cli/src/queries.rs @@ -6,6 +6,8 @@ pub mod search_games; pub use search_games::*; pub mod tournament_sets; pub use tournament_sets::*; +pub mod player_info; +pub use player_info::*; use schema::schema; diff --git a/cli/src/queries/player_info.rs b/cli/src/queries/player_info.rs new file mode 100644 index 0000000..debe942 --- /dev/null +++ b/cli/src/queries/player_info.rs @@ -0,0 +1,49 @@ +use super::{PlayerId, QueryUnwrap}; +use cynic::GraphQlResponse; +use schema::schema; + +// Variables + +#[derive(cynic::QueryVariables, Debug)] +pub struct PlayerInfoVars { + pub id: PlayerId, +} + +// Query + +#[derive(cynic::QueryFragment, Debug)] +#[cynic(graphql_type = "Query", variables = "PlayerInfoVars")] +pub struct PlayerInfo { + #[arguments(id: $id)] + player: Option, +} + +#[derive(cynic::QueryFragment, Debug)] +struct Player { + gamer_tag: Option, + prefix: Option, +} + +// Unwrapping + +pub struct PlayerInfoResponse { + pub name: String, + pub prefix: Option, +} + +impl QueryUnwrap for PlayerInfo { + type VarsUnwrapped = PlayerInfoVars; + type Unwrapped = PlayerInfoResponse; + + fn wrap_vars(vars: PlayerInfoVars) -> PlayerInfoVars { + vars + } + + fn unwrap_response(response: GraphQlResponse) -> Option { + let player = response.data?.player?; + Some(PlayerInfoResponse { + name: player.gamer_tag?, + prefix: player.prefix.filter(|pr| !pr.is_empty()), + }) + } +}