From bf739f548903d181e2e6dd50433147b9cd3cedab Mon Sep 17 00:00:00 2001 From: Garrett Moseke Date: Thu, 21 Jul 2022 14:15:01 -0600 Subject: [PATCH 1/3] fix: expose onTop callback handler --- Sources/GraphQLWS/Server.swift | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Sources/GraphQLWS/Server.swift b/Sources/GraphQLWS/Server.swift index b043ed3..6fed66e 100644 --- a/Sources/GraphQLWS/Server.swift +++ b/Sources/GraphQLWS/Server.swift @@ -19,6 +19,7 @@ public class Server { var auth: (InitPayload) throws -> Void = { _ in } var onExit: () -> Void = { } var onMessage: (String) -> Void = { _ in } + var onStop: () -> Void = { } var initialized = false @@ -66,6 +67,7 @@ public class Server { return } + // handle incoming message switch request.type { case .GQL_CONNECTION_INIT: guard let connectionInitRequest = try? self.decoder.decode(ConnectionInitRequest.self, from: json) else { @@ -116,6 +118,12 @@ public class Server { self.onMessage = callback } + /// Define the callback run on receipt of a`GQL_STOP` message + /// - Parameter callback: The callback to assign + public func onStop(_ callback: @escaping () -> Void) { + self.onStop = callback + } + private func onConnectionInit(_ connectionInitRequest: ConnectionInitRequest, _ messenger: Messenger) { guard !initialized else { self.error(.tooManyInitializations()) @@ -206,6 +214,7 @@ public class Server { self.error(.notInitialized()) return } + onStop() } private func onConnectionTerminate(_: ConnectionTerminateRequest, _ messenger: Messenger) { From 2019bc376977994b6ce825ffec760b2d3074588a Mon Sep 17 00:00:00 2001 From: Garrett Moseke Date: Fri, 22 Jul 2022 10:46:24 -0600 Subject: [PATCH 2/3] feat: add onOperationComplete & onOperationError callbacks --- Sources/GraphQLWS/Server.swift | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/Sources/GraphQLWS/Server.swift b/Sources/GraphQLWS/Server.swift index 6fed66e..a3d41e7 100644 --- a/Sources/GraphQLWS/Server.swift +++ b/Sources/GraphQLWS/Server.swift @@ -19,7 +19,8 @@ public class Server { var auth: (InitPayload) throws -> Void = { _ in } var onExit: () -> Void = { } var onMessage: (String) -> Void = { _ in } - var onStop: () -> Void = { } + var onOperationComplete: () -> Void = {} + var onOperationError: () -> Void = {} var initialized = false @@ -118,10 +119,16 @@ public class Server { self.onMessage = callback } - /// Define the callback run on receipt of a`GQL_STOP` message + /// Define the callback run on the completion a full operation (query/mutation, end of subscription) /// - Parameter callback: The callback to assign - public func onStop(_ callback: @escaping () -> Void) { - self.onStop = callback + public func onOperationComplete(_ callback: @escaping () -> Void) { + self.onOperationComplete = callback + } + + /// Define the callback to run on error of any full operation (failed query, interrupted subscription) + /// - Parameter callback: The callback to assign + public func onOperationError(_ callback: @escaping () -> Void) { + self.onOperationError = callback } private func onConnectionInit(_ connectionInitRequest: ConnectionInitRequest, _ messenger: Messenger) { @@ -214,7 +221,7 @@ public class Server { self.error(.notInitialized()) return } - onStop() + onOperationComplete() } private func onConnectionTerminate(_: ConnectionTerminateRequest, _ messenger: Messenger) { @@ -265,6 +272,7 @@ public class Server { id: id ).toJSON(encoder) ) + onOperationComplete() } /// Send an `error` response through the messenger @@ -276,6 +284,7 @@ public class Server { id: id ).toJSON(encoder) ) + onOperationError() } /// Send an `error` response through the messenger From 6462f866c194f6a810a9b9b406f7f8e638471daa Mon Sep 17 00:00:00 2001 From: Garrett Moseke Date: Fri, 22 Jul 2022 11:30:47 -0600 Subject: [PATCH 3/3] fix: add id parameter for onComplete & onError callbacks --- Sources/GraphQLWS/Server.swift | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/Sources/GraphQLWS/Server.swift b/Sources/GraphQLWS/Server.swift index a3d41e7..fb9ea17 100644 --- a/Sources/GraphQLWS/Server.swift +++ b/Sources/GraphQLWS/Server.swift @@ -19,8 +19,8 @@ public class Server { var auth: (InitPayload) throws -> Void = { _ in } var onExit: () -> Void = { } var onMessage: (String) -> Void = { _ in } - var onOperationComplete: () -> Void = {} - var onOperationError: () -> Void = {} + var onOperationComplete: (String) -> Void = { _ in } + var onOperationError: (String) -> Void = { _ in } var initialized = false @@ -87,7 +87,7 @@ public class Server { self.error(.invalidRequestFormat(messageType: .GQL_STOP)) return } - self.onStop(stopRequest, messenger) + self.onOperationComplete(stopRequest.id) case .GQL_CONNECTION_TERMINATE: guard let connectionTerminateRequest = try? self.decoder.decode(ConnectionTerminateRequest.self, from: json) else { self.error(.invalidRequestFormat(messageType: .GQL_CONNECTION_TERMINATE)) @@ -121,13 +121,13 @@ public class Server { /// Define the callback run on the completion a full operation (query/mutation, end of subscription) /// - Parameter callback: The callback to assign - public func onOperationComplete(_ callback: @escaping () -> Void) { + public func onOperationComplete(_ callback: @escaping (String) -> Void) { self.onOperationComplete = callback } /// Define the callback to run on error of any full operation (failed query, interrupted subscription) /// - Parameter callback: The callback to assign - public func onOperationError(_ callback: @escaping () -> Void) { + public func onOperationError(_ callback: @escaping (String) -> Void) { self.onOperationError = callback } @@ -216,14 +216,6 @@ public class Server { } } - private func onStop(_: StopRequest, _ messenger: Messenger) { - guard initialized else { - self.error(.notInitialized()) - return - } - onOperationComplete() - } - private func onConnectionTerminate(_: ConnectionTerminateRequest, _ messenger: Messenger) { onExit() _ = messenger.close() @@ -272,7 +264,7 @@ public class Server { id: id ).toJSON(encoder) ) - onOperationComplete() + onOperationComplete(id) } /// Send an `error` response through the messenger @@ -284,7 +276,7 @@ public class Server { id: id ).toJSON(encoder) ) - onOperationError() + onOperationError(id) } /// Send an `error` response through the messenger