Skip to content

Commit bd21d75

Browse files
author
Garrett Moseke
authored
feat: add onOperationComplete, onOperationError callbacks
1 parent f7fca67 commit bd21d75

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

Sources/GraphQLTransportWS/Server.swift

Lines changed: 18 additions & 9 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. Handles 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. 0 or 1 subscriptions per connection and no more.
1010
///
1111
/// By default, there are no authorization checks
1212
public class Server<InitPayload: Equatable & Codable> {
@@ -18,7 +18,8 @@ public class Server<InitPayload: Equatable & Codable> {
1818

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

2425
var initialized = false
@@ -65,7 +66,7 @@ public class Server<InitPayload: Equatable & Codable> {
6566
return
6667
}
6768

68-
// handle incoming message
69+
// handle incoing message
6970
switch request.type {
7071
case .connectionInit:
7172
guard let connectionInitRequest = try? self.decoder.decode(ConnectionInitRequest<InitPayload>.self, from: data) else {
@@ -84,7 +85,7 @@ public class Server<InitPayload: Equatable & Codable> {
8485
self.error(.invalidRequestFormat(messageType: .complete))
8586
return
8687
}
87-
self.onComplete(completeRequest)
88+
self.onSubscribeComplete(completeRequest)
8889
case .unknown:
8990
self.error(.invalidType())
9091
}
@@ -109,10 +110,16 @@ public class Server<InitPayload: Equatable & Codable> {
109110
self.onMessage = callback
110111
}
111112

112-
/// Define the callback run on receipt of a `complete` message
113+
/// Define the callback run on the completion a full operation (query/mutation, end of subscription)
113114
/// - Parameter callback: The callback to assign
114-
public func onComplete(_ callback: @escaping () -> Void) {
115-
self.onComplete = callback
115+
public func onOperationComplete(_ callback: @escaping () -> Void) {
116+
self.onOperationComplete = callback
117+
}
118+
119+
/// 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) {
122+
self.onOperationError = callback
116123
}
117124

118125
private func onConnectionInit(_ connectionInitRequest: ConnectionInitRequest<InitPayload>) {
@@ -201,12 +208,12 @@ public class Server<InitPayload: Equatable & Codable> {
201208
}
202209
}
203210

204-
private func onComplete(_: CompleteRequest) {
211+
private func onSubscribeComplete(_: CompleteRequest) {
205212
guard initialized else {
206213
self.error(.notInitialized())
207214
return
208215
}
209-
onComplete()
216+
onOperationComplete()
210217
}
211218

212219
/// Send a `connection_ack` response through the messenger
@@ -236,6 +243,7 @@ public class Server<InitPayload: Equatable & Codable> {
236243
id: id
237244
).toJSON(encoder)
238245
)
246+
self.onOperationComplete()
239247
}
240248

241249
/// Send an `error` response through the messenger
@@ -247,6 +255,7 @@ public class Server<InitPayload: Equatable & Codable> {
247255
id: id
248256
).toJSON(encoder)
249257
)
258+
self.onOperationError()
250259
}
251260

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

0 commit comments

Comments
 (0)