Skip to content

Commit 72be825

Browse files
Merge pull request #2 from GNMoseke/fix/expose-on-stop
feat: add onOperationComplete & onOperationError callbacks
2 parents f051242 + 6462f86 commit 72be825

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

Sources/GraphQLWS/Server.swift

+18-8
Original file line numberDiff line numberDiff line change
@@ -19,6 +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 onOperationComplete: (String) -> Void = { _ in }
23+
var onOperationError: (String) -> Void = { _ in }
2224

2325
var initialized = false
2426

@@ -66,6 +68,7 @@ public class Server<InitPayload: Equatable & Codable> {
6668
return
6769
}
6870

71+
// handle incoming message
6972
switch request.type {
7073
case .GQL_CONNECTION_INIT:
7174
guard let connectionInitRequest = try? self.decoder.decode(ConnectionInitRequest<InitPayload>.self, from: json) else {
@@ -84,7 +87,7 @@ public class Server<InitPayload: Equatable & Codable> {
8487
self.error(.invalidRequestFormat(messageType: .GQL_STOP))
8588
return
8689
}
87-
self.onStop(stopRequest, messenger)
90+
self.onOperationComplete(stopRequest.id)
8891
case .GQL_CONNECTION_TERMINATE:
8992
guard let connectionTerminateRequest = try? self.decoder.decode(ConnectionTerminateRequest.self, from: json) else {
9093
self.error(.invalidRequestFormat(messageType: .GQL_CONNECTION_TERMINATE))
@@ -116,6 +119,18 @@ public class Server<InitPayload: Equatable & Codable> {
116119
self.onMessage = callback
117120
}
118121

122+
/// Define the callback run on the completion a full operation (query/mutation, end of subscription)
123+
/// - Parameter callback: The callback to assign
124+
public func onOperationComplete(_ callback: @escaping (String) -> 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 (String) -> Void) {
131+
self.onOperationError = callback
132+
}
133+
119134
private func onConnectionInit(_ connectionInitRequest: ConnectionInitRequest<InitPayload>, _ messenger: Messenger) {
120135
guard !initialized else {
121136
self.error(.tooManyInitializations())
@@ -201,13 +216,6 @@ public class Server<InitPayload: Equatable & Codable> {
201216
}
202217
}
203218

204-
private func onStop(_: StopRequest, _ messenger: Messenger) {
205-
guard initialized else {
206-
self.error(.notInitialized())
207-
return
208-
}
209-
}
210-
211219
private func onConnectionTerminate(_: ConnectionTerminateRequest, _ messenger: Messenger) {
212220
onExit()
213221
_ = messenger.close()
@@ -256,6 +264,7 @@ public class Server<InitPayload: Equatable & Codable> {
256264
id: id
257265
).toJSON(encoder)
258266
)
267+
onOperationComplete(id)
259268
}
260269

261270
/// Send an `error` response through the messenger
@@ -267,6 +276,7 @@ public class Server<InitPayload: Equatable & Codable> {
267276
id: id
268277
).toJSON(encoder)
269278
)
279+
onOperationError(id)
270280
}
271281

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

0 commit comments

Comments
 (0)