Skip to content

Commit

Permalink
Merge pull request #2 from GNMoseke/fix/expose-on-complete
Browse files Browse the repository at this point in the history
feat: add onOperationComplete & onOperationError callbacks
  • Loading branch information
NeedleInAJayStack authored Jul 23, 2022
2 parents 5050078 + 4033b03 commit 37de2c4
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions Sources/GraphQLTransportWS/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class Server<InitPayload: Equatable & Codable> {

var auth: (InitPayload) throws -> Void = { _ in }
var onExit: () -> Void = { }
var onOperationComplete: (String) -> Void = { _ in }
var onOperationError: (String) -> Void = { _ in }
var onMessage: (String) -> Void = { _ in }

var initialized = false
Expand Down Expand Up @@ -64,6 +66,7 @@ public class Server<InitPayload: Equatable & Codable> {
return
}

// handle incoming message
switch request.type {
case .connectionInit:
guard let connectionInitRequest = try? self.decoder.decode(ConnectionInitRequest<InitPayload>.self, from: data) else {
Expand All @@ -82,15 +85,16 @@ public class Server<InitPayload: Equatable & Codable> {
self.error(.invalidRequestFormat(messageType: .complete))
return
}
self.onComplete(completeRequest)
self.onOperationComplete(completeRequest.id)
case .unknown:
self.error(.invalidType())
}
}
}

/// 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
}
Expand All @@ -107,6 +111,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, 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, taking a string parameter for the ID of the operation
public func onOperationError(_ callback: @escaping (String) -> Void) {
self.onOperationError = callback
}

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

private func onComplete(_: CompleteRequest) {
guard initialized else {
self.error(.notInitialized())
return
}
}

/// Send a `connection_ack` response through the messenger
private func sendConnectionAck(_ payload: [String: Map]? = nil) {
guard let messenger = messenger else { return }
Expand Down Expand Up @@ -227,6 +236,7 @@ public class Server<InitPayload: Equatable & Codable> {
id: id
).toJSON(encoder)
)
self.onOperationComplete(id)
}

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

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

0 comments on commit 37de2c4

Please sign in to comment.