@@ -18,16 +18,34 @@ extension GraphQLHandler {
1818 }
1919 } ,
2020 onUpgrade: { websocket in
21+ let messageStream = AsyncThrowingStream < String , Error > { continuation in
22+ websocket. onText { _, text in
23+ continuation. yield ( text)
24+ }
25+ websocket. onClose. whenComplete { result in
26+ switch result {
27+ case . success:
28+ continuation. finish ( )
29+ case let . failure( error) :
30+ continuation. finish ( throwing: error)
31+ }
32+ }
33+ }
34+
2135 let messenger = WebSocketMessenger ( websocket: websocket)
2236 switch subProtocol {
2337 case . graphqlTransportWs:
2438 // https://github.com/enisdenjo/graphql-ws/blob/master/PROTOCOL.md
25- let server = GraphQLTransportWS . Server < WebSocketInit , AsyncThrowingStream < GraphQLResult , Error > > (
39+ let server = GraphQLTransportWS . Server < WebSocketInit , WebSocketInitResult , AsyncThrowingStream < GraphQLResult , Error > > (
2640 messenger: messenger,
27- onExecute: { graphQLRequest in
41+ onInit: { initPayload in
42+ try await config. websocket. onWebSocketInit ( initPayload, request)
43+ } ,
44+ onExecute: { graphQLRequest, initResult in
2845 let graphQLContextComputationInputs = GraphQLContextComputationInputs (
2946 vaporRequest: request,
30- graphQLRequest: graphQLRequest
47+ graphQLRequest: graphQLRequest,
48+ websocketInitResult: initResult
3149 )
3250 let context = try await computeContext ( graphQLContextComputationInputs)
3351 return try await graphql (
@@ -39,10 +57,11 @@ extension GraphQLHandler {
3957 operationName: graphQLRequest. operationName
4058 )
4159 } ,
42- onSubscribe: { graphQLRequest in
60+ onSubscribe: { graphQLRequest, initResult in
4361 let graphQLContextComputationInputs = GraphQLContextComputationInputs (
4462 vaporRequest: request,
45- graphQLRequest: graphQLRequest
63+ graphQLRequest: graphQLRequest,
64+ websocketInitResult: initResult
4665 )
4766 let context = try await computeContext ( graphQLContextComputationInputs)
4867 return try await graphqlSubscribe (
@@ -55,15 +74,22 @@ extension GraphQLHandler {
5574 ) . get ( )
5675 }
5776 )
58- server. auth ( config. websocket. onWebsocketInit)
77+ Task {
78+ // This task completes upon websocket closure
79+ try await server. listen ( to: messageStream)
80+ }
5981 case . graphqlWs:
6082 // https://github.com/apollographql/subscriptions-transport-ws/blob/master/PROTOCOL.md
61- let server = GraphQLWS . Server < WebSocketInit , AsyncThrowingStream < GraphQLResult , Error > > (
83+ let server = GraphQLWS . Server < WebSocketInit , WebSocketInitResult , AsyncThrowingStream < GraphQLResult , Error > > (
6284 messenger: messenger,
63- onExecute: { graphQLRequest in
85+ onInit: { initPayload in
86+ try await config. websocket. onWebSocketInit ( initPayload, request)
87+ } ,
88+ onExecute: { graphQLRequest, initResult in
6489 let graphQLContextComputationInputs = GraphQLContextComputationInputs (
6590 vaporRequest: request,
66- graphQLRequest: graphQLRequest
91+ graphQLRequest: graphQLRequest,
92+ websocketInitResult: initResult
6793 )
6894 let context = try await computeContext ( graphQLContextComputationInputs)
6995 return try await graphql (
@@ -75,10 +101,11 @@ extension GraphQLHandler {
75101 operationName: graphQLRequest. operationName
76102 )
77103 } ,
78- onSubscribe: { graphQLRequest in
104+ onSubscribe: { graphQLRequest, initResult in
79105 let graphQLContextComputationInputs = GraphQLContextComputationInputs (
80106 vaporRequest: request,
81- graphQLRequest: graphQLRequest
107+ graphQLRequest: graphQLRequest,
108+ websocketInitResult: initResult
82109 )
83110 let context = try await computeContext ( graphQLContextComputationInputs)
84111 return try await graphqlSubscribe (
@@ -91,7 +118,10 @@ extension GraphQLHandler {
91118 ) . get ( )
92119 }
93120 )
94- server. auth ( config. websocket. onWebsocketInit)
121+ Task {
122+ // This task completes upon websocket closure
123+ try await server. listen ( to: messageStream)
124+ }
95125 }
96126 }
97127 )
0 commit comments