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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//! WebSocket transport for subscription

use std::collections::HashMap;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};

use futures_util::stream::Stream;
use pin_project_lite::pin_project;
use serde::{Deserialize, Serialize};

use crate::{Data, Error, ObjectType, Request, Response, Result, Schema, SubscriptionType};

pin_project! {
    /// A GraphQL connection over websocket.
    ///
    /// [Reference](https://github.com/apollographql/subscriptions-transport-ws/blob/master/PROTOCOL.md).
    pub struct WebSocket<S, F, Query, Mutation, Subscription> {
        data_initializer: Option<F>,
        data: Arc<Data>,
        schema: Schema<Query, Mutation, Subscription>,
        streams: HashMap<String, Pin<Box<dyn Stream<Item = Response> + Send>>>,
        #[pin]
        stream: S,
    }
}

impl<S, Query, Mutation, Subscription>
    WebSocket<S, fn(serde_json::Value) -> Result<Data>, Query, Mutation, Subscription>
{
    /// Create a new websocket.
    #[must_use]
    pub fn new(schema: Schema<Query, Mutation, Subscription>, stream: S) -> Self {
        Self {
            data_initializer: None,
            data: Arc::default(),
            schema,
            streams: HashMap::new(),
            stream,
        }
    }
}

impl<S, F, Query, Mutation, Subscription> WebSocket<S, F, Query, Mutation, Subscription> {
    /// Create a new websocket with a data initialization function.
    ///
    /// This function, if present, will be called with the data sent by the client in the
    /// [`GQL_CONNECTION_INIT` message](https://github.com/apollographql/subscriptions-transport-ws/blob/master/PROTOCOL.md#gql_connection_init).
    /// From that point on the returned data will be accessible to all requests.
    #[must_use]
    pub fn with_data(
        schema: Schema<Query, Mutation, Subscription>,
        stream: S,
        data_initializer: Option<F>,
    ) -> Self {
        Self {
            data_initializer,
            data: Arc::default(),
            schema,
            streams: HashMap::new(),
            stream,
        }
    }
}

impl<S, F, Query, Mutation, Subscription> Stream for WebSocket<S, F, Query, Mutation, Subscription>
where
    S: Stream,
    S::Item: AsRef<[u8]>,
    F: FnOnce(serde_json::Value) -> Result<Data>,
    Query: ObjectType + Send + Sync + 'static,
    Mutation: ObjectType + Send + Sync + 'static,
    Subscription: SubscriptionType + Send + Sync + 'static,
{
    type Item = String;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
        let mut this = self.project();

        while let Poll::Ready(message) = Pin::new(&mut this.stream).poll_next(cx) {
            let message = match message {
                Some(message) => message,
                None => return Poll::Ready(None),
            };

            let message: ClientMessage = match serde_json::from_slice(message.as_ref()) {
                Ok(message) => message,
                Err(e) => {
                    return Poll::Ready(Some(
                        serde_json::to_string(&ServerMessage::ConnectionError {
                            payload: Error::new(e.to_string()),
                        })
                        .unwrap(),
                    ))
                }
            };

            match message {
                ClientMessage::ConnectionInit { payload } => {
                    if let Some(payload) = payload {
                        if let Some(data_initializer) = this.data_initializer.take() {
                            *this.data = Arc::new(match data_initializer(payload) {
                                Ok(data) => data,
                                Err(e) => {
                                    return Poll::Ready(Some(
                                        serde_json::to_string(&ServerMessage::ConnectionError {
                                            payload: e,
                                        })
                                        .unwrap(),
                                    ))
                                }
                            });
                        }
                    }
                    return Poll::Ready(Some(
                        serde_json::to_string(&ServerMessage::ConnectionAck).unwrap(),
                    ));
                }
                ClientMessage::Start {
                    id,
                    payload: request,
                } => {
                    this.streams.insert(
                        id,
                        Box::pin(
                            this.schema
                                .execute_stream_with_ctx_data(request, Arc::clone(this.data)),
                        ),
                    );
                }
                ClientMessage::Stop { id } => {
                    if this.streams.remove(id).is_some() {
                        return Poll::Ready(Some(
                            serde_json::to_string(&ServerMessage::Complete { id }).unwrap(),
                        ));
                    }
                }
                ClientMessage::ConnectionTerminate => return Poll::Ready(None),
            }
        }

        for (id, stream) in &mut *this.streams {
            match Pin::new(stream).poll_next(cx) {
                Poll::Ready(Some(payload)) => {
                    return Poll::Ready(Some(
                        serde_json::to_string(&ServerMessage::Data {
                            id,
                            payload: Box::new(payload),
                        })
                        .unwrap(),
                    ));
                }
                Poll::Ready(None) => {
                    let id = id.clone();
                    this.streams.remove(&id);
                    return Poll::Ready(Some(
                        serde_json::to_string(&ServerMessage::Complete { id: &id }).unwrap(),
                    ));
                }
                Poll::Pending => {}
            }
        }

        Poll::Pending
    }
}

#[derive(Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
enum ClientMessage<'a> {
    ConnectionInit { payload: Option<serde_json::Value> },
    Start { id: String, payload: Request },
    Stop { id: &'a str },
    ConnectionTerminate,
}

#[derive(Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
enum ServerMessage<'a> {
    ConnectionError { payload: Error },
    ConnectionAck,
    Data { id: &'a str, payload: Box<Response> },
    // Not used by this library, as it's not necessary to send
    // Error {
    //     id: &'a str,
    //     payload: serde_json::Value,
    // },
    Complete { id: &'a str },
    // Not used by this library
    // #[serde(rename = "ka")]
    // KeepAlive
}