Skip to content

Commit 19a4261

Browse files
committed
Update axum subscription example for axum 0.2
1 parent a1f3c0a commit 19a4261

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

axum/subscription/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ async-graphql-axum = { path = "../../../integrations/axum" }
99
tokio = { version = "1.8", features = ["macros", "rt-multi-thread"] }
1010
books = { path = "../../models/books" }
1111
hyper = "0.14"
12-
axum = { git = "https://github.com/tokio-rs/axum.git", rev = "1509d4a", features = ["ws", "headers"] }
12+
axum = { version = "~0.2.3", features = ["ws", "headers"] }

axum/subscription/src/main.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ use async_graphql::Schema;
33
use async_graphql_axum::{
44
graphql_subscription, GraphQLRequest, GraphQLResponse, SecWebsocketProtocol,
55
};
6-
use axum::extract::TypedHeader;
7-
use axum::response::IntoResponse;
8-
use axum::ws::{ws, WebSocket};
9-
use axum::{prelude::*, AddExtensionLayer};
6+
use axum::extract::{self, ws::WebSocketUpgrade, TypedHeader};
7+
use axum::handler::get;
8+
use axum::response::{self, IntoResponse};
9+
use axum::{AddExtensionLayer, Router, Server};
1010
use books::{BooksSchema, MutationRoot, QueryRoot, Storage, SubscriptionRoot};
11-
use hyper::http::HeaderValue;
1211

1312
async fn graphql_handler(
1413
schema: extract::Extension<BooksSchema>,
@@ -18,11 +17,14 @@ async fn graphql_handler(
1817
}
1918

2019
async fn graphql_subscription_handler(
21-
socket: WebSocket,
20+
ws: WebSocketUpgrade,
2221
schema: extract::Extension<BooksSchema>,
2322
protocol: TypedHeader<SecWebsocketProtocol>,
24-
) {
25-
graphql_subscription(socket, schema.0.clone(), protocol.0).await
23+
) -> impl IntoResponse {
24+
ws.protocols(ALL_WEBSOCKET_PROTOCOLS)
25+
.on_upgrade(move |socket| async move {
26+
graphql_subscription(socket, schema.0.clone(), protocol.0.clone()).await
27+
})
2628
}
2729

2830
async fn graphql_playground() -> impl IntoResponse {
@@ -35,16 +37,14 @@ async fn main() {
3537
.data(Storage::default())
3638
.finish();
3739

38-
let app = route("/", get(graphql_playground).post(graphql_handler))
39-
.route(
40-
"/ws",
41-
ws(graphql_subscription_handler).protocols(ALL_WEBSOCKET_PROTOCOLS),
42-
)
40+
let app = Router::new()
41+
.route("/", get(graphql_playground).post(graphql_handler))
42+
.route("/ws", get(graphql_subscription_handler))
4343
.layer(AddExtensionLayer::new(schema));
4444

4545
println!("Playground: http://localhost:8000");
4646

47-
hyper::Server::bind(&"0.0.0.0:8000".parse().unwrap())
47+
Server::bind(&"0.0.0.0:8000".parse().unwrap())
4848
.serve(app.into_make_service())
4949
.await
5050
.unwrap();

0 commit comments

Comments
 (0)