@@ -13,9 +13,9 @@ public class Server<InitPayload: Equatable & Codable> {
13
13
14
14
let onExecute : ( GraphQLRequest ) -> EventLoopFuture < GraphQLResult >
15
15
let onSubscribe : ( GraphQLRequest ) -> EventLoopFuture < SubscriptionResult >
16
-
17
- var auth : ( InitPayload ) throws -> Void = { _ in }
18
- var onExit : ( ) -> Void = { }
16
+ var auth : ( InitPayload ) throws -> EventLoopFuture < Void >
17
+
18
+ var onExit : ( ) -> Void = { }
19
19
var onOperationComplete : ( String ) -> Void = { _ in }
20
20
var onOperationError : ( String ) -> Void = { _ in }
21
21
var onMessage : ( String ) -> Void = { _ in }
@@ -33,14 +33,17 @@ public class Server<InitPayload: Equatable & Codable> {
33
33
/// - messenger: The messenger to bind the server to.
34
34
/// - onExecute: Callback run during `subscribe` resolution for non-streaming queries. Typically this is `API.execute`.
35
35
/// - onSubscribe: Callback run during `subscribe` resolution for streaming queries. Typically this is `API.subscribe`.
36
+ /// - eventLoop: EventLoop on which to perform server operations.
36
37
public init (
37
38
messenger: Messenger ,
38
39
onExecute: @escaping ( GraphQLRequest ) -> EventLoopFuture < GraphQLResult > ,
39
- onSubscribe: @escaping ( GraphQLRequest ) -> EventLoopFuture < SubscriptionResult >
40
+ onSubscribe: @escaping ( GraphQLRequest ) -> EventLoopFuture < SubscriptionResult > ,
41
+ eventLoop: EventLoop
40
42
) {
41
43
self . messenger = messenger
42
44
self . onExecute = onExecute
43
45
self . onSubscribe = onSubscribe
46
+ self . auth = { _ in eventLoop. makeSucceededVoidFuture ( ) }
44
47
45
48
messenger. onReceive { message in
46
49
self . onMessage ( message)
@@ -103,9 +106,9 @@ public class Server<InitPayload: Equatable & Codable> {
103
106
}
104
107
105
108
/// Define the callback run during `connection_init` resolution that allows authorization using the `payload`.
106
- /// Throw to indicate that authorization has failed.
109
+ /// Throw or fail the future to indicate that authorization has failed.
107
110
/// - Parameter callback: The callback to assign
108
- public func auth( _ callback: @escaping ( InitPayload ) throws -> Void ) {
111
+ public func auth( _ callback: @escaping ( InitPayload ) throws -> EventLoopFuture < Void > ) {
109
112
self . auth = callback
110
113
}
111
114
@@ -150,14 +153,20 @@ public class Server<InitPayload: Equatable & Codable> {
150
153
}
151
154
152
155
do {
153
- try self . auth ( connectionInitRequest. payload)
156
+ let authResult = try self . auth ( connectionInitRequest. payload)
157
+ authResult. whenSuccess {
158
+ self . initialized = true
159
+ self . sendConnectionAck ( )
160
+ }
161
+ authResult. whenFailure { error in
162
+ self . error ( . unauthorized( ) )
163
+ return
164
+ }
154
165
}
155
166
catch {
156
167
self . error ( . unauthorized( ) )
157
168
return
158
169
}
159
- initialized = true
160
- self . sendConnectionAck ( )
161
170
}
162
171
163
172
private func onSubscribe( _ subscribeRequest: SubscribeRequest ) {
0 commit comments