mentorenwahl/frontend/src/graphql/mod.rs

35 lines
947 B
Rust

use lazy_static::lazy_static;
use std::path::Path;
pub mod mutations;
pub mod queries;
lazy_static! {
pub static ref URL: String =
Path::new(&web_sys::window().unwrap().location().origin().unwrap())
.join("graphql")
.to_str()
.unwrap()
.to_string();
}
pub fn client(token: Option<&String>) -> Result<reqwest::Client, reqwest::Error> {
if let Some(x) = token {
let mut headers = reqwest::header::HeaderMap::new();
headers.insert(
"Authorization",
reqwest::header::HeaderValue::from_str(&format!("Bearer {}", x)).unwrap(),
);
reqwest::Client::builder().default_headers(headers).build()
} else {
Ok(reqwest::Client::new())
}
}
pub type Errors = Option<Vec<String>>;
pub fn convert(errors: Option<Vec<graphql_client::Error>>) -> Errors {
errors.map(|x| x.iter().map(|e| e.message.to_owned()).collect())
}