Kiana Sheibani
dd4f0838ab
Somehow I missed that this was a thing you could do? It's a much cleaner organization, and it makes it so that you don't have to explicitly specify the crate to build.
49 lines
1 KiB
Rust
49 lines
1 KiB
Rust
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 {
|
|
id: Option<PlayerId>,
|
|
gamer_tag: Option<String>,
|
|
prefix: Option<String>,
|
|
}
|
|
|
|
// Unwrapping
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct PlayerData {
|
|
pub id: PlayerId,
|
|
pub name: Option<String>,
|
|
pub prefix: Option<String>,
|
|
}
|
|
|
|
impl QueryUnwrap<PlayerInfoVars> for PlayerInfo {
|
|
type Unwrapped = PlayerData;
|
|
|
|
fn unwrap_response(response: GraphQlResponse<PlayerInfo>) -> Option<PlayerData> {
|
|
let player = response.data?.player?;
|
|
Some(PlayerData {
|
|
id: player.id?,
|
|
name: player.gamer_tag,
|
|
prefix: player.prefix.filter(|pr| !pr.is_empty()),
|
|
})
|
|
}
|
|
}
|