@@ -14,18 +14,32 @@ extension GraphQLHandler where Context: WebSocketRequestContext {
1414 subProtocol: WebSocketSubProtocol ,
1515 logger: Logger
1616 ) async throws {
17- let messenger = WebSocketMessenger ( inbound: inbound, outbound: outbound, logger: logger)
17+ let messenger = WebSocketMessenger ( outbound: outbound, logger: logger)
18+
19+ // TODO: Make maxSize configurable
20+ let messageStream = inbound. messages ( maxSize: 1024 * 1024 ) . compactMap { message -> String ? in
21+ // TODO: Add binary support
22+ guard case let . text( text) = message else {
23+ return nil
24+ }
25+ logger. trace ( " GraphQL server received: \( message) " )
26+ return text
27+ }
1828
1929 switch subProtocol {
2030 case . graphqlTransportWs:
2131 // https://github.com/enisdenjo/graphql-ws/blob/master/PROTOCOL.md
22- let server = GraphQLTransportWS . Server < WebSocketInit , AsyncThrowingStream < GraphQLResult , Error > > (
32+ let server = GraphQLTransportWS . Server < WebSocketInit , WebSocketInitResult , AsyncThrowingStream < GraphQLResult , Error > > (
2333 messenger: messenger,
24- onExecute: { graphQLRequest in
25- let graphQLContextComputationInputs = GraphQLContextComputationInputs < Context > (
34+ onInit: { initPayload in
35+ try await config. websocket. onWebSocketInit ( initPayload, context. request, context. requestContext)
36+ } ,
37+ onExecute: { graphQLRequest, initResult in
38+ let graphQLContextComputationInputs = GraphQLContextComputationInputs < Context , WebSocketInitResult > (
2639 hummingbirdRequest: context. request,
2740 hummingbirdContext: context. requestContext,
28- graphQLRequest: graphQLRequest
41+ graphQLRequest: graphQLRequest,
42+ websocketInitResult: initResult
2943 )
3044 let graphQLContext = try await computeContext ( graphQLContextComputationInputs)
3145 return try await graphql (
@@ -37,11 +51,12 @@ extension GraphQLHandler where Context: WebSocketRequestContext {
3751 operationName: graphQLRequest. operationName
3852 )
3953 } ,
40- onSubscribe: { graphQLRequest in
41- let graphQLContextComputationInputs = GraphQLContextComputationInputs < Context > (
54+ onSubscribe: { graphQLRequest, initResult in
55+ let graphQLContextComputationInputs = GraphQLContextComputationInputs < Context , WebSocketInitResult > (
4256 hummingbirdRequest: context. request,
4357 hummingbirdContext: context. requestContext,
44- graphQLRequest: graphQLRequest
58+ graphQLRequest: graphQLRequest,
59+ websocketInitResult: initResult
4560 )
4661 let graphQLContext = try await computeContext ( graphQLContextComputationInputs)
4762 return try await graphqlSubscribe (
@@ -54,19 +69,20 @@ extension GraphQLHandler where Context: WebSocketRequestContext {
5469 ) . get ( )
5570 }
5671 )
57- server. onMessage { message in
58- logger. trace ( " GraphQL server received: \( String ( message) ) " )
59- }
60- server. auth ( config. websocket. onWebSocketInit)
72+ try await server. listen ( to: messageStream)
6173 case . graphqlWs:
6274 // https://github.com/apollographql/subscriptions-transport-ws/blob/master/PROTOCOL.md
63- let server = GraphQLWS . Server < WebSocketInit , AsyncThrowingStream < GraphQLResult , Error > > (
75+ let server = GraphQLWS . Server < WebSocketInit , WebSocketInitResult , AsyncThrowingStream < GraphQLResult , Error > > (
6476 messenger: messenger,
65- onExecute: { graphQLRequest in
66- let graphQLContextComputationInputs = GraphQLContextComputationInputs < Context > (
77+ onInit: { initPayload in
78+ try await config. websocket. onWebSocketInit ( initPayload, context. request, context. requestContext)
79+ } ,
80+ onExecute: { graphQLRequest, initResult in
81+ let graphQLContextComputationInputs = GraphQLContextComputationInputs < Context , WebSocketInitResult > (
6782 hummingbirdRequest: context. request,
6883 hummingbirdContext: context. requestContext,
69- graphQLRequest: graphQLRequest
84+ graphQLRequest: graphQLRequest,
85+ websocketInitResult: initResult
7086 )
7187 let graphQLContext = try await computeContext ( graphQLContextComputationInputs)
7288 return try await graphql (
@@ -78,11 +94,12 @@ extension GraphQLHandler where Context: WebSocketRequestContext {
7894 operationName: graphQLRequest. operationName
7995 )
8096 } ,
81- onSubscribe: { graphQLRequest in
82- let graphQLContextComputationInputs = GraphQLContextComputationInputs < Context > (
97+ onSubscribe: { graphQLRequest, initResult in
98+ let graphQLContextComputationInputs = GraphQLContextComputationInputs < Context , WebSocketInitResult > (
8399 hummingbirdRequest: context. request,
84100 hummingbirdContext: context. requestContext,
85- graphQLRequest: graphQLRequest
101+ graphQLRequest: graphQLRequest,
102+ websocketInitResult: initResult
86103 )
87104 let graphQLContext = try await computeContext ( graphQLContextComputationInputs)
88105 return try await graphqlSubscribe (
@@ -95,12 +112,8 @@ extension GraphQLHandler where Context: WebSocketRequestContext {
95112 ) . get ( )
96113 }
97114 )
98- server. onMessage { message in
99- logger. trace ( " GraphQL server received: \( String ( message) ) " )
100- }
101- server. auth ( config. websocket. onWebSocketInit)
115+ try await server. listen ( to: messageStream)
102116 }
103- try await messenger. start ( )
104117 }
105118
106119 func shouldUpgrade( request: Request ) throws -> RouterShouldUpgrade {
0 commit comments