Skip to content

Commit 2019bc3

Browse files
author
Garrett Moseke
authored
feat: add onOperationComplete & onOperationError callbacks
1 parent bf739f5 commit 2019bc3

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

Sources/GraphQLWS/Server.swift

+14-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public class Server<InitPayload: Equatable & Codable> {
1919
var auth: (InitPayload) throws -> Void = { _ in }
2020
var onExit: () -> Void = { }
2121
var onMessage: (String) -> Void = { _ in }
22-
var onStop: () -> Void = { }
22+
var onOperationComplete: () -> Void = {}
23+
var onOperationError: () -> Void = {}
2324

2425
var initialized = false
2526

@@ -118,10 +119,16 @@ public class Server<InitPayload: Equatable & Codable> {
118119
self.onMessage = callback
119120
}
120121

121-
/// Define the callback run on receipt of a`GQL_STOP` message
122+
/// Define the callback run on the completion a full operation (query/mutation, end of subscription)
122123
/// - Parameter callback: The callback to assign
123-
public func onStop(_ callback: @escaping () -> Void) {
124-
self.onStop = callback
124+
public func onOperationComplete(_ callback: @escaping () -> Void) {
125+
self.onOperationComplete = callback
126+
}
127+
128+
/// Define the callback to run on error of any full operation (failed query, interrupted subscription)
129+
/// - Parameter callback: The callback to assign
130+
public func onOperationError(_ callback: @escaping () -> Void) {
131+
self.onOperationError = callback
125132
}
126133

127134
private func onConnectionInit(_ connectionInitRequest: ConnectionInitRequest<InitPayload>, _ messenger: Messenger) {
@@ -214,7 +221,7 @@ public class Server<InitPayload: Equatable & Codable> {
214221
self.error(.notInitialized())
215222
return
216223
}
217-
onStop()
224+
onOperationComplete()
218225
}
219226

220227
private func onConnectionTerminate(_: ConnectionTerminateRequest, _ messenger: Messenger) {
@@ -265,6 +272,7 @@ public class Server<InitPayload: Equatable & Codable> {
265272
id: id
266273
).toJSON(encoder)
267274
)
275+
onOperationComplete()
268276
}
269277

270278
/// Send an `error` response through the messenger
@@ -276,6 +284,7 @@ public class Server<InitPayload: Equatable & Codable> {
276284
id: id
277285
).toJSON(encoder)
278286
)
287+
onOperationError()
279288
}
280289

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

0 commit comments

Comments
 (0)