diff --git a/Cargo.toml b/Cargo.toml index 63a15b51..75dd9eee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" } diff --git a/integrations/warp/Cargo.toml b/integrations/warp/Cargo.toml index 44461368..4e3c49f1 100644 --- a/integrations/warp/Cargo.toml +++ b/integrations/warp/Cargo.toml @@ -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" } diff --git a/integrations/warp/src/subscription.rs b/integrations/warp/src/subscription.rs index 60199e5e..4ba17483 100644 --- a/integrations/warp/src/subscription.rs +++ b/integrations/warp/src/subscription.rs @@ -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") + } }) } diff --git a/src/http/websocket.rs b/src/http/websocket.rs index 91096a40..dd5402ed 100644 --- a/src/http/websocket.rs +++ b/src/http/websocket.rs @@ -169,7 +169,9 @@ where #[serde(tag = "type", rename_all = "snake_case")] enum ClientMessage<'a> { ConnectionInit { payload: Option }, + #[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 }, // Not used by this library, as it's not necessary to send // Error {