Skip to content
Merged
Changes from 3 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
24 changes: 21 additions & 3 deletions Sources/GraphQLTransportWS/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Member

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.

Copy link
Contributor Author

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.

///
/// By default, there are no authorization checks
public class Server<InitPayload: Equatable & Codable> {
Expand All @@ -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 = {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be convenient for both of these to provide the ID of the operation that has completed or errored.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, added.

var onMessage: (String) -> Void = { _ in }

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

// handle incoing message
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix incoing to incoming

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops fixed.

switch request.type {
case .connectionInit:
guard let connectionInitRequest = try? self.decoder.decode(ConnectionInitRequest<InitPayload>.self, from: data) else {
Expand All @@ -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())
}
Expand All @@ -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())
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -227,6 +243,7 @@ public class Server<InitPayload: Equatable & Codable> {
id: id
).toJSON(encoder)
)
self.onOperationComplete()
}

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

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