Skip to content

Commit 37de2c4

Browse files
Merge pull request #2 from GNMoseke/fix/expose-on-complete
feat: add onOperationComplete & onOperationError callbacks
2 parents 5050078 + 4033b03 commit 37de2c4

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

Sources/GraphQLTransportWS/Server.swift

+20-9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public class Server<InitPayload: Equatable & Codable> {
1818

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

2325
var initialized = false
@@ -64,6 +66,7 @@ public class Server<InitPayload: Equatable & Codable> {
6466
return
6567
}
6668

69+
// handle incoming message
6770
switch request.type {
6871
case .connectionInit:
6972
guard let connectionInitRequest = try? self.decoder.decode(ConnectionInitRequest<InitPayload>.self, from: data) else {
@@ -82,15 +85,16 @@ public class Server<InitPayload: Equatable & Codable> {
8285
self.error(.invalidRequestFormat(messageType: .complete))
8386
return
8487
}
85-
self.onComplete(completeRequest)
88+
self.onOperationComplete(completeRequest.id)
8689
case .unknown:
8790
self.error(.invalidType())
8891
}
8992
}
9093
}
9194

9295
/// Define the callback run during `connection_init` resolution that allows authorization using the `payload`.
93-
/// 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
9498
public func auth(_ callback: @escaping (InitPayload) throws -> Void) {
9599
self.auth = callback
96100
}
@@ -107,6 +111,18 @@ public class Server<InitPayload: Equatable & Codable> {
107111
self.onMessage = callback
108112
}
109113

114+
/// Define the callback run on the completion a full operation (query/mutation, end of subscription)
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) {
117+
self.onOperationComplete = callback
118+
}
119+
120+
/// Define the callback to run on error of any full operation (failed query, interrupted subscription)
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) {
123+
self.onOperationError = callback
124+
}
125+
110126
private func onConnectionInit(_ connectionInitRequest: ConnectionInitRequest<InitPayload>) {
111127
guard !initialized else {
112128
self.error(.tooManyInitializations())
@@ -193,13 +209,6 @@ public class Server<InitPayload: Equatable & Codable> {
193209
}
194210
}
195211

196-
private func onComplete(_: CompleteRequest) {
197-
guard initialized else {
198-
self.error(.notInitialized())
199-
return
200-
}
201-
}
202-
203212
/// Send a `connection_ack` response through the messenger
204213
private func sendConnectionAck(_ payload: [String: Map]? = nil) {
205214
guard let messenger = messenger else { return }
@@ -227,6 +236,7 @@ public class Server<InitPayload: Equatable & Codable> {
227236
id: id
228237
).toJSON(encoder)
229238
)
239+
self.onOperationComplete(id)
230240
}
231241

232242
/// Send an `error` response through the messenger
@@ -238,6 +248,7 @@ public class Server<InitPayload: Equatable & Codable> {
238248
id: id
239249
).toJSON(encoder)
240250
)
251+
self.onOperationError(id)
241252
}
242253

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

0 commit comments

Comments
 (0)