-
Notifications
You must be signed in to change notification settings - Fork 6
feat: add onOperationComplete & onOperationError callbacks #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<InitPayload: Equatable & Codable> { | ||
|
@@ -18,6 +18,8 @@ public class Server<InitPayload: Equatable & Codable> { | |
|
||
var auth: (InitPayload) throws -> Void = { _ in } | ||
var onExit: () -> Void = { } | ||
var onOperationComplete: () -> Void = {} | ||
var onOperationError: () -> Void = {} | ||
|
||
var onMessage: (String) -> Void = { _ in } | ||
|
||
var initialized = false | ||
|
@@ -64,6 +66,7 @@ public class Server<InitPayload: Equatable & Codable> { | |
return | ||
} | ||
|
||
// handle incoing message | ||
|
||
switch request.type { | ||
case .connectionInit: | ||
guard let connectionInitRequest = try? self.decoder.decode(ConnectionInitRequest<InitPayload>.self, from: data) else { | ||
|
@@ -82,7 +85,7 @@ public class Server<InitPayload: Equatable & Codable> { | |
self.error(.invalidRequestFormat(messageType: .complete)) | ||
return | ||
} | ||
self.onComplete(completeRequest) | ||
self.onSubscribeComplete(completeRequest) | ||
case .unknown: | ||
self.error(.invalidType()) | ||
} | ||
|
@@ -107,6 +110,18 @@ public class Server<InitPayload: Equatable & Codable> { | |
self.onMessage = callback | ||
} | ||
|
||
/// 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) { | ||
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<InitPayload>) { | ||
guard !initialized else { | ||
self.error(.tooManyInitializations()) | ||
|
@@ -193,11 +208,12 @@ public class Server<InitPayload: Equatable & Codable> { | |
} | ||
} | ||
|
||
private func onComplete(_: CompleteRequest) { | ||
private func onSubscribeComplete(_: CompleteRequest) { | ||
guard initialized else { | ||
self.error(.notInitialized()) | ||
return | ||
} | ||
onOperationComplete() | ||
} | ||
|
||
/// Send a `connection_ack` response through the messenger | ||
|
@@ -227,6 +243,7 @@ public class Server<InitPayload: Equatable & Codable> { | |
id: id | ||
).toJSON(encoder) | ||
) | ||
self.onOperationComplete() | ||
} | ||
|
||
/// Send an `error` response through the messenger | ||
|
@@ -238,6 +255,7 @@ public class Server<InitPayload: Equatable & Codable> { | |
id: id | ||
).toJSON(encoder) | ||
) | ||
self.onOperationError() | ||
} | ||
|
||
/// Send an `error` response through the messenger | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we specify 0 or 1 subscription? I believe it can support any number of subscriptions, as long as they have different IDs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was just an incorrect assumption by me that I forgot to remove. Removed now.