@@ -19,6 +19,8 @@ public class Server<InitPayload: Equatable & Codable> {
19
19
var auth : ( InitPayload ) throws -> Void = { _ in }
20
20
var onExit : ( ) -> Void = { }
21
21
var onMessage : ( String ) -> Void = { _ in }
22
+ var onOperationComplete : ( String ) -> Void = { _ in }
23
+ var onOperationError : ( String ) -> Void = { _ in }
22
24
23
25
var initialized = false
24
26
@@ -66,6 +68,7 @@ public class Server<InitPayload: Equatable & Codable> {
66
68
return
67
69
}
68
70
71
+ // handle incoming message
69
72
switch request. type {
70
73
case . GQL_CONNECTION_INIT:
71
74
guard let connectionInitRequest = try ? self . decoder. decode ( ConnectionInitRequest< InitPayload> . self , from: json) else {
@@ -84,7 +87,7 @@ public class Server<InitPayload: Equatable & Codable> {
84
87
self . error ( . invalidRequestFormat( messageType: . GQL_STOP) )
85
88
return
86
89
}
87
- self . onStop ( stopRequest, messenger )
90
+ self . onOperationComplete ( stopRequest. id )
88
91
case . GQL_CONNECTION_TERMINATE:
89
92
guard let connectionTerminateRequest = try ? self . decoder. decode ( ConnectionTerminateRequest . self, from: json) else {
90
93
self . error ( . invalidRequestFormat( messageType: . GQL_CONNECTION_TERMINATE) )
@@ -116,6 +119,18 @@ public class Server<InitPayload: Equatable & Codable> {
116
119
self . onMessage = callback
117
120
}
118
121
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
+
119
134
private func onConnectionInit( _ connectionInitRequest: ConnectionInitRequest < InitPayload > , _ messenger: Messenger ) {
120
135
guard !initialized else {
121
136
self . error ( . tooManyInitializations( ) )
@@ -201,13 +216,6 @@ public class Server<InitPayload: Equatable & Codable> {
201
216
}
202
217
}
203
218
204
- private func onStop( _: StopRequest , _ messenger: Messenger ) {
205
- guard initialized else {
206
- self . error ( . notInitialized( ) )
207
- return
208
- }
209
- }
210
-
211
219
private func onConnectionTerminate( _: ConnectionTerminateRequest , _ messenger: Messenger ) {
212
220
onExit ( )
213
221
_ = messenger. close ( )
@@ -256,6 +264,7 @@ public class Server<InitPayload: Equatable & Codable> {
256
264
id: id
257
265
) . toJSON ( encoder)
258
266
)
267
+ onOperationComplete ( id)
259
268
}
260
269
261
270
/// Send an `error` response through the messenger
@@ -267,6 +276,7 @@ public class Server<InitPayload: Equatable & Codable> {
267
276
id: id
268
277
) . toJSON ( encoder)
269
278
)
279
+ onOperationError ( id)
270
280
}
271
281
272
282
/// Send an `error` response through the messenger
0 commit comments