Skip to content

Commit 5a993cd

Browse files
feat: Adds Data support to Messenger
This is backwards compatible support for Data sending.
1 parent 30dcc49 commit 5a993cd

6 files changed

Lines changed: 64 additions & 61 deletions

File tree

Sources/GraphQLWS/Client.swift

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -141,35 +141,43 @@ public actor Client<InitPayload: Equatable & Codable> {
141141
/// Send a `connection_init` request through the messenger
142142
public func sendConnectionInit(payload: InitPayload) async throws {
143143
try await messenger.send(
144-
ConnectionInitRequest(
145-
payload: payload
146-
).toJSON(encoder)
144+
encoder.encode(
145+
ConnectionInitRequest(
146+
payload: payload
147+
)
148+
)
147149
)
148150
}
149151

150152
/// Send a `start` request through the messenger
151153
public func sendStart(payload: GraphQLRequest, id: String) async throws {
152154
try await messenger.send(
153-
StartRequest(
154-
payload: payload,
155-
id: id
156-
).toJSON(encoder)
155+
encoder.encode(
156+
StartRequest(
157+
payload: payload,
158+
id: id
159+
)
160+
)
157161
)
158162
}
159163

160164
/// Send a `stop` request through the messenger
161165
public func sendStop(id: String) async throws {
162166
try await messenger.send(
163-
StopRequest(
164-
id: id
165-
).toJSON(encoder)
167+
encoder.encode(
168+
StopRequest(
169+
id: id
170+
)
171+
)
166172
)
167173
}
168174

169175
/// Send a `connection_terminate` request through the messenger
170176
public func sendConnectionTerminate() async throws {
171177
try await messenger.send(
172-
ConnectionTerminateRequest().toJSON(encoder)
178+
encoder.encode(
179+
ConnectionTerminateRequest()
180+
)
173181
)
174182
}
175183

Sources/GraphQLWS/JsonEncodable.swift

Lines changed: 0 additions & 23 deletions
This file was deleted.

Sources/GraphQLWS/Messenger.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,15 @@ 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 a message through this messenger
21+
/// - Parameter message: The message to send
22+
func send(_ message: Data) async throws {
23+
// TODO: Ideally Data is our native interface, and String is the extension.
24+
// Since that change is breaking, we will do it on the next major version.
25+
if let string = String(data: message, encoding: .utf8) {
26+
try await send(string)
27+
}
28+
}
29+
}

Sources/GraphQLWS/Requests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import Foundation
22
import GraphQL
33

44
/// A general request. This object's type is used to triage to other, more specific request objects.
5-
public struct Request: Equatable, JsonEncodable {
5+
public struct Request: Equatable, Codable {
66
public let type: RequestMessageType
77
}
88

99
/// A websocket `connection_init` request from the client to the server
10-
public struct ConnectionInitRequest<InitPayload: Codable & Equatable>: Equatable, JsonEncodable {
10+
public struct ConnectionInitRequest<InitPayload: Codable & Equatable>: Equatable, Codable {
1111
public let type: RequestMessageType = .GQL_CONNECTION_INIT
1212
public let payload: InitPayload
1313

@@ -31,7 +31,7 @@ public struct ConnectionInitRequest<InitPayload: Codable & Equatable>: Equatable
3131
}
3232

3333
/// A websocket `start` request from the client to the server
34-
public struct StartRequest: Equatable, JsonEncodable {
34+
public struct StartRequest: Equatable, Codable {
3535
public let type: RequestMessageType = .GQL_START
3636
public let payload: GraphQLRequest
3737
public let id: String
@@ -57,7 +57,7 @@ public struct StartRequest: Equatable, JsonEncodable {
5757
}
5858

5959
/// A websocket `stop` request from the client to the server
60-
public struct StopRequest: Equatable, JsonEncodable {
60+
public struct StopRequest: Equatable, Codable {
6161
public let type: RequestMessageType = .GQL_STOP
6262
public let id: String
6363

@@ -81,7 +81,7 @@ public struct StopRequest: Equatable, JsonEncodable {
8181
}
8282

8383
/// A websocket `connection_terminate` request from the client to the server
84-
public struct ConnectionTerminateRequest: Equatable, JsonEncodable {
84+
public struct ConnectionTerminateRequest: Equatable, Codable {
8585
public let type: RequestMessageType = .GQL_CONNECTION_TERMINATE
8686

8787
public init() {}

Sources/GraphQLWS/Responses.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import Foundation
22
import GraphQL
33

44
/// A general response. This object's type is used to triage to other, more specific response objects.
5-
public struct Response: Equatable, JsonEncodable {
5+
public struct Response: Equatable, Codable {
66
public let type: ResponseMessageType
77
}
88

99
/// A websocket `connection_ack` response from the server to the client
10-
public struct ConnectionAckResponse: Equatable, JsonEncodable {
10+
public struct ConnectionAckResponse: Equatable, Codable {
1111
public let type: ResponseMessageType = .GQL_CONNECTION_ACK
1212
public let payload: [String: Map]?
1313

@@ -31,7 +31,7 @@ public struct ConnectionAckResponse: Equatable, JsonEncodable {
3131
}
3232

3333
/// A websocket `connection_error` response from the server to the client
34-
public struct ConnectionErrorResponse: Equatable, JsonEncodable {
34+
public struct ConnectionErrorResponse: Equatable, Codable {
3535
public let type: ResponseMessageType = .GQL_CONNECTION_ERROR
3636
public let payload: [String: Map]?
3737

@@ -55,7 +55,7 @@ public struct ConnectionErrorResponse: Equatable, JsonEncodable {
5555
}
5656

5757
/// A websocket `ka` response from the server to the client
58-
public struct ConnectionKeepAliveResponse: Equatable, JsonEncodable {
58+
public struct ConnectionKeepAliveResponse: Equatable, Codable {
5959
public let type: ResponseMessageType = .GQL_CONNECTION_KEEP_ALIVE
6060
public let payload: [String: Map]?
6161

@@ -81,7 +81,7 @@ public struct ConnectionKeepAliveResponse: Equatable, JsonEncodable {
8181
}
8282

8383
/// A websocket `data` response from the server to the client
84-
public struct DataResponse: Equatable, JsonEncodable {
84+
public struct DataResponse: Equatable, Codable {
8585
public let type: ResponseMessageType = .GQL_DATA
8686
public let payload: GraphQLResult?
8787
public let id: String
@@ -107,7 +107,7 @@ public struct DataResponse: Equatable, JsonEncodable {
107107
}
108108

109109
/// A websocket `complete` response from the server to the client
110-
public struct CompleteResponse: Equatable, JsonEncodable {
110+
public struct CompleteResponse: Equatable, Codable {
111111
public let type: ResponseMessageType = .GQL_COMPLETE
112112
public let id: String
113113

@@ -130,7 +130,7 @@ public struct CompleteResponse: Equatable, JsonEncodable {
130130
}
131131

132132
/// A websocket `error` response from the server to the client
133-
public struct ErrorResponse: Equatable, JsonEncodable {
133+
public struct ErrorResponse: Equatable, Codable {
134134
public let type: ResponseMessageType = .GQL_ERROR
135135
public let payload: [GraphQLError]
136136
public let id: String
@@ -203,7 +203,7 @@ public struct ResponseMessageType: Equatable, Codable, Sendable {
203203

204204
/// A websocket `error` response from the server to the client that indicates an issue with encoding
205205
/// a response JSON
206-
struct EncodingErrorResponse: Equatable, Codable, JsonEncodable {
206+
struct EncodingErrorResponse: Equatable, Codable {
207207
let type: ResponseMessageType
208208
let payload: [String: String]
209209

Sources/GraphQLWS/Server.swift

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -226,51 +226,57 @@ where
226226
/// Send a `connection_ack` response through the messenger
227227
private func sendConnectionAck(_ payload: [String: Map]? = nil) async throws {
228228
try await messenger.send(
229-
ConnectionAckResponse(payload: payload).toJSON(encoder)
229+
encoder.encode(ConnectionAckResponse(payload: payload))
230230
)
231231
}
232232

233233
/// Send a `connection_error` response through the messenger
234234
private func sendConnectionError(_ payload: [String: Map]? = nil) async throws {
235235
try await messenger.send(
236-
ConnectionErrorResponse(payload: payload).toJSON(encoder)
236+
encoder.encode(ConnectionErrorResponse(payload: payload))
237237
)
238238
}
239239

240240
/// Send a `ka` response through the messenger
241241
private func sendConnectionKeepAlive(_ payload: [String: Map]? = nil) async throws {
242242
try await messenger.send(
243-
ConnectionKeepAliveResponse(payload: payload).toJSON(encoder)
243+
encoder.encode(ConnectionKeepAliveResponse(payload: payload))
244244
)
245245
}
246246

247247
/// Send a `data` response through the messenger
248248
private func sendData(_ payload: GraphQLResult? = nil, id: String) async throws {
249249
try await messenger.send(
250-
DataResponse(
251-
payload: payload,
252-
id: id
253-
).toJSON(encoder)
250+
encoder.encode(
251+
DataResponse(
252+
payload: payload,
253+
id: id
254+
)
255+
)
254256
)
255257
}
256258

257259
/// Send a `complete` response through the messenger
258260
private func sendComplete(id: String) async throws {
259261
try await messenger.send(
260-
CompleteResponse(
261-
id: id
262-
).toJSON(encoder)
262+
encoder.encode(
263+
CompleteResponse(
264+
id: id
265+
)
266+
)
263267
)
264268
try await onOperationComplete(id)
265269
}
266270

267271
/// Send an `error` response through the messenger
268272
private func sendError(_ errors: [Error], id: String) async throws {
269273
try await messenger.send(
270-
ErrorResponse(
271-
errors,
272-
id: id
273-
).toJSON(encoder)
274+
encoder.encode(
275+
ErrorResponse(
276+
errors,
277+
id: id
278+
)
279+
)
274280
)
275281
try await onOperationError(id, errors)
276282
}

0 commit comments

Comments
 (0)