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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use crate::conn::{MutationConn, QueryConn, SubscriptionConn};
use futures::stream::BoxStream;
use graphql_client::GraphQLQuery;
use std::{fmt::Debug, marker::PhantomData};
pub struct Client<T, E> {
conn: T,
_err_type: PhantomData<E>,
}
impl<T, E> Client<T, E> {
pub fn new(conn: T) -> Self {
Self {
conn,
_err_type: PhantomData,
}
}
pub fn into_connection(self) -> T {
self.conn
}
}
impl<T: Clone, E> Clone for Client<T, E> {
fn clone(&self) -> Self {
Self {
conn: self.conn.clone(),
_err_type: PhantomData,
}
}
}
impl<T: Debug, E> Debug for Client<T, E> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Client").field("conn", &self.conn).finish()
}
}
impl<T, E> Client<T, E>
where
T: QueryConn<Error = E>,
{
pub async fn query<Q>(&self, variables: Q::Variables) -> Result<Q::ResponseData, E>
where
Q: GraphQLQuery,
Q::Variables: Send + Sync,
{
self.conn.query::<Q>(variables).await
}
}
impl<T, E> Client<T, E>
where
T: MutationConn<Error = E>,
{
pub async fn mutate<Q>(&self, variables: Q::Variables) -> Result<Q::ResponseData, E>
where
Q: GraphQLQuery,
Q::Variables: Send + Sync,
{
self.conn.mutate::<Q>(variables).await
}
}
impl<T, E> Client<T, E>
where
T: SubscriptionConn<Error = E>,
{
pub async fn subscribe<'a, Q>(
&self,
variables: Q::Variables,
) -> Result<BoxStream<'a, Q::ResponseData>, E>
where
Q: GraphQLQuery,
Q::Variables: Send + Sync,
{
self.conn.subscribe::<Q>(variables).await
}
}