@@ -8,25 +8,7 @@ extension GraphQLHandler {
88 func handleWebSocket(
99 request: Request
1010 ) async throws -> Response {
11- var subProtocol : WebSocketSubProtocol ?
12- let requestedSubProtocols = request. headers [ " Sec-WebSocket-Protocol " ]
13- if requestedSubProtocols. isEmpty {
14- // Default
15- subProtocol = . graphqlTransportWs
16- } else {
17- // Choose highest client preference that we understand
18- for requestedSubProtocol in requestedSubProtocols {
19- if let selectedSubProtocol = WebSocketSubProtocol ( rawValue: requestedSubProtocol) {
20- subProtocol = selectedSubProtocol
21- break
22- }
23- }
24- }
25- guard let subProtocol = subProtocol else {
26- // If they provided options but none matched, fail
27- throw Abort ( . badRequest, reason: " Unable to negotiate subprotocol. \( WebSocketSubProtocol . allCases) are supported. " )
28- }
29-
11+ let subProtocol = try negotiateSubProtocol ( request: request)
3012 let context = try await computeContext ( request)
3113 let response = Response ( status: . switchingProtocols)
3214 response. upgrader = WebSocketUpgrader (
@@ -96,4 +78,26 @@ extension GraphQLHandler {
9678 )
9779 return response
9880 }
81+
82+ func negotiateSubProtocol( request: Request ) throws -> WebSocketSubProtocol {
83+ var subProtocol : WebSocketSubProtocol ?
84+ let requestedSubProtocols = request. headers [ " Sec-WebSocket-Protocol " ]
85+ if requestedSubProtocols. isEmpty {
86+ // Default
87+ subProtocol = . graphqlTransportWs
88+ } else {
89+ // Choose highest client preference that we understand
90+ for requestedSubProtocol in requestedSubProtocols {
91+ if let selectedSubProtocol = WebSocketSubProtocol ( rawValue: requestedSubProtocol) {
92+ subProtocol = selectedSubProtocol
93+ break
94+ }
95+ }
96+ }
97+ guard let subProtocol = subProtocol else {
98+ // If they provided options but none matched, fail
99+ throw Abort ( . badRequest, reason: " Unable to negotiate subprotocol. \( WebSocketSubProtocol . allCases) are supported. " )
100+ }
101+ return subProtocol
102+ }
99103}
0 commit comments