@@ -13,8 +13,8 @@ public class Server<InitPayload: Equatable & Codable> {
13
13
14
14
let onExecute : ( GraphQLRequest ) -> EventLoopFuture < GraphQLResult >
15
15
let onSubscribe : ( GraphQLRequest ) -> EventLoopFuture < SubscriptionResult >
16
+ var auth : ( InitPayload ) throws -> EventLoopFuture < Void >
16
17
17
- var auth : ( InitPayload ) throws -> Void = { _ in }
18
18
var onExit : ( ) -> Void = { }
19
19
var onMessage : ( String ) -> Void = { _ in }
20
20
var onOperationComplete : ( String ) -> Void = { _ in }
@@ -32,14 +32,17 @@ public class Server<InitPayload: Equatable & Codable> {
32
32
/// - messenger: The messenger to bind the server to.
33
33
/// - onExecute: Callback run during `start` resolution for non-streaming queries. Typically this is `API.execute`.
34
34
/// - onSubscribe: Callback run during `start` resolution for streaming queries. Typically this is `API.subscribe`.
35
+ /// - eventLoop: EventLoop on which to perform server operations.
35
36
public init (
36
37
messenger: Messenger ,
37
38
onExecute: @escaping ( GraphQLRequest ) -> EventLoopFuture < GraphQLResult > ,
38
- onSubscribe: @escaping ( GraphQLRequest ) -> EventLoopFuture < SubscriptionResult >
39
+ onSubscribe: @escaping ( GraphQLRequest ) -> EventLoopFuture < SubscriptionResult > ,
40
+ eventLoop: EventLoop
39
41
) {
40
42
self . messenger = messenger
41
43
self . onExecute = onExecute
42
44
self . onSubscribe = onSubscribe
45
+ self . auth = { _ in eventLoop. makeSucceededVoidFuture ( ) }
43
46
44
47
messenger. onReceive { message in
45
48
guard let messenger = self . messenger else { return }
@@ -98,10 +101,10 @@ public class Server<InitPayload: Equatable & Codable> {
98
101
}
99
102
}
100
103
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.
103
106
/// - 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 > ) {
105
108
self . auth = callback
106
109
}
107
110
@@ -136,14 +139,20 @@ public class Server<InitPayload: Equatable & Codable> {
136
139
}
137
140
138
141
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
+ }
140
151
}
141
152
catch {
142
153
self . error ( . unauthorized( ) )
143
154
return
144
155
}
145
- initialized = true
146
- self . sendConnectionAck ( )
147
156
// TODO: Should we send the `ka` message?
148
157
}
149
158
0 commit comments