sui_gql_client/
raw_client.rs

1use cynic::{GraphQlResponse, Operation};
2use serde::Serialize;
3use serde::de::DeserializeOwned;
4use serde_json::Value as Json;
5
6use crate::GraphQlClient;
7
8/// Client that takes a Cynic operation and return the GraphQL response as JSON.
9#[trait_variant::make(Send)]
10pub trait RawClient {
11    type Error;
12
13    /// Run the operation and return the raw JSON GraphQL response.
14    async fn run_graphql_raw<Query, Vars>(
15        &self,
16        operation: Operation<Query, Vars>,
17    ) -> Result<Json, Self::Error>
18    where
19        Vars: Serialize + Send;
20}
21
22#[derive(thiserror::Error, Debug)]
23pub enum Error<I> {
24    #[error(transparent)]
25    Inner(I),
26
27    #[error("Deserializing query: {0}")]
28    Json(#[from] serde_json::Error),
29}
30
31impl<T> GraphQlClient for T
32where
33    T: RawClient + Sync,
34    T::Error: std::error::Error + Send + 'static,
35{
36    type Error = Error<T::Error>;
37
38    async fn run_graphql<Query, Vars>(
39        &self,
40        operation: Operation<Query, Vars>,
41    ) -> Result<GraphQlResponse<Query>, Self::Error>
42    where
43        Vars: Serialize + Send,
44        Query: DeserializeOwned + 'static,
45    {
46        let json = self
47            .run_graphql_raw(operation)
48            .await
49            .map_err(Error::Inner)?;
50        Ok(serde_json::from_value(json)?)
51    }
52}