From 15a5084b4df346ed965c71f06df26605196b81e3 Mon Sep 17 00:00:00 2001 From: Garrett Moseke Date: Thu, 21 Jul 2022 13:23:22 -0600 Subject: [PATCH 1/4] fix: add onComplete callback --- Sources/GraphQLTransportWS/Server.swift | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Sources/GraphQLTransportWS/Server.swift b/Sources/GraphQLTransportWS/Server.swift index a3b2d14..5ee0b2b 100644 --- a/Sources/GraphQLTransportWS/Server.swift +++ b/Sources/GraphQLTransportWS/Server.swift @@ -6,7 +6,7 @@ import GraphQLRxSwift import NIO import RxSwift -/// Server implements the server-side portion of the protocol, allowing a few callbacks for customization. +/// Server implements the server-side portion of the protocol, allowing a few callbacks for customization. 0 or 1 subscriptions per connection and no more. /// /// By default, there are no authorization checks public class Server { @@ -18,6 +18,7 @@ public class Server { var auth: (InitPayload) throws -> Void = { _ in } var onExit: () -> Void = { } + var onComplete: () -> Void = {} var onMessage: (String) -> Void = { _ in } var initialized = false @@ -64,6 +65,7 @@ public class Server { return } + // handle incoing message switch request.type { case .connectionInit: guard let connectionInitRequest = try? self.decoder.decode(ConnectionInitRequest.self, from: data) else { @@ -107,6 +109,12 @@ public class Server { self.onMessage = callback } + /// Define the callback run on receipt of a `complete` message + /// - Parameter callback: The callback to assign + public func onComplete(_ callback: @escaping () -> Void) { + self.onComplete = callback + } + private func onConnectionInit(_ connectionInitRequest: ConnectionInitRequest) { guard !initialized else { self.error(.tooManyInitializations()) @@ -198,6 +206,7 @@ public class Server { self.error(.notInitialized()) return } + onComplete() } /// Send a `connection_ack` response through the messenger From f7fca67e048219b3583380d64edc04cfa318d622 Mon Sep 17 00:00:00 2001 From: Garrett Moseke Date: Thu, 21 Jul 2022 14:10:18 -0600 Subject: [PATCH 2/4] chore: typos --- Sources/GraphQLTransportWS/Server.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/GraphQLTransportWS/Server.swift b/Sources/GraphQLTransportWS/Server.swift index 5ee0b2b..d1c420e 100644 --- a/Sources/GraphQLTransportWS/Server.swift +++ b/Sources/GraphQLTransportWS/Server.swift @@ -6,7 +6,7 @@ import GraphQLRxSwift import NIO import RxSwift -/// Server implements the server-side portion of the protocol, allowing a few callbacks for customization. 0 or 1 subscriptions per connection and no more. +/// 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. /// /// By default, there are no authorization checks public class Server { @@ -65,7 +65,7 @@ public class Server { return } - // handle incoing message + // handle incoming message switch request.type { case .connectionInit: guard let connectionInitRequest = try? self.decoder.decode(ConnectionInitRequest.self, from: data) else { From bd21d75d7dd06ff45150f241971d6b5d88f0b08b Mon Sep 17 00:00:00 2001 From: Garrett Moseke Date: Fri, 22 Jul 2022 10:38:14 -0600 Subject: [PATCH 3/4] feat: add onOperationComplete, onOperationError callbacks --- Sources/GraphQLTransportWS/Server.swift | 27 ++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/Sources/GraphQLTransportWS/Server.swift b/Sources/GraphQLTransportWS/Server.swift index d1c420e..aa9c6c0 100644 --- a/Sources/GraphQLTransportWS/Server.swift +++ b/Sources/GraphQLTransportWS/Server.swift @@ -6,7 +6,7 @@ import GraphQLRxSwift import NIO import RxSwift -/// 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. +/// Server implements the server-side portion of the protocol, allowing a few callbacks for customization. 0 or 1 subscriptions per connection and no more. /// /// By default, there are no authorization checks public class Server { @@ -18,7 +18,8 @@ public class Server { var auth: (InitPayload) throws -> Void = { _ in } var onExit: () -> Void = { } - var onComplete: () -> Void = {} + var onOperationComplete: () -> Void = {} + var onOperationError: () -> Void = {} var onMessage: (String) -> Void = { _ in } var initialized = false @@ -65,7 +66,7 @@ public class Server { return } - // handle incoming message + // handle incoing message switch request.type { case .connectionInit: guard let connectionInitRequest = try? self.decoder.decode(ConnectionInitRequest.self, from: data) else { @@ -84,7 +85,7 @@ public class Server { self.error(.invalidRequestFormat(messageType: .complete)) return } - self.onComplete(completeRequest) + self.onSubscribeComplete(completeRequest) case .unknown: self.error(.invalidType()) } @@ -109,10 +110,16 @@ public class Server { self.onMessage = callback } - /// Define the callback run on receipt of a `complete` message + /// Define the callback run on the completion a full operation (query/mutation, end of subscription) /// - Parameter callback: The callback to assign - public func onComplete(_ callback: @escaping () -> Void) { - self.onComplete = 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) { @@ -201,12 +208,12 @@ public class Server { } } - private func onComplete(_: CompleteRequest) { + private func onSubscribeComplete(_: CompleteRequest) { guard initialized else { self.error(.notInitialized()) return } - onComplete() + onOperationComplete() } /// Send a `connection_ack` response through the messenger @@ -236,6 +243,7 @@ public class Server { id: id ).toJSON(encoder) ) + self.onOperationComplete() } /// Send an `error` response through the messenger @@ -247,6 +255,7 @@ public class Server { id: id ).toJSON(encoder) ) + self.onOperationError() } /// Send an `error` response through the messenger From 4033b03652a8b253edf6afe800875232cc02311f Mon Sep 17 00:00:00 2001 From: Garrett Moseke Date: Fri, 22 Jul 2022 11:21:54 -0600 Subject: [PATCH 4/4] fix: add ID parameter for completion/error callbacks --- Sources/GraphQLTransportWS/Server.swift | 33 ++++++++++--------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/Sources/GraphQLTransportWS/Server.swift b/Sources/GraphQLTransportWS/Server.swift index aa9c6c0..5baee2e 100644 --- a/Sources/GraphQLTransportWS/Server.swift +++ b/Sources/GraphQLTransportWS/Server.swift @@ -6,7 +6,7 @@ import GraphQLRxSwift import NIO import RxSwift -/// Server implements the server-side portion of the protocol, allowing a few callbacks for customization. 0 or 1 subscriptions per connection and no more. +/// Server implements the server-side portion of the protocol, allowing a few callbacks for customization. /// /// By default, there are no authorization checks public class Server { @@ -18,8 +18,8 @@ public class Server { var auth: (InitPayload) throws -> Void = { _ in } var onExit: () -> Void = { } - var onOperationComplete: () -> Void = {} - var onOperationError: () -> Void = {} + var onOperationComplete: (String) -> Void = { _ in } + var onOperationError: (String) -> Void = { _ in } var onMessage: (String) -> Void = { _ in } var initialized = false @@ -66,7 +66,7 @@ public class Server { return } - // handle incoing message + // handle incoming message switch request.type { case .connectionInit: guard let connectionInitRequest = try? self.decoder.decode(ConnectionInitRequest.self, from: data) else { @@ -85,7 +85,7 @@ public class Server { self.error(.invalidRequestFormat(messageType: .complete)) return } - self.onSubscribeComplete(completeRequest) + self.onOperationComplete(completeRequest.id) case .unknown: self.error(.invalidType()) } @@ -93,7 +93,8 @@ public class Server { } /// Define the callback run during `connection_init` resolution that allows authorization using the `payload`. - /// Throw to indicate that authorization has failed. /// - Parameter callback: The callback to assign + /// Throw to indicate that authorization has failed. + /// - Parameter callback: The callback to assign public func auth(_ callback: @escaping (InitPayload) throws -> Void) { self.auth = callback } @@ -111,14 +112,14 @@ 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) { + /// - Parameter callback: The callback to assign, taking a string parameter for the ID of the operation + 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) { + /// - Parameter callback: The callback to assign, taking a string parameter for the ID of the operation + public func onOperationError(_ callback: @escaping (String) -> Void) { self.onOperationError = callback } @@ -208,14 +209,6 @@ public class Server { } } - private func onSubscribeComplete(_: CompleteRequest) { - guard initialized else { - self.error(.notInitialized()) - return - } - onOperationComplete() - } - /// Send a `connection_ack` response through the messenger private func sendConnectionAck(_ payload: [String: Map]? = nil) { guard let messenger = messenger else { return } @@ -243,7 +236,7 @@ public class Server { id: id ).toJSON(encoder) ) - self.onOperationComplete() + self.onOperationComplete(id) } /// Send an `error` response through the messenger @@ -255,7 +248,7 @@ public class Server { id: id ).toJSON(encoder) ) - self.onOperationError() + self.onOperationError(id) } /// Send an `error` response through the messenger