Skip to content

Commit fbe96f6

Browse files
author
Jay Herron
committed
Minor final cleanup
1 parent 6728e31 commit fbe96f6

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

Sources/GraphQL/GraphQL.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ public struct GraphQLResult : Equatable, Codable, CustomStringConvertible {
4040
}
4141
}
4242

43+
/// SubscriptionResult wraps the observable and error data returned by the subscribe request.
44+
public struct SubscriptionResult {
45+
public let observable: SubscriptionObservable?
46+
public let errors: [GraphQLError]
47+
48+
public init(observable: SubscriptionObservable? = nil, errors: [GraphQLError] = []) {
49+
self.observable = observable
50+
self.errors = errors
51+
}
52+
}
53+
/// SubscriptionObservable represents an event stream of fully resolved GraphQL subscription results. Subscribers can be added to this stream.
54+
public typealias SubscriptionObservable = Observable<Future<GraphQLResult>>
55+
4356
/// This is the primary entry point function for fulfilling GraphQL operations
4457
/// by parsing, validating, and executing a GraphQL document along side a
4558
/// GraphQL schema.

Sources/GraphQL/Subscription/Subscribe.swift

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ func subscribe(
3737
operationName: String? = nil
3838
) -> EventLoopFuture<SubscriptionResult> {
3939

40-
4140
let sourceFuture = createSourceEventStream(
4241
queryStrategy: queryStrategy,
4342
mutationStrategy: mutationStrategy,
@@ -252,7 +251,7 @@ func executeSubscription(
252251
return SourceEventStreamResult(errors: context.errors)
253252
} else if let error = resolved as? GraphQLError {
254253
return SourceEventStreamResult(errors: [error])
255-
} else if let observable = resolved as? SourceEventStreamObservable {
254+
} else if let observable = resolved as? SourceEventObservable {
256255
return SourceEventStreamResult(observable: observable)
257256
} else if resolved == nil {
258257
return SourceEventStreamResult(errors: [
@@ -262,36 +261,23 @@ func executeSubscription(
262261
let resolvedObj = resolved as AnyObject
263262
return SourceEventStreamResult(errors: [
264263
GraphQLError(
265-
message: "Subscription field resolver must return SourceEventStreamObservable. Received: '\(resolvedObj)'"
264+
message: "Subscription field resolver must return SourceEventObservable. Received: '\(resolvedObj)'"
266265
)
267266
])
268267
}
269268
}
270269
}
271270

272-
/// SubscriptionResult wraps the observable and error data returned by the subscribe request.
273-
public struct SubscriptionResult {
274-
public let observable: SubscriptionObservable?
275-
public let errors: [GraphQLError]
276-
277-
public init(observable: SubscriptionObservable? = nil, errors: [GraphQLError] = []) {
278-
self.observable = observable
279-
self.errors = errors
280-
}
281-
}
282-
/// SubscriptionObservable represents an event stream of fully resolved GraphQL subscription results. It can be used to add subscribers to this stream
283-
public typealias SubscriptionObservable = Observable<Future<GraphQLResult>>
284-
285271
struct SourceEventStreamResult {
286-
public let observable: SourceEventStreamObservable?
272+
public let observable: SourceEventObservable?
287273
public let errors: [GraphQLError]
288274

289-
public init(observable: SourceEventStreamObservable? = nil, errors: [GraphQLError] = []) {
275+
public init(observable: SourceEventObservable? = nil, errors: [GraphQLError] = []) {
290276
self.observable = observable
291277
self.errors = errors
292278
}
293279
}
294-
/// Observables MUST be declared as 'Any' due to Swift not having covariant generic support. Resolvers should handle type checks.
295-
typealias SourceEventStreamObservable = Observable<Any>
296-
297280

281+
// Subscription resolvers MUST return observables that are declared as 'Any' due to Swift not having covariant generic support for type
282+
// checking. Normal resolvers for subscription fields should handle type casting, same as resolvers for query fields.
283+
typealias SourceEventObservable = Observable<Any>

Tests/GraphQLTests/Subscription/SubscriptionTests.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class SubscriptionTests : XCTestCase {
88

99
// MARK: Test primary graphqlSubscribe function
1010

11-
/// This is not present in graphql-js.
11+
/// This test is not present in graphql-js, but just tests basic functionality.
1212
func testGraphqlSubscribe() throws {
1313
let db = EmailDb()
1414
let schema = db.defaultSchema()
@@ -261,7 +261,7 @@ class SubscriptionTests : XCTestCase {
261261
let graphQLError = error as! GraphQLError
262262
XCTAssertEqual(
263263
graphQLError.message,
264-
"Subscription field resolver must return SourceEventStreamObservable. Received: 'test'"
264+
"Subscription field resolver must return SourceEventObservable. Received: 'test'"
265265
)
266266
}
267267
}
@@ -462,6 +462,7 @@ class SubscriptionTests : XCTestCase {
462462
var currentResult = GraphQLResult()
463463
let _ = subscription.subscribe { event in
464464
currentResult = try! event.element!.wait()
465+
print(currentResult)
465466
}.disposed(by: db.disposeBag)
466467

467468
db.trigger(email: Email(

0 commit comments

Comments
 (0)