StartRNR/src/queries/player_info.rs

57 lines
1.2 KiB
Rust
Raw Normal View History

2023-09-02 20:46:23 -04:00
use super::{PlayerId, QueryUnwrap};
use cynic::GraphQlResponse;
use schema::schema;
// Variables
2023-10-02 20:34:54 -04:00
#[derive(cynic::QueryVariables, Debug, Copy, Clone)]
2023-09-02 20:46:23 -04:00
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 {
2023-09-23 02:35:24 -04:00
id: Option<PlayerId>,
2023-09-02 20:46:23 -04:00
gamer_tag: Option<String>,
prefix: Option<String>,
2023-11-26 14:25:14 -05:00
user: Option<User>,
}
#[derive(cynic::QueryFragment, Debug)]
struct User {
slug: Option<String>,
2023-09-02 20:46:23 -04:00
}
// Unwrapping
2023-09-23 02:35:24 -04:00
#[derive(Debug, Clone)]
2023-09-02 23:25:17 -04:00
pub struct PlayerData {
2023-09-23 02:35:24 -04:00
pub id: PlayerId,
2023-11-26 14:25:14 -05:00
pub name: String,
2023-09-02 20:46:23 -04:00
pub prefix: Option<String>,
2023-11-26 14:25:14 -05:00
pub slug: String,
2023-09-02 20:46:23 -04:00
}
impl QueryUnwrap<PlayerInfoVars> for PlayerInfo {
2023-09-02 23:25:17 -04:00
type Unwrapped = PlayerData;
2023-09-02 20:46:23 -04:00
2023-09-02 23:25:17 -04:00
fn unwrap_response(response: GraphQlResponse<PlayerInfo>) -> Option<PlayerData> {
2023-09-02 20:46:23 -04:00
let player = response.data?.player?;
2023-09-02 23:25:17 -04:00
Some(PlayerData {
2023-09-23 02:35:24 -04:00
id: player.id?,
2023-11-26 14:25:14 -05:00
name: player.gamer_tag?,
2023-09-02 20:46:23 -04:00
prefix: player.prefix.filter(|pr| !pr.is_empty()),
2023-11-26 14:25:14 -05:00
slug: player.user?.slug?,
2023-09-02 20:46:23 -04:00
})
}
}