From 52bcca9c64c726564da2374c13755dee586f11a6 Mon Sep 17 00:00:00 2001 From: Jay Herron Date: Fri, 6 Feb 2026 00:31:45 -0700 Subject: [PATCH 1/2] refactor: Split out negotiateSubProtocol --- .../GraphQLHandler+handleWebSocket.swift | 42 ++++++++++--------- Tests/GraphQLVaporTests/HTTPTests.swift | 2 +- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/Sources/GraphQLVapor/WebSocket/GraphQLHandler+handleWebSocket.swift b/Sources/GraphQLVapor/WebSocket/GraphQLHandler+handleWebSocket.swift index e0d6f2a..20a4499 100644 --- a/Sources/GraphQLVapor/WebSocket/GraphQLHandler+handleWebSocket.swift +++ b/Sources/GraphQLVapor/WebSocket/GraphQLHandler+handleWebSocket.swift @@ -8,25 +8,7 @@ extension GraphQLHandler { func handleWebSocket( request: Request ) async throws -> Response { - var subProtocol: WebSocketSubProtocol? - let requestedSubProtocols = request.headers["Sec-WebSocket-Protocol"] - if requestedSubProtocols.isEmpty { - // Default - subProtocol = .graphqlTransportWs - } else { - // Choose highest client preference that we understand - for requestedSubProtocol in requestedSubProtocols { - if let selectedSubProtocol = WebSocketSubProtocol(rawValue: requestedSubProtocol) { - subProtocol = selectedSubProtocol - break - } - } - } - guard let subProtocol = subProtocol else { - // If they provided options but none matched, fail - throw Abort(.badRequest, reason: "Unable to negotiate subprotocol. \(WebSocketSubProtocol.allCases) are supported.") - } - + let subProtocol = try negotiateSubProtocol(request: request) let context = try await computeContext(request) let response = Response(status: .switchingProtocols) response.upgrader = WebSocketUpgrader( @@ -96,4 +78,26 @@ extension GraphQLHandler { ) return response } + + func negotiateSubProtocol(request: Request) throws -> WebSocketSubProtocol { + var subProtocol: WebSocketSubProtocol? + let requestedSubProtocols = request.headers["Sec-WebSocket-Protocol"] + if requestedSubProtocols.isEmpty { + // Default + subProtocol = .graphqlTransportWs + } else { + // Choose highest client preference that we understand + for requestedSubProtocol in requestedSubProtocols { + if let selectedSubProtocol = WebSocketSubProtocol(rawValue: requestedSubProtocol) { + subProtocol = selectedSubProtocol + break + } + } + } + guard let subProtocol = subProtocol else { + // If they provided options but none matched, fail + throw Abort(.badRequest, reason: "Unable to negotiate subprotocol. \(WebSocketSubProtocol.allCases) are supported.") + } + return subProtocol + } } diff --git a/Tests/GraphQLVaporTests/HTTPTests.swift b/Tests/GraphQLVaporTests/HTTPTests.swift index 5d857aa..c343ab2 100644 --- a/Tests/GraphQLVaporTests/HTTPTests.swift +++ b/Tests/GraphQLVaporTests/HTTPTests.swift @@ -223,7 +223,7 @@ struct HTTPTests { EmptyContext() } - try await app.test(.GET, "/graphql?query=%7Btest%7D", headers: defaultHeaders) { _ in + try await app.test(.GET, "/graphql?query=%7Bhello%7D", headers: defaultHeaders) { _ in } afterResponse: { response in #expect(response.status == .methodNotAllowed) } From 94824087820e4862cd81c39110dbf77446c02ee7 Mon Sep 17 00:00:00 2001 From: Jay Herron Date: Fri, 6 Feb 2026 00:39:57 -0700 Subject: [PATCH 2/2] refactor: removes unnecessary initializer --- Sources/GraphQLVapor/GraphQLHandler.swift | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/Sources/GraphQLVapor/GraphQLHandler.swift b/Sources/GraphQLVapor/GraphQLHandler.swift index ac7477b..3655ed7 100644 --- a/Sources/GraphQLVapor/GraphQLHandler.swift +++ b/Sources/GraphQLVapor/GraphQLHandler.swift @@ -9,16 +9,4 @@ struct GraphQLHandler< let rootValue: any Sendable let config: GraphQLConfig let computeContext: @Sendable (Request) async throws -> Context - - init( - schema: GraphQLSchema, - rootValue: any Sendable, - config: GraphQLConfig, - computeContext: @Sendable @escaping (Request) async throws -> Context - ) { - self.schema = schema - self.rootValue = rootValue - self.config = config - self.computeContext = computeContext - } }