Hide implementation details of query

This commit is contained in:
Kiana Sheibani 2023-09-02 19:48:47 -04:00
parent 6287c325ef
commit abd9f947ab
Signed by: toki
GPG key ID: 6CB106C25E86A9F7
2 changed files with 44 additions and 43 deletions

View file

@ -9,27 +9,6 @@ pub use tournament_sets::*;
use schema::schema; use schema::schema;
// Types
// HACK: Unfortunately, start.gg seems to use integers for its ID type, whereas
// cynic always assumes that IDs are strings. To get around that, we define new
// scalar types that deserialize to u64.
#[derive(cynic::Scalar, Debug, Copy, Clone)]
#[cynic(graphql_type = "ID")]
pub struct VideogameId(pub u64);
#[derive(cynic::Scalar, Debug, Copy, Clone)]
#[cynic(graphql_type = "ID")]
pub struct EntrantId(pub u64);
#[derive(cynic::Scalar, Debug, Copy, Clone)]
#[cynic(graphql_type = "ID")]
pub struct PlayerId(pub u64);
#[derive(cynic::Scalar, Debug, Clone)]
pub struct Timestamp(pub u64);
// Auth key // Auth key
pub fn get_auth_key(config_dir: &Path) -> Option<String> { pub fn get_auth_key(config_dir: &Path) -> Option<String> {
@ -54,6 +33,27 @@ pub fn get_auth_key(config_dir: &Path) -> Option<String> {
} }
} }
// Types
// HACK: Unfortunately, start.gg seems to use integers for its ID type, whereas
// cynic always assumes that IDs are strings. To get around that, we define new
// scalar types that deserialize to u64.
#[derive(cynic::Scalar, Debug, Copy, Clone)]
#[cynic(graphql_type = "ID")]
pub struct VideogameId(pub u64);
#[derive(cynic::Scalar, Debug, Copy, Clone)]
#[cynic(graphql_type = "ID")]
pub struct EntrantId(pub u64);
#[derive(cynic::Scalar, Debug, Copy, Clone)]
#[cynic(graphql_type = "ID")]
pub struct PlayerId(pub u64);
#[derive(cynic::Scalar, Debug, Clone)]
pub struct Timestamp(pub u64);
// Query machinery // Query machinery
pub trait QueryUnwrap<Vars>: 'static + QueryBuilder<Vars> { pub trait QueryUnwrap<Vars>: 'static + QueryBuilder<Vars> {

View file

@ -8,6 +8,7 @@ use schema::schema;
pub struct TournamentSetsVars<'a> { pub struct TournamentSetsVars<'a> {
// HACK: This should really be an optional variable, but there seems to be a // HACK: This should really be an optional variable, but there seems to be a
// server-side bug that completely breaks everything when this isn't passed. // server-side bug that completely breaks everything when this isn't passed.
// We can use a dummy value of 1 when we don't want to filter by time.
pub last_query: Timestamp, pub last_query: Timestamp,
pub game_id: VideogameId, pub game_id: VideogameId,
@ -31,65 +32,65 @@ pub struct TournamentSets {
countryCode: $country, countryCode: $country,
addrState: $state addrState: $state
}})] }})]
pub tournaments: Option<TournamentConnection>, tournaments: Option<TournamentConnection>,
} }
#[derive(cynic::QueryFragment, Debug)] #[derive(cynic::QueryFragment, Debug)]
#[cynic(variables = "TournamentSetsVars")] #[cynic(variables = "TournamentSetsVars")]
pub struct TournamentConnection { struct TournamentConnection {
#[cynic(flatten)] #[cynic(flatten)]
pub nodes: Vec<Tournament>, nodes: Vec<Tournament>,
} }
#[derive(cynic::QueryFragment, Debug)] #[derive(cynic::QueryFragment, Debug)]
#[cynic(variables = "TournamentSetsVars")] #[cynic(variables = "TournamentSetsVars")]
pub struct Tournament { struct Tournament {
pub name: Option<String>, name: Option<String>,
#[arguments(limit: 99999, filter: { videogameId: [$game_id] })] #[arguments(limit: 99999, filter: { videogameId: [$game_id] })]
#[cynic(flatten)] #[cynic(flatten)]
pub events: Vec<Event>, events: Vec<Event>,
} }
#[derive(cynic::QueryFragment, Debug)] #[derive(cynic::QueryFragment, Debug)]
pub struct Event { struct Event {
#[arguments(page: 1, perPage: 999)] #[arguments(page: 1, perPage: 999)]
pub sets: Option<SetConnection>, sets: Option<SetConnection>,
} }
#[derive(cynic::QueryFragment, Debug)] #[derive(cynic::QueryFragment, Debug)]
pub struct SetConnection { struct SetConnection {
#[cynic(flatten)] #[cynic(flatten)]
pub nodes: Vec<Set>, nodes: Vec<Set>,
} }
#[derive(cynic::QueryFragment, Debug)] #[derive(cynic::QueryFragment, Debug)]
pub struct Set { struct Set {
#[arguments(includeByes: true)] #[arguments(includeByes: true)]
#[cynic(flatten)] #[cynic(flatten)]
pub slots: Vec<SetSlot>, slots: Vec<SetSlot>,
pub winner_id: Option<i32>, winner_id: Option<i32>,
} }
#[derive(cynic::QueryFragment, Debug)] #[derive(cynic::QueryFragment, Debug)]
pub struct SetSlot { struct SetSlot {
pub entrant: Option<Entrant>, entrant: Option<Entrant>,
} }
#[derive(cynic::QueryFragment, Debug)] #[derive(cynic::QueryFragment, Debug)]
pub struct Entrant { struct Entrant {
pub id: Option<EntrantId>, id: Option<EntrantId>,
#[cynic(flatten)] #[cynic(flatten)]
pub participants: Vec<Participant>, participants: Vec<Participant>,
} }
#[derive(cynic::QueryFragment, Debug)] #[derive(cynic::QueryFragment, Debug)]
pub struct Participant { struct Participant {
pub player: Option<Player>, player: Option<Player>,
} }
#[derive(cynic::QueryFragment, Debug)] #[derive(cynic::QueryFragment, Debug)]
pub struct Player { struct Player {
pub id: Option<PlayerId>, id: Option<PlayerId>,
} }
// Unwrap // Unwrap