Skip to content

Commit 220713b

Browse files
author
Jay Herron
committed
Implements some testing... getting closer
1 parent 681e213 commit 220713b

3 files changed

Lines changed: 275 additions & 68 deletions

File tree

Sources/GraphQL/Subscription/Subscribe.swift

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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(
181181
func 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

262279
typealias SubscriptionResult = Result<Observable<GraphQLResult>, GraphQLError>
280+
typealias SourceEventStreamResult = Result<Observable<Any>, GraphQLError>

Sources/GraphQL/Type/Definition.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,22 @@ public struct GraphQLField {
548548
self.subscribe = nil
549549
}
550550

551+
public init(
552+
type: GraphQLOutputType,
553+
description: String? = nil,
554+
deprecationReason: String? = nil,
555+
args: GraphQLArgumentConfigMap = [:],
556+
resolve: GraphQLFieldResolve?,
557+
subscribe: GraphQLFieldResolve?
558+
) {
559+
self.type = type
560+
self.args = args
561+
self.deprecationReason = deprecationReason
562+
self.description = description
563+
self.resolve = resolve
564+
self.subscribe = subscribe
565+
}
566+
551567
public init(
552568
type: GraphQLOutputType,
553569
description: String? = nil,
@@ -581,6 +597,25 @@ public struct GraphQLField {
581597
}
582598
self.subscribe = nil
583599
}
600+
601+
// public init(
602+
// type: GraphQLOutputType,
603+
// description: String? = nil,
604+
// deprecationReason: String? = nil,
605+
// args: GraphQLArgumentConfigMap = [:],
606+
// subscribe: GraphQLFieldResolveInput
607+
// ) {
608+
// self.type = type
609+
// self.args = args
610+
// self.deprecationReason = deprecationReason
611+
// self.description = description
612+
//
613+
// self.resolve = nil
614+
// self.subscribe = { source, args, context, eventLoopGroup, info in
615+
// let result = try subscribe(source, args, context, info)
616+
// return eventLoopGroup.next().makeSucceededFuture(result)
617+
// }
618+
// }
584619
}
585620

586621
public typealias GraphQLFieldDefinitionMap = [String: GraphQLFieldDefinition]

0 commit comments

Comments
 (0)