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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use crate::{
conn::{MutationConn, QueryConn, SubscriptionConn},
subscription::Subscription,
Client,
};
use graphql_client::GraphQLQuery;
use std::fmt::Debug;
pub struct Connection<C, S> {
query_conn: C,
subscription_conn: S,
}
impl<C, S> Connection<C, S> {
pub fn new(query_conn: C, subscription_conn: S) -> Self {
Self {
query_conn,
subscription_conn,
}
}
pub fn as_client(&self) -> Client<&Self> {
Client::new(self)
}
pub fn into_client(self) -> Client<Self> {
Client::new(self)
}
}
impl<C, S> Clone for Connection<C, S>
where
C: Clone,
S: Clone,
{
fn clone(&self) -> Self {
Self {
query_conn: self.query_conn.clone(),
subscription_conn: self.subscription_conn.clone(),
}
}
}
impl<C, S> Connection<C, S>
where
C: Clone,
S: Clone,
{
pub fn to_client(&self) -> Client<Self> {
Client::new(self.clone())
}
}
impl<C, S> Debug for Connection<C, S> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Client").finish_non_exhaustive()
}
}
#[async_trait]
impl<C, S> QueryConn for Connection<C, S>
where
C: QueryConn + Send + Sync,
S: Send + Sync,
{
type Error = C::Error;
async fn query<Q>(&self, variables: Q::Variables) -> Result<Q::ResponseData, Self::Error>
where
Q: GraphQLQuery,
Q::Variables: Send + Sync,
{
self.query_conn.query::<Q>(variables).await
}
}
#[async_trait]
impl<C, S> MutationConn for Connection<C, S>
where
C: MutationConn + Send + Sync,
S: Send + Sync,
{
type Error = C::Error;
async fn mutate<Q>(&self, variables: Q::Variables) -> Result<Q::ResponseData, Self::Error>
where
Q: GraphQLQuery,
Q::Variables: Send + Sync,
{
self.query_conn.mutate::<Q>(variables).await
}
}
#[async_trait]
impl<C, S> SubscriptionConn for Connection<C, S>
where
S: SubscriptionConn + Send + Sync,
C: Send + Sync,
{
type Error = S::Error;
async fn subscribe<Q>(
&self,
variables: Q::Variables,
) -> Result<Subscription<Q::ResponseData>, Self::Error>
where
Q: GraphQLQuery,
Q::Variables: Send + Sync,
{
self.subscription_conn.subscribe::<Q>(variables).await
}
}
pub type HybridClient<C, S> = Client<Connection<C, S>>;