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
use std::str::FromStr;

use async_graphql::http::{WebSocketProtocols, WsMessage, ALL_WEBSOCKET_PROTOCOLS};
use async_graphql::{Data, ObjectType, Schema, SubscriptionType};
use futures_util::future::{self, Ready};
use futures_util::{Future, SinkExt, StreamExt};
use poem::web::websocket::{Message, WebSocket};
use poem::{http, Endpoint, FromRequest, IntoResponse, Request, Response, Result};

/// A GraphQL subscription endpoint.
///
/// # Example
///
/// ```
/// use poem::{Route, get};
/// use async_graphql_poem::GraphQLSubscription;
/// use async_graphql::{EmptyMutation, Object, Schema, Subscription};
/// use futures_util::{Stream, stream};
///
/// struct Query;
///
/// #[Object]
/// impl Query {
///     async fn value(&self) -> i32 {
///         100
///     }
/// }
///
/// struct Subscription;
///
/// #[Subscription]
/// impl Subscription {
///     async fn values(&self) -> impl Stream<Item = i32> {
///         stream::iter(vec![1, 2, 3, 4, 5])
///     }
/// }
///
/// type MySchema = Schema<Query, EmptyMutation, Subscription>;
///
/// let schema = Schema::new(Query, EmptyMutation, Subscription);
/// let app = Route::new().at("/ws", get(GraphQLSubscription::new(schema)));
/// ```
pub struct GraphQLSubscription<Query, Mutation, Subscription, F> {
    schema: Schema<Query, Mutation, Subscription>,
    initializer: F,
}

impl<Query, Mutation, Subscription>
    GraphQLSubscription<
        Query,
        Mutation,
        Subscription,
        fn(serde_json::Value) -> Ready<async_graphql::Result<Data>>,
    >
{
    /// Create a GraphQL subscription endpoint.
    pub fn new(schema: Schema<Query, Mutation, Subscription>) -> Self {
        Self {
            schema,
            initializer: |_| futures_util::future::ready(Ok(Default::default())),
        }
    }
}

impl<Query, Mutation, Subscription, F> GraphQLSubscription<Query, Mutation, Subscription, F> {
    /// With a data initialization function.
    pub fn with_initializer<F2, R>(
        self,
        initializer: F2,
    ) -> GraphQLSubscription<Query, Mutation, Subscription, F2>
    where
        F2: FnOnce(serde_json::Value) -> R + Clone + Send + Sync + 'static,
        R: Future<Output = Result<Data>> + Send + 'static,
    {
        GraphQLSubscription {
            schema: self.schema,
            initializer,
        }
    }
}

#[poem::async_trait]
impl<Query, Mutation, Subscription, F, R> Endpoint
    for GraphQLSubscription<Query, Mutation, Subscription, F>
where
    Query: ObjectType + 'static,
    Mutation: ObjectType + 'static,
    Subscription: SubscriptionType + 'static,
    F: FnOnce(serde_json::Value) -> R + Clone + Send + Sync + 'static,
    R: Future<Output = async_graphql::Result<Data>> + Send + 'static,
{
    type Output = Result<Response>;

    async fn call(&self, req: Request) -> Self::Output {
        let (req, mut body) = req.split();
        let websocket = WebSocket::from_request(&req, &mut body).await?;
        let protocol = req
            .headers()
            .get(http::header::SEC_WEBSOCKET_PROTOCOL)
            .and_then(|value| value.to_str().ok())
            .and_then(|protocols| {
                protocols
                    .split(',')
                    .find_map(|p| WebSocketProtocols::from_str(p.trim()).ok())
            })
            .unwrap_or(WebSocketProtocols::SubscriptionsTransportWS);
        let schema = self.schema.clone();
        let initializer = self.initializer.clone();

        let resp = websocket
            .protocols(ALL_WEBSOCKET_PROTOCOLS)
            .on_upgrade(move |socket| async move {
                let (mut sink, stream) = socket.split();

                let stream = stream
                    .take_while(|res| future::ready(res.is_ok()))
                    .map(Result::unwrap)
                    .filter_map(|msg| {
                        if msg.is_text() || msg.is_binary() {
                            future::ready(Some(msg))
                        } else {
                            future::ready(None)
                        }
                    })
                    .map(Message::into_bytes)
                    .boxed();

                let mut stream = async_graphql::http::WebSocket::with_data(
                    schema,
                    stream,
                    initializer,
                    protocol,
                )
                .map(|msg| match msg {
                    WsMessage::Text(text) => Message::text(text),
                    WsMessage::Close(code, status) => Message::close_with(code, status),
                });

                while let Some(item) = stream.next().await {
                    let _ = sink.send(item).await;
                }
            })
            .into_response();

        Ok(resp)
    }
}