Skip to content

Commit 4033b03

Browse files
author
Garrett Moseke
authored
fix: add ID parameter for completion/error callbacks
1 parent bd21d75 commit 4033b03

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

Sources/GraphQLTransportWS/Server.swift

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import GraphQLRxSwift
66
import NIO
77
import RxSwift
88

9-
/// Server implements the server-side portion of the protocol, allowing a few callbacks for customization. 0 or 1 subscriptions per connection and no more.
9+
/// Server implements the server-side portion of the protocol, allowing a few callbacks for customization.
1010
///
1111
/// By default, there are no authorization checks
1212
public class Server<InitPayload: Equatable & Codable> {
@@ -18,8 +18,8 @@ public class Server<InitPayload: Equatable & Codable> {
1818

1919
var auth: (InitPayload) throws -> Void = { _ in }
2020
var onExit: () -> Void = { }
21-
var onOperationComplete: () -> Void = {}
22-
var onOperationError: () -> Void = {}
21+
var onOperationComplete: (String) -> Void = { _ in }
22+
var onOperationError: (String) -> Void = { _ in }
2323
var onMessage: (String) -> Void = { _ in }
2424

2525
var initialized = false
@@ -66,7 +66,7 @@ public class Server<InitPayload: Equatable & Codable> {
6666
return
6767
}
6868

69-
// handle incoing message
69+
// handle incoming message
7070
switch request.type {
7171
case .connectionInit:
7272
guard let connectionInitRequest = try? self.decoder.decode(ConnectionInitRequest<InitPayload>.self, from: data) else {
@@ -85,15 +85,16 @@ public class Server<InitPayload: Equatable & Codable> {
8585
self.error(.invalidRequestFormat(messageType: .complete))
8686
return
8787
}
88-
self.onSubscribeComplete(completeRequest)
88+
self.onOperationComplete(completeRequest.id)
8989
case .unknown:
9090
self.error(.invalidType())
9191
}
9292
}
9393
}
9494

9595
/// Define the callback run during `connection_init` resolution that allows authorization using the `payload`.
96-
/// Throw to indicate that authorization has failed. /// - Parameter callback: The callback to assign
96+
/// Throw to indicate that authorization has failed.
97+
/// - Parameter callback: The callback to assign
9798
public func auth(_ callback: @escaping (InitPayload) throws -> Void) {
9899
self.auth = callback
99100
}
@@ -111,14 +112,14 @@ public class Server<InitPayload: Equatable & Codable> {
111112
}
112113

113114
/// Define the callback run on the completion a full operation (query/mutation, end of subscription)
114-
/// - Parameter callback: The callback to assign
115-
public func onOperationComplete(_ callback: @escaping () -> Void) {
115+
/// - Parameter callback: The callback to assign, taking a string parameter for the ID of the operation
116+
public func onOperationComplete(_ callback: @escaping (String) -> Void) {
116117
self.onOperationComplete = callback
117118
}
118119

119120
/// Define the callback to run on error of any full operation (failed query, interrupted subscription)
120-
/// - Parameter callback: The callback to assign
121-
public func onOperationError(_ callback: @escaping () -> Void) {
121+
/// - Parameter callback: The callback to assign, taking a string parameter for the ID of the operation
122+
public func onOperationError(_ callback: @escaping (String) -> Void) {
122123
self.onOperationError = callback
123124
}
124125

@@ -208,14 +209,6 @@ public class Server<InitPayload: Equatable & Codable> {
208209
}
209210
}
210211

211-
private func onSubscribeComplete(_: CompleteRequest) {
212-
guard initialized else {
213-
self.error(.notInitialized())
214-
return
215-
}
216-
onOperationComplete()
217-
}
218-
219212
/// Send a `connection_ack` response through the messenger
220213
private func sendConnectionAck(_ payload: [String: Map]? = nil) {
221214
guard let messenger = messenger else { return }
@@ -243,7 +236,7 @@ public class Server<InitPayload: Equatable & Codable> {
243236
id: id
244237
).toJSON(encoder)
245238
)
246-
self.onOperationComplete()
239+
self.onOperationComplete(id)
247240
}
248241

249242
/// Send an `error` response through the messenger
@@ -255,7 +248,7 @@ public class Server<InitPayload: Equatable & Codable> {
255248
id: id
256249
).toJSON(encoder)
257250
)
258-
self.onOperationError()
251+
self.onOperationError(id)
259252
}
260253

261254
/// Send an `error` response through the messenger

0 commit comments

Comments
 (0)