Skip to content

Commit 9760a33

Browse files
feat: Adds Data support to Messenger
1 parent d0b7113 commit 9760a33

6 files changed

Lines changed: 61 additions & 58 deletions

File tree

Sources/GraphQLTransportWS/Client.swift

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public actor Client<InitPayload: Equatable & Codable> {
3535
self.onError = onError
3636
self.onComplete = onComplete
3737
}
38-
38+
3939
/// Listen and react to the provided async sequence of server messages. This function will block until the stream is completed.
4040
/// - Parameter incoming: The server message sequence that the client should react to.
4141
public func listen<A: AsyncSequence & Sendable>(to incoming: A) async throws
@@ -107,28 +107,34 @@ public actor Client<InitPayload: Equatable & Codable> {
107107
/// Send a `connection_init` request through the messenger
108108
public func sendConnectionInit(payload: InitPayload) async throws {
109109
try await messenger.send(
110-
ConnectionInitRequest(
111-
payload: payload
112-
).toJSON(encoder)
110+
encoder.encode(
111+
ConnectionInitRequest(
112+
payload: payload
113+
)
114+
)
113115
)
114116
}
115117

116118
/// Send a `subscribe` request through the messenger
117119
public func sendStart(payload: GraphQLRequest, id: String) async throws {
118120
try await messenger.send(
119-
SubscribeRequest(
120-
payload: payload,
121-
id: id
122-
).toJSON(encoder)
121+
encoder.encode(
122+
SubscribeRequest(
123+
payload: payload,
124+
id: id
125+
)
126+
)
123127
)
124128
}
125129

126130
/// Send a `complete` request through the messenger
127131
public func sendStop(id: String) async throws {
128132
try await messenger.send(
129-
CompleteRequest(
130-
id: id
131-
).toJSON(encoder)
133+
encoder.encode(
134+
CompleteRequest(
135+
id: id
136+
)
137+
)
132138
)
133139
}
134140

Sources/GraphQLTransportWS/JsonEncodable.swift

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

Sources/GraphQLTransportWS/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/GraphQLTransportWS/Requests.swift

Lines changed: 4 additions & 4 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 = .connectionInit
1212
public let payload: InitPayload
1313

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

3232
/// A websocket `subscribe` request from the client to the server
33-
public struct SubscribeRequest: Equatable, JsonEncodable {
33+
public struct SubscribeRequest: Equatable, Codable {
3434
public let type = RequestMessageType.subscribe
3535
public let payload: GraphQLRequest
3636
public let id: String
@@ -56,7 +56,7 @@ public struct SubscribeRequest: Equatable, JsonEncodable {
5656
}
5757

5858
/// A websocket `complete` request from the client to the server
59-
public struct CompleteRequest: Equatable, JsonEncodable {
59+
public struct CompleteRequest: Equatable, Codable {
6060
public let type = RequestMessageType.complete
6161
public let id: String
6262

Sources/GraphQLTransportWS/Responses.swift

Lines changed: 6 additions & 6 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 = .connectionAck
1212
public let payload: [String: Map]?
1313

@@ -30,7 +30,7 @@ public struct ConnectionAckResponse: Equatable, JsonEncodable {
3030
}
3131

3232
/// A websocket `next` response from the server to the client
33-
public struct NextResponse: Equatable, JsonEncodable {
33+
public struct NextResponse: Equatable, Codable {
3434
public let type: ResponseMessageType = .next
3535
public let payload: GraphQLResult?
3636
public let id: String
@@ -56,7 +56,7 @@ public struct NextResponse: Equatable, JsonEncodable {
5656
}
5757

5858
/// A websocket `complete` response from the server to the client
59-
public struct CompleteResponse: Equatable, JsonEncodable {
59+
public struct CompleteResponse: Equatable, Codable {
6060
public let type: ResponseMessageType = .complete
6161
public let id: String
6262

@@ -79,7 +79,7 @@ public struct CompleteResponse: Equatable, JsonEncodable {
7979
}
8080

8181
/// A websocket `error` response from the server to the client
82-
public struct ErrorResponse: Equatable, JsonEncodable {
82+
public struct ErrorResponse: Equatable, Codable {
8383
public let type: ResponseMessageType = .error
8484
public let payload: [GraphQLError]
8585
public let id: String
@@ -148,7 +148,7 @@ public struct ResponseMessageType: Equatable, Codable, Sendable {
148148

149149
/// A websocket `error` response from the server to the client that indicates an issue with encoding
150150
/// a response JSON
151-
struct EncodingErrorResponse: Equatable, Codable, JsonEncodable {
151+
struct EncodingErrorResponse: Equatable, Codable {
152152
let type: ResponseMessageType
153153
let payload: [String: String]
154154

Sources/GraphQLTransportWS/Server.swift

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ where
5151
self.onOperationComplete = onOperationComplete
5252
self.onOperationError = onOperationError
5353
}
54-
54+
5555
deinit {
5656
subscriptionTasks.values.forEach { $0.cancel() }
5757
}
58-
58+
5959
/// Listen and react to the provided async sequence of client messages. This function will block until the stream is completed.
6060
/// - Parameter incoming: The client message sequence that the server should react to.
6161
public func listen<A: AsyncSequence & Sendable>(to incoming: A) async throws
@@ -206,37 +206,45 @@ where
206206
/// Send a `connection_ack` response through the messenger
207207
private func sendConnectionAck(_ payload: [String: Map]? = nil) async throws {
208208
try await messenger.send(
209-
ConnectionAckResponse(payload: payload).toJSON(encoder)
209+
encoder.encode(
210+
ConnectionAckResponse(payload: payload)
211+
)
210212
)
211213
}
212214

213215
/// Send a `next` response through the messenger
214216
private func sendNext(_ payload: GraphQLResult? = nil, id: String) async throws {
215217
try await messenger.send(
216-
NextResponse(
217-
payload: payload,
218-
id: id
219-
).toJSON(encoder)
218+
encoder.encode(
219+
NextResponse(
220+
payload: payload,
221+
id: id
222+
)
223+
)
220224
)
221225
}
222226

223227
/// Send a `complete` response through the messenger
224228
private func sendComplete(id: String) async throws {
225229
try await messenger.send(
226-
CompleteResponse(
227-
id: id
228-
).toJSON(encoder)
230+
encoder.encode(
231+
CompleteResponse(
232+
id: id
233+
)
234+
)
229235
)
230236
try await onOperationComplete(id)
231237
}
232238

233239
/// Send an `error` response through the messenger
234240
private func sendError(_ errors: [Error], id: String) async throws {
235241
try await messenger.send(
236-
ErrorResponse(
237-
errors,
238-
id: id
239-
).toJSON(encoder)
242+
encoder.encode(
243+
ErrorResponse(
244+
errors,
245+
id: id
246+
)
247+
)
240248
)
241249
try await onOperationError(id, errors)
242250
}

0 commit comments

Comments
 (0)