linera_service_graphql_client/
utils.rs

1// Copyright (c) Zefchain Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use graphql_client::{reqwest::post_graphql, GraphQLQuery};
5use reqwest::Client;
6use thiserror::Error;
7
8#[derive(Error, Debug)]
9pub enum Error {
10    #[error(transparent)]
11    ReqwestError(#[from] reqwest::Error),
12    #[error("GraphQL errors: {0:?}")]
13    GraphQLError(Vec<graphql_client::Error>),
14}
15
16impl From<Option<Vec<graphql_client::Error>>> for Error {
17    fn from(val: Option<Vec<graphql_client::Error>>) -> Self {
18        Self::GraphQLError(val.unwrap_or_default())
19    }
20}
21
22pub async fn request<T, V>(
23    client: &Client,
24    url: &str,
25    variables: V,
26) -> Result<T::ResponseData, Error>
27where
28    T: GraphQLQuery<Variables = V> + Send + Unpin + 'static,
29    V: Send + Unpin,
30{
31    let response = post_graphql::<T, _>(client, url, variables).await?;
32    match response.data {
33        None => Err(response.errors.into()),
34        Some(data) => Ok(data),
35    }
36}