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