Add graphql-ws feature for subprotcol selection

On 2020-09-14 a revised version of the [GraphQL over
WebSocket](https://the-guild.dev/blog/graphql-over-websockets) spec was
released as [graphql-ws](https://github.com/enisdenjo/graphql-ws), which
differs from the de facto standard implemented by
[subscriptions-transport-ws](https://github.com/apollographql/subscriptions-transport-ws).

This adds a new `cargo` (non-default) feature, `graphql_ws`, which when enabled utilizes the new protocol spec.

Original [subscriptions-transport-ws
protocol](https://github.com/apollographql/subscriptions-transport-ws/blob/master/PROTOCOL.md)
Revised [graphql-ws
protocol](https://github.com/enisdenjo/graphql-ws/blob/master/PROTOCOL.md)
This commit is contained in:
Patrick Fernie 2020-12-02 08:13:00 -05:00
parent 64d3c56cd9
commit 5f25b296b0
4 changed files with 21 additions and 1 deletions

View File

@ -34,6 +34,8 @@ unblock = ["blocking"]
string_number = ["num-traits"]
# Used for doc(cfg())
nightly = []
# To enable the use of [graphql-ws spec](https://github.com/enisdenjo/graphql-ws)
graphql_ws = []
[dependencies]
async-graphql-derive = { path = "derive", version = "=2.1.4" }

View File

@ -11,6 +11,10 @@ repository = "https://github.com/async-graphql/async-graphql"
keywords = ["futures", "async", "graphql"]
categories = ["network-programming", "asynchronous"]
[features]
# To enable the use of [graphql-ws spec](https://github.com/enisdenjo/graphql-ws)
graphql_ws = []
[dependencies]
async-graphql = { path = "../..", version = "=2.1.4" }

View File

@ -86,6 +86,17 @@ where
.await;
}
});
warp::reply::with_header(reply, "Sec-WebSocket-Protocol", "graphql-ws")
#[cfg(not(feature = "graphql_ws"))]
{
// confusingly, the old subprotocol name for the susbscription-transport-ws spec was
// `graphql-ws`
warp::reply::with_header(reply, "Sec-WebSocket-Protocol", "graphql-ws")
}
#[cfg(feature = "graphql_ws")]
{
// ...and the new one `graphql-transport-ws`
warp::reply::with_header(reply, "Sec-WebSocket-Protocol", "graphql-transport-ws")
}
})
}

View File

@ -169,7 +169,9 @@ where
#[serde(tag = "type", rename_all = "snake_case")]
enum ClientMessage<'a> {
ConnectionInit { payload: Option<serde_json::Value> },
#[cfg_attr(feature = "graphql_ws", serde(rename = "subscribe"))]
Start { id: String, payload: Request },
#[cfg_attr(feature = "graphql_ws", serde(rename = "complete"))]
Stop { id: &'a str },
ConnectionTerminate,
}
@ -179,6 +181,7 @@ enum ClientMessage<'a> {
enum ServerMessage<'a> {
ConnectionError { payload: Error },
ConnectionAck,
#[cfg_attr(feature = "graphql_ws", serde(rename = "next"))]
Data { id: &'a str, payload: Box<Response> },
// Not used by this library, as it's not necessary to send
// Error {