use std::{borrow::Cow, convert::Infallible, future::Future, str::FromStr, time::Duration};
use async_graphql::{
futures_util::task::{Context, Poll},
http::{
default_on_connection_init, default_on_ping, DefaultOnConnInitType, DefaultOnPingType,
WebSocketProtocols, WsMessage, ALL_WEBSOCKET_PROTOCOLS,
},
Data, Executor, Result,
};
use axum::{
body::{Body, HttpBody},
extract::{
ws::{CloseFrame, Message},
FromRequestParts, WebSocketUpgrade,
},
http::{self, request::Parts, Request, Response, StatusCode},
response::IntoResponse,
Error,
};
use futures_util::{
future,
future::BoxFuture,
stream::{SplitSink, SplitStream},
Sink, SinkExt, Stream, StreamExt,
};
use tower_service::Service;
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct GraphQLProtocol(WebSocketProtocols);
#[async_trait::async_trait]
impl<S> FromRequestParts<S> for GraphQLProtocol
where
S: Send + Sync,
{
type Rejection = StatusCode;
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
parts
.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())
})
.map(Self)
.ok_or(StatusCode::BAD_REQUEST)
}
}
pub struct GraphQLSubscription<E> {
executor: E,
}
impl<E> Clone for GraphQLSubscription<E>
where
E: Executor,
{
fn clone(&self) -> Self {
Self {
executor: self.executor.clone(),
}
}
}
impl<E> GraphQLSubscription<E>
where
E: Executor,
{
pub fn new(executor: E) -> Self {
Self { executor }
}
}
impl<B, E> Service<Request<B>> for GraphQLSubscription<E>
where
B: HttpBody + Send + 'static,
E: Executor,
{
type Response = Response<Body>;
type Error = Infallible;
type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: Request<B>) -> Self::Future {
let executor = self.executor.clone();
Box::pin(async move {
let (mut parts, _body) = req.into_parts();
let protocol = match GraphQLProtocol::from_request_parts(&mut parts, &()).await {
Ok(protocol) => protocol,
Err(err) => return Ok(err.into_response()),
};
let upgrade = match WebSocketUpgrade::from_request_parts(&mut parts, &()).await {
Ok(protocol) => protocol,
Err(err) => return Ok(err.into_response()),
};
let executor = executor.clone();
let resp = upgrade
.protocols(ALL_WEBSOCKET_PROTOCOLS)
.on_upgrade(move |stream| {
GraphQLWebSocket::new(stream, executor, protocol).serve()
});
Ok(resp.into_response())
})
}
}
pub struct GraphQLWebSocket<Sink, Stream, E, OnConnInit, OnPing> {
sink: Sink,
stream: Stream,
executor: E,
data: Data,
on_connection_init: OnConnInit,
on_ping: OnPing,
protocol: GraphQLProtocol,
keepalive_timeout: Option<Duration>,
}
impl<S, E>
GraphQLWebSocket<
SplitSink<S, Message>,
SplitStream<S>,
E,
DefaultOnConnInitType,
DefaultOnPingType,
>
where
S: Stream<Item = Result<Message, Error>> + Sink<Message>,
E: Executor,
{
pub fn new(stream: S, executor: E, protocol: GraphQLProtocol) -> Self {
let (sink, stream) = stream.split();
GraphQLWebSocket::new_with_pair(sink, stream, executor, protocol)
}
}
impl<Sink, Stream, E> GraphQLWebSocket<Sink, Stream, E, DefaultOnConnInitType, DefaultOnPingType>
where
Sink: futures_util::sink::Sink<Message>,
Stream: futures_util::stream::Stream<Item = Result<Message, Error>>,
E: Executor,
{
pub fn new_with_pair(
sink: Sink,
stream: Stream,
executor: E,
protocol: GraphQLProtocol,
) -> Self {
GraphQLWebSocket {
sink,
stream,
executor,
data: Data::default(),
on_connection_init: default_on_connection_init,
on_ping: default_on_ping,
protocol,
keepalive_timeout: None,
}
}
}
impl<Sink, Stream, E, OnConnInit, OnConnInitFut, OnPing, OnPingFut>
GraphQLWebSocket<Sink, Stream, E, OnConnInit, OnPing>
where
Sink: futures_util::sink::Sink<Message>,
Stream: futures_util::stream::Stream<Item = Result<Message, Error>>,
E: Executor,
OnConnInit: FnOnce(serde_json::Value) -> OnConnInitFut + Send + 'static,
OnConnInitFut: Future<Output = async_graphql::Result<Data>> + Send + 'static,
OnPing: FnOnce(Option<&Data>, Option<serde_json::Value>) -> OnPingFut + Clone + Send + 'static,
OnPingFut: Future<Output = async_graphql::Result<Option<serde_json::Value>>> + Send + 'static,
{
#[must_use]
pub fn with_data(self, data: Data) -> Self {
Self { data, ..self }
}
#[must_use]
pub fn on_connection_init<F, R>(
self,
callback: F,
) -> GraphQLWebSocket<Sink, Stream, E, F, OnPing>
where
F: FnOnce(serde_json::Value) -> R + Send + 'static,
R: Future<Output = async_graphql::Result<Data>> + Send + 'static,
{
GraphQLWebSocket {
sink: self.sink,
stream: self.stream,
executor: self.executor,
data: self.data,
on_connection_init: callback,
on_ping: self.on_ping,
protocol: self.protocol,
keepalive_timeout: self.keepalive_timeout,
}
}
#[must_use]
pub fn on_ping<F, R>(self, callback: F) -> GraphQLWebSocket<Sink, Stream, E, OnConnInit, F>
where
F: FnOnce(Option<&Data>, Option<serde_json::Value>) -> R + Clone + Send + 'static,
R: Future<Output = Result<Option<serde_json::Value>>> + Send + 'static,
{
GraphQLWebSocket {
sink: self.sink,
stream: self.stream,
executor: self.executor,
data: self.data,
on_connection_init: self.on_connection_init,
on_ping: callback,
protocol: self.protocol,
keepalive_timeout: self.keepalive_timeout,
}
}
#[must_use]
pub fn keepalive_timeout(self, timeout: impl Into<Option<Duration>>) -> Self {
Self {
keepalive_timeout: timeout.into(),
..self
}
}
pub async fn serve(self) {
let input = self
.stream
.take_while(|res| future::ready(res.is_ok()))
.map(Result::unwrap)
.filter_map(|msg| {
if let Message::Text(_) | Message::Binary(_) = msg {
future::ready(Some(msg))
} else {
future::ready(None)
}
})
.map(Message::into_data);
let stream =
async_graphql::http::WebSocket::new(self.executor.clone(), input, self.protocol.0)
.connection_data(self.data)
.on_connection_init(self.on_connection_init)
.on_ping(self.on_ping.clone())
.keepalive_timeout(self.keepalive_timeout)
.map(|msg| match msg {
WsMessage::Text(text) => Message::Text(text),
WsMessage::Close(code, status) => Message::Close(Some(CloseFrame {
code,
reason: Cow::from(status),
})),
});
let sink = self.sink;
futures_util::pin_mut!(stream, sink);
while let Some(item) = stream.next().await {
if sink.send(item).await.is_err() {
break;
}
}
}
}