Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions Sources/GraphQLVapor/GraphQLHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,4 @@ struct GraphQLHandler<
let rootValue: any Sendable
let config: GraphQLConfig<WebSocketInit>
let computeContext: @Sendable (Request) async throws -> Context

init(
schema: GraphQLSchema,
rootValue: any Sendable,
config: GraphQLConfig<WebSocketInit>,
computeContext: @Sendable @escaping (Request) async throws -> Context
) {
self.schema = schema
self.rootValue = rootValue
self.config = config
self.computeContext = computeContext
}
}
42 changes: 23 additions & 19 deletions Sources/GraphQLVapor/WebSocket/GraphQLHandler+handleWebSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
}
}
2 changes: 1 addition & 1 deletion Tests/GraphQLVaporTests/HTTPTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down