Skip to content
Open
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
72 changes: 45 additions & 27 deletions Sources/GraphQLWS/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,65 +62,88 @@ public actor Client<InitPayload: Equatable & Codable> {
do {
response = try decoder.decode(Response.self, from: message)
} catch {
try await self.error(.noType())
try await messenger.error(.noType())
return
}

switch response.type {
case .GQL_CONNECTION_ERROR:
guard
let connectionErrorResponse = try? decoder.decode(
let connectionErrorResponse: ConnectionErrorResponse
do {
connectionErrorResponse = try decoder.decode(
ConnectionErrorResponse.self,
from: message
)
else {
try await error(.invalidResponseFormat(messageType: .GQL_CONNECTION_ERROR))
} catch {
try await messenger.error(
.invalidResponseFormat(messageType: .GQL_CONNECTION_ERROR, error: error)
)
return
}
try await onConnectionError(connectionErrorResponse, self)
case .GQL_CONNECTION_ACK:
guard
let connectionAckResponse = try? decoder.decode(
let connectionAckResponse: ConnectionAckResponse
do {
connectionAckResponse = try decoder.decode(
ConnectionAckResponse.self,
from: message
)
else {
try await error(.invalidResponseFormat(messageType: .GQL_CONNECTION_ERROR))
} catch {
try await messenger.error(
.invalidResponseFormat(messageType: .GQL_CONNECTION_ERROR, error: error)
)
return
}
try await onConnectionAck(connectionAckResponse, self)
case .GQL_CONNECTION_KEEP_ALIVE:
guard
let connectionKeepAliveResponse = try? decoder.decode(
let connectionKeepAliveResponse: ConnectionKeepAliveResponse
do {
connectionKeepAliveResponse = try decoder.decode(
ConnectionKeepAliveResponse.self,
from: message
)
else {
try await error(.invalidResponseFormat(messageType: .GQL_CONNECTION_KEEP_ALIVE))
} catch {
try await messenger.error(
.invalidResponseFormat(messageType: .GQL_CONNECTION_KEEP_ALIVE, error: error)
)
return
}
try await onConnectionKeepAlive(connectionKeepAliveResponse, self)
case .GQL_DATA:
guard let nextResponse = try? decoder.decode(DataResponse.self, from: message) else {
try await error(.invalidResponseFormat(messageType: .GQL_DATA))
let dataResponse: DataResponse
do {
dataResponse = try decoder.decode(DataResponse.self, from: message)
} catch {
try await messenger.error(
.invalidResponseFormat(messageType: .GQL_DATA, error: error)
)
return
}
try await onData(nextResponse, self)
try await onData(dataResponse, self)
case .GQL_ERROR:
guard let errorResponse = try? decoder.decode(ErrorResponse.self, from: message) else {
try await error(.invalidResponseFormat(messageType: .GQL_ERROR))
let errorResponse: ErrorResponse
do {
errorResponse = try decoder.decode(ErrorResponse.self, from: message)
} catch {
try await messenger.error(
.invalidResponseFormat(messageType: .GQL_ERROR, error: error)
)
return
}
try await onError(errorResponse, self)
case .GQL_COMPLETE:
guard let completeResponse = try? decoder.decode(CompleteResponse.self, from: message)
else {
try await error(.invalidResponseFormat(messageType: .GQL_COMPLETE))
let completeResponse: CompleteResponse
do {
completeResponse = try decoder.decode(CompleteResponse.self, from: message)
} catch {
try await messenger.error(
.invalidResponseFormat(messageType: .GQL_COMPLETE, error: error)
)
return
}
try await onComplete(completeResponse, self)
default:
try await error(.invalidType())
try await messenger.error(.invalidType())
}
}

Expand Down Expand Up @@ -166,9 +189,4 @@ public actor Client<InitPayload: Equatable & Codable> {
)
)
}

/// Send an error through the messenger and close the connection
private func error(_ error: GraphQLWSError) async throws {
try await messenger.error(error.message, code: error.code.rawValue)
}
}
8 changes: 4 additions & 4 deletions Sources/GraphQLWS/GraphQLWSError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ struct GraphQLWSError: Error {
)
}

static func invalidRequestFormat(messageType: RequestMessageType) -> Self {
static func invalidRequestFormat(messageType: RequestMessageType, error: Error) -> Self {
return self.init(
"Request message doesn't match '\(messageType.type.rawValue)' JSON format",
"Request message doesn't match '\(messageType.type.rawValue)' JSON format: \(error)",
code: .invalidRequestFormat
)
}

static func invalidResponseFormat(messageType: ResponseMessageType) -> Self {
static func invalidResponseFormat(messageType: ResponseMessageType, error: Error) -> Self {
return self.init(
"Response message doesn't match '\(messageType.type.rawValue)' JSON format",
"Response message doesn't match '\(messageType.type.rawValue)' JSON format: \(error)",
code: .invalidResponseFormat
)
}
Expand Down
7 changes: 7 additions & 0 deletions Sources/GraphQLWS/Messenger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ public protocol Messenger: Sendable {
/// - code: An error code
func error(_ message: String, code: Int) async throws
}

extension Messenger {
/// Send an error through the messenger and close the connection
func error(_ error: GraphQLWSError) async throws {
try await self.error(error.message, code: error.code.rawValue)
}
}
3 changes: 1 addition & 2 deletions Sources/GraphQLWS/Requests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ public struct StopRequest: Equatable, Codable {

public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: Self.CodingKeys.self)
if try container.decode(RequestMessageType.self, forKey: .type) != .GQL_CONNECTION_TERMINATE
{
if try container.decode(RequestMessageType.self, forKey: .type) != .GQL_STOP {
throw DecodingError.dataCorrupted(
.init(
codingPath: decoder.codingPath,
Expand Down
Loading
Loading