Generalize query running function

This commit is contained in:
Kiana Sheibani 2023-08-27 03:52:38 -04:00
parent 17c139f694
commit edcef12c2f
Signed by: toki
GPG key ID: 6CB106C25E86A9F7
3 changed files with 25 additions and 17 deletions

0
cli/src/dataset.rs Normal file
View file

View file

@ -3,7 +3,8 @@ use std::path::Path;
use futures::executor::block_on; use futures::executor::block_on;
mod queries; mod queries;
use queries::search_games::{VideogameSearch, VideogameSearchVars}; use queries::*;
use search_games::{VideogameSearch, VideogameSearchVars};
fn get_auth_key(config_dir: &Path) -> Option<String> { fn get_auth_key(config_dir: &Path) -> Option<String> {
@ -23,21 +24,6 @@ fn get_auth_key(config_dir: &Path) -> Option<String> {
} }
} }
async fn run_query(name: &str, auth: &str) -> cynic::GraphQlResponse<VideogameSearch> {
use cynic::http::SurfExt;
use cynic::QueryBuilder;
let query = VideogameSearch::build(VideogameSearchVars {
name: String::from(name)
});
let response = surf::post("https://api.start.gg/gql/alpha")
.header("Authorization", String::from("Bearer ") + auth)
.run_graphql(query)
.await;
response.unwrap()
}
fn main() { fn main() {
let mut config_dir = dirs::config_dir().unwrap(); let mut config_dir = dirs::config_dir().unwrap();
@ -50,7 +36,8 @@ fn main() {
let _ = io::stdout().flush(); let _ = io::stdout().flush();
io::stdin().read_line(&mut search).expect("Error reading from stdin"); io::stdin().read_line(&mut search).expect("Error reading from stdin");
if let Some(response) = block_on(run_query(&search, &auth_key)).data { if let Some(response) = block_on(
run_query::<VideogameSearch,_>(VideogameSearchVars { name: search }, &auth_key)).data {
for maybe_game in response.videogames.unwrap().nodes.unwrap().into_iter() { for maybe_game in response.videogames.unwrap().nodes.unwrap().into_iter() {
let game = maybe_game.unwrap(); let game = maybe_game.unwrap();
println!("{:?} - {}", game.id.unwrap(), game.name.unwrap()); println!("{:?} - {}", game.id.unwrap(), game.name.unwrap());

View file

@ -1,3 +1,5 @@
use cynic::QueryBuilder;
use serde::{Serialize, Deserialize};
pub mod search_games; pub mod search_games;
@ -8,3 +10,22 @@ use schema::schema;
/// new scalar type that serializes to u64. /// new scalar type that serializes to u64.
#[derive(cynic::Scalar, Debug)] #[derive(cynic::Scalar, Debug)]
pub struct ID(u64); pub struct ID(u64);
pub async fn run_query<Builder: 'static, Vars>(vars: Vars, auth: &str) -> cynic::GraphQlResponse<Builder>
where Builder: QueryBuilder<Vars>,
Vars: Serialize,
for<'de> Builder: Deserialize<'de>
{
use cynic::http::SurfExt;
let query = Builder::build(vars);
let response = surf::post("https://api.start.gg/gql/alpha")
.header("Authorization", String::from("Bearer ") + auth)
.run_graphql(query)
.await;
response.unwrap()
}