Skip to content

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

Merged
merged 3 commits into from
Jul 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions Sources/GraphQLWS/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class Server<InitPayload: Equatable & Codable> {
var auth: (InitPayload) throws -> Void = { _ in }
var onExit: () -> Void = { }
var onMessage: (String) -> Void = { _ in }
var onOperationComplete: (String) -> Void = { _ in }
var onOperationError: (String) -> Void = { _ in }

var initialized = false

Expand Down Expand Up @@ -66,6 +68,7 @@ public class Server<InitPayload: Equatable & Codable> {
return
}

// handle incoming message
switch request.type {
case .GQL_CONNECTION_INIT:
guard let connectionInitRequest = try? self.decoder.decode(ConnectionInitRequest<InitPayload>.self, from: json) else {
Expand All @@ -84,7 +87,7 @@ public class Server<InitPayload: Equatable & Codable> {
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))
Expand Down Expand Up @@ -116,6 +119,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 (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 (String) -> Void) {
self.onOperationError = callback
}

private func onConnectionInit(_ connectionInitRequest: ConnectionInitRequest<InitPayload>, _ messenger: Messenger) {
guard !initialized else {
self.error(.tooManyInitializations())
Expand Down Expand Up @@ -201,13 +216,6 @@ public class Server<InitPayload: Equatable & Codable> {
}
}

private func onStop(_: StopRequest, _ messenger: Messenger) {
guard initialized else {
self.error(.notInitialized())
return
}
}

private func onConnectionTerminate(_: ConnectionTerminateRequest, _ messenger: Messenger) {
onExit()
_ = messenger.close()
Expand Down Expand Up @@ -256,6 +264,7 @@ public class Server<InitPayload: Equatable & Codable> {
id: id
).toJSON(encoder)
)
onOperationComplete(id)
}

/// Send an `error` response through the messenger
Expand All @@ -267,6 +276,7 @@ public class Server<InitPayload: Equatable & Codable> {
id: id
).toJSON(encoder)
)
onOperationError(id)
}

/// Send an `error` response through the messenger
Expand Down