@@ -3,12 +3,11 @@ use async_graphql::Schema;
3
3
use async_graphql_axum:: {
4
4
graphql_subscription, GraphQLRequest , GraphQLResponse , SecWebsocketProtocol ,
5
5
} ;
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 } ;
10
10
use books:: { BooksSchema , MutationRoot , QueryRoot , Storage , SubscriptionRoot } ;
11
- use hyper:: http:: HeaderValue ;
12
11
13
12
async fn graphql_handler (
14
13
schema : extract:: Extension < BooksSchema > ,
@@ -18,11 +17,14 @@ async fn graphql_handler(
18
17
}
19
18
20
19
async fn graphql_subscription_handler (
21
- socket : WebSocket ,
20
+ ws : WebSocketUpgrade ,
22
21
schema : extract:: Extension < BooksSchema > ,
23
22
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
+ } )
26
28
}
27
29
28
30
async fn graphql_playground ( ) -> impl IntoResponse {
@@ -35,16 +37,14 @@ async fn main() {
35
37
. data ( Storage :: default ( ) )
36
38
. finish ( ) ;
37
39
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) )
43
43
. layer ( AddExtensionLayer :: new ( schema) ) ;
44
44
45
45
println ! ( "Playground: http://localhost:8000" ) ;
46
46
47
- hyper :: Server :: bind ( & "0.0.0.0:8000" . parse ( ) . unwrap ( ) )
47
+ Server :: bind ( & "0.0.0.0:8000" . parse ( ) . unwrap ( ) )
48
48
. serve ( app. into_make_service ( ) )
49
49
. await
50
50
. unwrap ( ) ;
0 commit comments