@@ -15,8 +15,8 @@ public class Server<InitPayload: Equatable & Codable> {
1515
1616 let onExecute : ( GraphQLRequest ) -> EventLoopFuture < GraphQLResult >
1717 let onSubscribe : ( GraphQLRequest ) -> EventLoopFuture < SubscriptionResult >
18+ var auth : ( InitPayload ) throws -> EventLoopFuture < Void >
1819
19- var auth : ( InitPayload ) throws -> Void = { _ in }
2020 var onExit : ( ) -> Void = { }
2121 var onMessage : ( String ) -> Void = { _ in }
2222 var onOperationComplete : ( String ) -> Void = { _ in }
@@ -34,14 +34,17 @@ public class Server<InitPayload: Equatable & Codable> {
3434 /// - messenger: The messenger to bind the server to.
3535 /// - onExecute: Callback run during `start` resolution for non-streaming queries. Typically this is `API.execute`.
3636 /// - onSubscribe: Callback run during `start` resolution for streaming queries. Typically this is `API.subscribe`.
37+ /// - eventLoop: EventLoop on which to perform server operations.
3738 public init (
3839 messenger: Messenger ,
3940 onExecute: @escaping ( GraphQLRequest ) -> EventLoopFuture < GraphQLResult > ,
40- onSubscribe: @escaping ( GraphQLRequest ) -> EventLoopFuture < SubscriptionResult >
41+ onSubscribe: @escaping ( GraphQLRequest ) -> EventLoopFuture < SubscriptionResult > ,
42+ eventLoop: EventLoop
4143 ) {
4244 self . messenger = messenger
4345 self . onExecute = onExecute
4446 self . onSubscribe = onSubscribe
47+ self . auth = { _ in eventLoop. makeSucceededVoidFuture ( ) }
4548
4649 messenger. onReceive { message in
4750 guard let messenger = self . messenger else { return }
@@ -100,10 +103,10 @@ public class Server<InitPayload: Equatable & Codable> {
100103 }
101104 }
102105
103- /// Define the callback run during `connection_init` resolution that allows authorization using the `payload`.
104- /// Throw from this closure to indicate that authorization has failed.
106+ /// Define a custom callback run during `connection_init` resolution that allows authorization using the `payload`.
107+ /// Throw or fail the future from this closure to indicate that authorization has failed.
105108 /// - Parameter callback: The callback to assign
106- public func auth( _ callback: @escaping ( InitPayload ) throws -> Void ) {
109+ public func auth( _ callback: @escaping ( InitPayload ) throws -> EventLoopFuture < Void > ) {
107110 self . auth = callback
108111 }
109112
@@ -138,14 +141,20 @@ public class Server<InitPayload: Equatable & Codable> {
138141 }
139142
140143 do {
141- try self . auth ( connectionInitRequest. payload)
144+ let authResult = try self . auth ( connectionInitRequest. payload)
145+ authResult. whenSuccess {
146+ self . initialized = true
147+ self . sendConnectionAck ( )
148+ }
149+ authResult. whenFailure { error in
150+ self . error ( . unauthorized( ) )
151+ return
152+ }
142153 }
143154 catch {
144155 self . error ( . unauthorized( ) )
145156 return
146157 }
147- initialized = true
148- self . sendConnectionAck ( )
149158 // TODO: Should we send the `ka` message?
150159 }
151160
0 commit comments