use anyhow::Context;
use graphql_client::GraphQLQuery;
use url::Url;
use crate::gql::HealthQueryExt;
pub type QueryResult<T> =
anyhow::Result<graphql_client::Response<<T as GraphQLQuery>::ResponseData>>;
#[derive(Debug)]
pub struct Client {
url: Url,
}
impl Client {
pub fn new(url: Url) -> Self {
Self { url }
}
pub async fn healthcheck(&self) -> Result<(), ()> {
self.health_query().await.map(|_| ()).map_err(|_| ())
}
pub async fn query<T: GraphQLQuery>(
&self,
request_body: &graphql_client::QueryBody<T::Variables>,
) -> QueryResult<T> {
let client = reqwest::Client::new();
client
.post(self.url.clone())
.json(request_body)
.send()
.await
.with_context(|| {
format!(
"Couldn't send '{}' query to {}",
request_body.operation_name,
&self.url.as_str()
)
})?
.json()
.await
.with_context(|| {
format!(
"Couldn't serialize the response for '{}' query: {:?}",
request_body.operation_name, request_body.query
)
})
}
}