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
//! Hybrid connection that can use a different connection for subscriptions vs querues & mutations.
//!
//! You will usually pretty much immediately turn the connection into a Client.
//! E.g.
//! ```rust
//! use glimesh::{hybrid, ws, http, Auth};
//! let auth = Auth::client_credentials("<GLIMESH_CLIENT_ID>", "<GLIMESH_CLIENT_SECRET>");
//! let query = http::Connection::new(auth.clone());
//! let sub = ws::Connection::new(auth);
//! let client = hybrid::Connection::new(query, sub).into_client();
//! ```

use crate::{
    conn::{MutationConn, QueryConn, SubscriptionConn},
    subscription::Subscription,
    Client,
};
use graphql_client::GraphQLQuery;
use std::fmt::Debug;

/// A hybrid connection that allows combining two different connections into one effective
/// connection, for example for using an http client for querying & mutating, and a websocket client
/// for subscriptions.
pub struct Connection<C, S> {
    query_conn: C,
    subscription_conn: S,
}

impl<C, S> Connection<C, S> {
    /// Create a new hybrid connection. The first connection will be used for query and mutate
    /// operations, and the second only for subscriptions.
    pub fn new(query_conn: C, subscription_conn: S) -> Self {
        Self {
            query_conn,
            subscription_conn,
        }
    }

    /// Create a client with reference to this connection
    pub fn as_client(&self) -> Client<&Self> {
        Client::new(self)
    }

    /// Convert this connection into a client
    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,
{
    /// Create a client with a clone of this connection
    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
    }
}

/// Type alias for a hybrid backed client
pub type HybridClient<C, S> = Client<Connection<C, S>>;