Skip to content

Commit dc54805

Browse files
Merge pull request #12 from GraphQLSwift/experiment/parallel-execution
GraphQL executions are handled in parallel
2 parents 038a19b + 56a5cab commit dc54805

6 files changed

Lines changed: 186 additions & 92 deletions

File tree

Sources/GraphQLWS/Client.swift

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -62,65 +62,88 @@ public actor Client<InitPayload: Equatable & Codable> {
6262
do {
6363
response = try decoder.decode(Response.self, from: message)
6464
} catch {
65-
try await self.error(.noType())
65+
try await messenger.error(.noType())
6666
return
6767
}
6868

6969
switch response.type {
7070
case .GQL_CONNECTION_ERROR:
71-
guard
72-
let connectionErrorResponse = try? decoder.decode(
71+
let connectionErrorResponse: ConnectionErrorResponse
72+
do {
73+
connectionErrorResponse = try decoder.decode(
7374
ConnectionErrorResponse.self,
7475
from: message
7576
)
76-
else {
77-
try await error(.invalidResponseFormat(messageType: .GQL_CONNECTION_ERROR))
77+
} catch {
78+
try await messenger.error(
79+
.invalidResponseFormat(messageType: .GQL_CONNECTION_ERROR, error: error)
80+
)
7881
return
7982
}
8083
try await onConnectionError(connectionErrorResponse, self)
8184
case .GQL_CONNECTION_ACK:
82-
guard
83-
let connectionAckResponse = try? decoder.decode(
85+
let connectionAckResponse: ConnectionAckResponse
86+
do {
87+
connectionAckResponse = try decoder.decode(
8488
ConnectionAckResponse.self,
8589
from: message
8690
)
87-
else {
88-
try await error(.invalidResponseFormat(messageType: .GQL_CONNECTION_ERROR))
91+
} catch {
92+
try await messenger.error(
93+
.invalidResponseFormat(messageType: .GQL_CONNECTION_ERROR, error: error)
94+
)
8995
return
9096
}
9197
try await onConnectionAck(connectionAckResponse, self)
9298
case .GQL_CONNECTION_KEEP_ALIVE:
93-
guard
94-
let connectionKeepAliveResponse = try? decoder.decode(
99+
let connectionKeepAliveResponse: ConnectionKeepAliveResponse
100+
do {
101+
connectionKeepAliveResponse = try decoder.decode(
95102
ConnectionKeepAliveResponse.self,
96103
from: message
97104
)
98-
else {
99-
try await error(.invalidResponseFormat(messageType: .GQL_CONNECTION_KEEP_ALIVE))
105+
} catch {
106+
try await messenger.error(
107+
.invalidResponseFormat(messageType: .GQL_CONNECTION_KEEP_ALIVE, error: error)
108+
)
100109
return
101110
}
102111
try await onConnectionKeepAlive(connectionKeepAliveResponse, self)
103112
case .GQL_DATA:
104-
guard let nextResponse = try? decoder.decode(DataResponse.self, from: message) else {
105-
try await error(.invalidResponseFormat(messageType: .GQL_DATA))
113+
let dataResponse: DataResponse
114+
do {
115+
dataResponse = try decoder.decode(DataResponse.self, from: message)
116+
} catch {
117+
try await messenger.error(
118+
.invalidResponseFormat(messageType: .GQL_DATA, error: error)
119+
)
106120
return
107121
}
108-
try await onData(nextResponse, self)
122+
try await onData(dataResponse, self)
109123
case .GQL_ERROR:
110-
guard let errorResponse = try? decoder.decode(ErrorResponse.self, from: message) else {
111-
try await error(.invalidResponseFormat(messageType: .GQL_ERROR))
124+
let errorResponse: ErrorResponse
125+
do {
126+
errorResponse = try decoder.decode(ErrorResponse.self, from: message)
127+
} catch {
128+
try await messenger.error(
129+
.invalidResponseFormat(messageType: .GQL_ERROR, error: error)
130+
)
112131
return
113132
}
114133
try await onError(errorResponse, self)
115134
case .GQL_COMPLETE:
116-
guard let completeResponse = try? decoder.decode(CompleteResponse.self, from: message)
117-
else {
118-
try await error(.invalidResponseFormat(messageType: .GQL_COMPLETE))
135+
let completeResponse: CompleteResponse
136+
do {
137+
completeResponse = try decoder.decode(CompleteResponse.self, from: message)
138+
} catch {
139+
try await messenger.error(
140+
.invalidResponseFormat(messageType: .GQL_COMPLETE, error: error)
141+
)
119142
return
120143
}
121144
try await onComplete(completeResponse, self)
122145
default:
123-
try await error(.invalidType())
146+
try await messenger.error(.invalidType())
124147
}
125148
}
126149

@@ -166,9 +189,4 @@ public actor Client<InitPayload: Equatable & Codable> {
166189
)
167190
)
168191
}
169-
170-
/// Send an error through the messenger and close the connection
171-
private func error(_ error: GraphQLWSError) async throws {
172-
try await messenger.error(error.message, code: error.code.rawValue)
173-
}
174192
}

Sources/GraphQLWS/GraphQLWSError.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,16 @@ struct GraphQLWSError: Error {
5858
)
5959
}
6060

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

68-
static func invalidResponseFormat(messageType: ResponseMessageType) -> Self {
68+
static func invalidResponseFormat(messageType: ResponseMessageType, error: Error) -> Self {
6969
return self.init(
70-
"Response message doesn't match '\(messageType.type.rawValue)' JSON format",
70+
"Response message doesn't match '\(messageType.type.rawValue)' JSON format: \(error)",
7171
code: .invalidResponseFormat
7272
)
7373
}

Sources/GraphQLWS/Messenger.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,10 @@ public protocol Messenger: Sendable {
1515
/// - code: An error code
1616
func error(_ message: String, code: Int) async throws
1717
}
18+
19+
extension Messenger {
20+
/// Send an error through the messenger and close the connection
21+
func error(_ error: GraphQLWSError) async throws {
22+
try await self.error(error.message, code: error.code.rawValue)
23+
}
24+
}

Sources/GraphQLWS/Requests.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ public struct StopRequest: Equatable, Codable {
6767

6868
public init(from decoder: any Decoder) throws {
6969
let container = try decoder.container(keyedBy: Self.CodingKeys.self)
70-
if try container.decode(RequestMessageType.self, forKey: .type) != .GQL_CONNECTION_TERMINATE
71-
{
70+
if try container.decode(RequestMessageType.self, forKey: .type) != .GQL_STOP {
7271
throw DecodingError.dataCorrupted(
7372
.init(
7473
codingPath: decoder.codingPath,

0 commit comments

Comments
 (0)