1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use anyhow::Context;
use graphql_client::GraphQLQuery;
use url::Url;

use crate::gql::HealthQueryExt;

/// Wrapped `Result` type, that returns deserialized GraphQL response data.
pub type QueryResult<T> =
    anyhow::Result<graphql_client::Response<<T as GraphQLQuery>::ResponseData>>;

/// GraphQL query client over HTTP.
#[derive(Debug)]
pub struct Client {
    url: Url,
}

impl Client {
    /// Returns a new GraphQL query client, bound to the provided URL.
    pub fn new(url: Url) -> Self {
        Self { url }
    }

    /// Send a health query
    pub async fn healthcheck(&self) -> Result<(), ()> {
        self.health_query().await.map(|_| ()).map_err(|_| ())
    }

    /// Issue a GraphQL query using Reqwest, serializing the response to the associated
    /// GraphQL type for the given `request_body`.
    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
                )
            })
    }
}