@@ -13,8 +13,8 @@ public class Server<InitPayload: Equatable & Codable> {
1313
1414 let onExecute : ( GraphQLRequest ) -> EventLoopFuture < GraphQLResult >
1515 let onSubscribe : ( GraphQLRequest ) -> EventLoopFuture < SubscriptionResult >
16+ var auth : ( InitPayload ) throws -> EventLoopFuture < Void >
1617
17- var auth : ( InitPayload ) throws -> Void = { _ in }
1818 var onExit : ( ) -> Void = { }
1919 var onMessage : ( String ) -> Void = { _ in }
2020 var onOperationComplete : ( String ) -> Void = { _ in }
@@ -32,14 +32,17 @@ public class Server<InitPayload: Equatable & Codable> {
3232 /// - messenger: The messenger to bind the server to.
3333 /// - onExecute: Callback run during `start` resolution for non-streaming queries. Typically this is `API.execute`.
3434 /// - onSubscribe: Callback run during `start` resolution for streaming queries. Typically this is `API.subscribe`.
35+ /// - eventLoop: EventLoop on which to perform server operations.
3536 public init (
3637 messenger: Messenger ,
3738 onExecute: @escaping ( GraphQLRequest ) -> EventLoopFuture < GraphQLResult > ,
38- onSubscribe: @escaping ( GraphQLRequest ) -> EventLoopFuture < SubscriptionResult >
39+ onSubscribe: @escaping ( GraphQLRequest ) -> EventLoopFuture < SubscriptionResult > ,
40+ eventLoop: EventLoop
3941 ) {
4042 self . messenger = messenger
4143 self . onExecute = onExecute
4244 self . onSubscribe = onSubscribe
45+ self . auth = { _ in eventLoop. makeSucceededVoidFuture ( ) }
4346
4447 messenger. onReceive { message in
4548 guard let messenger = self . messenger else { return }
@@ -98,10 +101,10 @@ public class Server<InitPayload: Equatable & Codable> {
98101 }
99102 }
100103
101- /// Define the callback run during `connection_init` resolution that allows authorization using the `payload`.
102- /// Throw from this closure to indicate that authorization has failed.
104+ /// Define a custom callback run during `connection_init` resolution that allows authorization using the `payload`.
105+ /// Throw or fail the future from this closure to indicate that authorization has failed.
103106 /// - Parameter callback: The callback to assign
104- public func auth( _ callback: @escaping ( InitPayload ) throws -> Void ) {
107+ public func auth( _ callback: @escaping ( InitPayload ) throws -> EventLoopFuture < Void > ) {
105108 self . auth = callback
106109 }
107110
@@ -136,14 +139,20 @@ public class Server<InitPayload: Equatable & Codable> {
136139 }
137140
138141 do {
139- try self . auth ( connectionInitRequest. payload)
142+ let authResult = try self . auth ( connectionInitRequest. payload)
143+ authResult. whenSuccess {
144+ self . initialized = true
145+ self . sendConnectionAck ( )
146+ }
147+ authResult. whenFailure { error in
148+ self . error ( . unauthorized( ) )
149+ return
150+ }
140151 }
141152 catch {
142153 self . error ( . unauthorized( ) )
143154 return
144155 }
145- initialized = true
146- self . sendConnectionAck ( )
147156 // TODO: Should we send the `ka` message?
148157 }
149158
0 commit comments