Skip to content

Commit 61952ff

Browse files
Merge pull request #3 from GraphQLSwift/refactor/negotiateSubProtocol
Breaks out WebSocket subprotocol negotiation
2 parents 446eb3c + 9482408 commit 61952ff

File tree

3 files changed

+24
-32
lines changed

3 files changed

+24
-32
lines changed

Sources/GraphQLVapor/GraphQLHandler.swift

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,4 @@ struct GraphQLHandler<
99
let rootValue: any Sendable
1010
let config: GraphQLConfig<WebSocketInit>
1111
let computeContext: @Sendable (Request) async throws -> Context
12-
13-
init(
14-
schema: GraphQLSchema,
15-
rootValue: any Sendable,
16-
config: GraphQLConfig<WebSocketInit>,
17-
computeContext: @Sendable @escaping (Request) async throws -> Context
18-
) {
19-
self.schema = schema
20-
self.rootValue = rootValue
21-
self.config = config
22-
self.computeContext = computeContext
23-
}
2412
}

Sources/GraphQLVapor/WebSocket/GraphQLHandler+handleWebSocket.swift

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

Tests/GraphQLVaporTests/HTTPTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ struct HTTPTests {
223223
EmptyContext()
224224
}
225225

226-
try await app.test(.GET, "/graphql?query=%7Btest%7D", headers: defaultHeaders) { _ in
226+
try await app.test(.GET, "/graphql?query=%7Bhello%7D", headers: defaultHeaders) { _ in
227227
} afterResponse: { response in
228228
#expect(response.status == .methodNotAllowed)
229229
}

0 commit comments

Comments
 (0)