Skip to content

Commit 6728e31

Browse files
author
Jay Herron
committed
Adds test for observable type checking
1 parent baba1bc commit 6728e31

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

Sources/GraphQL/Subscription/Subscribe.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ struct SourceEventStreamResult {
291291
self.errors = errors
292292
}
293293
}
294+
/// Observables MUST be declared as 'Any' due to Swift not having covariant generic support. Resolvers should handle type checks.
294295
typealias SourceEventStreamObservable = Observable<Any>
295296

296297

Tests/GraphQLTests/Subscription/SubscriptionTests.swift

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,39 @@ class SubscriptionTests : XCTestCase {
726726

727727
/// 'should resolve GraphQL error from source event stream'
728728
// Not necessary - Pub/sub implementation handles event erroring
729+
730+
/// Test incorrect observable publish type errors
731+
func testErrorWrongObservableType() throws {
732+
let db = EmailDb()
733+
let subscription = try db.subscription(query: """
734+
subscription ($priority: Int = 0) {
735+
importantEmail(priority: $priority) {
736+
email {
737+
from
738+
subject
739+
}
740+
inbox {
741+
unread
742+
total
743+
}
744+
}
745+
}
746+
""")
747+
748+
var currentResult = GraphQLResult()
749+
let _ = subscription.subscribe { event in
750+
currentResult = try! event.element!.wait()
751+
}.disposed(by: db.disposeBag)
752+
753+
db.publisher.onNext("String instead of email")
754+
755+
XCTAssertEqual(currentResult, GraphQLResult(
756+
data: ["importantEmail": nil],
757+
errors: [
758+
GraphQLError(message: "String is not Email")
759+
]
760+
))
761+
}
729762
}
730763

731764
// MARK: Types
@@ -844,17 +877,23 @@ class EmailDb {
844877
func defaultSchema() -> GraphQLSchema {
845878
return emailSchemaWithResolvers(
846879
resolve: {emailAny, _, _, eventLoopGroup, _ throws -> EventLoopFuture<Any?> in
847-
let email = emailAny as! Email
848-
return eventLoopGroup.next().makeSucceededFuture(EmailEvent(
849-
email: email,
850-
inbox: Inbox(emails: self.emails)
851-
))
880+
if let email = emailAny as? Email {
881+
return eventLoopGroup.next().makeSucceededFuture(EmailEvent(
882+
email: email,
883+
inbox: Inbox(emails: self.emails)
884+
))
885+
} else {
886+
throw GraphQLError(message: "\(type(of:emailAny)) is not Email")
887+
}
852888
},
853889
subscribe: {_, args, _, eventLoopGroup, _ throws -> EventLoopFuture<Any?> in
854890
let priority = args["priority"].int ?? 0
855-
let filtered = self.publisher.filter { emailAny in
856-
let email = emailAny as! Email
857-
return email.priority >= priority
891+
let filtered = self.publisher.filter { emailAny throws in
892+
if let email = emailAny as? Email {
893+
return email.priority >= priority
894+
} else {
895+
return true
896+
}
858897
}
859898
return eventLoopGroup.next().makeSucceededFuture(filtered)
860899
}

0 commit comments

Comments
 (0)