@@ -36,8 +36,8 @@ func subscribe(
3636 eventLoopGroup: EventLoopGroup ,
3737 variableValues: [ String : Map ] = [ : ] ,
3838 operationName: String ? = nil ,
39- fieldResolver: GraphQLFieldResolve ,
40- subscribeFieldResolver: GraphQLFieldResolve
39+ fieldResolver: GraphQLFieldResolve ? = nil ,
40+ subscribeFieldResolver: GraphQLFieldResolve ? = nil
4141) -> EventLoopFuture < SubscriptionResult > {
4242
4343
@@ -62,7 +62,7 @@ func subscribe(
6262 // the GraphQL specification. The `execute` function provides the
6363 // "ExecuteSubscriptionEvent" algorithm, as it is nearly identical to the
6464 // "ExecuteQuery" algorithm, for which `execute` is also used.
65- func mapSourceToResponse( payload: GraphQLResult ) -> EventLoopFuture < GraphQLResult > {
65+ func mapSourceToResponse( payload: Any ) -> EventLoopFuture < GraphQLResult > {
6666 return execute (
6767 queryStrategy: queryStrategy,
6868 mutationStrategy: mutationStrategy,
@@ -131,8 +131,8 @@ func createSourceEventStream(
131131 eventLoopGroup: EventLoopGroup ,
132132 variableValues: [ String : Map ] = [ : ] ,
133133 operationName: String ? = nil ,
134- subscribeFieldResolver: GraphQLFieldResolve
135- ) -> EventLoopFuture < SubscriptionResult > {
134+ subscribeFieldResolver: GraphQLFieldResolve ? = nil
135+ ) -> EventLoopFuture < SourceEventStreamResult > {
136136
137137 let executeStarted = instrumentation. now
138138 let exeContext : ExecutionContext
@@ -170,9 +170,9 @@ func createSourceEventStream(
170170 result: nil
171171 )
172172
173- return eventLoopGroup. next ( ) . makeSucceededFuture ( SubscriptionResult . failure ( error) )
173+ return eventLoopGroup. next ( ) . makeSucceededFuture ( SourceEventStreamResult . failure ( error) )
174174 } catch {
175- return eventLoopGroup. next ( ) . makeSucceededFuture ( SubscriptionResult . failure ( GraphQLError ( error) ) )
175+ return eventLoopGroup. next ( ) . makeSucceededFuture ( SourceEventStreamResult . failure ( GraphQLError ( error) ) )
176176 }
177177
178178 return try ! executeSubscription ( context: exeContext, eventLoopGroup: eventLoopGroup)
@@ -181,7 +181,7 @@ func createSourceEventStream(
181181func executeSubscription(
182182 context: ExecutionContext ,
183183 eventLoopGroup: EventLoopGroup
184- ) throws -> EventLoopFuture < SubscriptionResult > {
184+ ) throws -> EventLoopFuture < SourceEventStreamResult > {
185185
186186 // Get the first node
187187 let type = try getOperationRootType ( schema: context. schema, operation: context. operation)
@@ -241,22 +241,40 @@ func executeSubscription(
241241 eventLoopGroup: eventLoopGroup,
242242 info: info
243243 )
244-
245244 return try completeValueCatchingError (
246245 exeContext: context,
247246 returnType: fieldDef. type,
248247 fieldASTs: fieldNodes,
249248 info: info,
250249 path: path,
251250 result: result
252- ) . map { value -> SubscriptionResult in
253- if let observable = value as? Observable < GraphQLResult > {
254- return SubscriptionResult . success ( observable)
251+ )
252+ // TODO do we need to create this data map?
253+ // .flatMapThrowing { data -> Any in
254+ // // Translate from raw value completion map into a GraphQLResult
255+ // var dataMap: Map = [:]
256+ // dataMap[fieldDef.name] = try map(from: data)
257+ // var result: GraphQLResult = GraphQLResult(data: dataMap)
258+ //
259+ // if !context.errors.isEmpty {
260+ // result.errors = context.errors
261+ // }
262+ // return result
263+ // }
264+ . map { value -> SourceEventStreamResult in
265+ if !context. errors. isEmpty {
266+ // TODO improve this to return multiple errors if we have them.
267+ return SourceEventStreamResult . failure ( context. errors. first!)
268+ } else if value is Observable < Any ? > {
269+ let observable = value as! Observable < Any >
270+ return SourceEventStreamResult . success ( observable)
271+ } else if let error = value as? GraphQLError {
272+ return SourceEventStreamResult . failure ( error)
255273 } else {
256- context. append ( error: GraphQLError ( message: " Subscription field resolver must return Observable of GraphQLResults. " ) )
257- return SubscriptionResult . failure ( GraphQLError ( message: " Subscription field resolver must return Observable of GraphQLResults. " ) )
274+ return SourceEventStreamResult . failure ( GraphQLError ( message: " Subscription field resolver must return Observable of GraphQLResults. " ) )
258275 }
259276 }
260277}
261278
262279typealias SubscriptionResult = Result < Observable < GraphQLResult > , GraphQLError >
280+ typealias SourceEventStreamResult = Result < Observable < Any > , GraphQLError >
0 commit comments