StartRNR/cli/src/queries/player_info.rs

45 lines
997 B
Rust
Raw Normal View History

2023-09-02 20:46:23 -04:00
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<Player>,
}
#[derive(cynic::QueryFragment, Debug)]
struct Player {
gamer_tag: Option<String>,
prefix: Option<String>,
}
// Unwrapping
pub struct PlayerInfoResponse {
2023-09-02 21:27:21 -04:00
pub name: Option<String>,
2023-09-02 20:46:23 -04:00
pub prefix: Option<String>,
}
impl QueryUnwrap<PlayerInfoVars> for PlayerInfo {
type Unwrapped = PlayerInfoResponse;
fn unwrap_response(response: GraphQlResponse<PlayerInfo>) -> Option<PlayerInfoResponse> {
let player = response.data?.player?;
Some(PlayerInfoResponse {
2023-09-02 21:27:21 -04:00
name: player.gamer_tag,
2023-09-02 20:46:23 -04:00
prefix: player.prefix.filter(|pr| !pr.is_empty()),
})
}
}