@@ -726,6 +726,39 @@ class SubscriptionTests : XCTestCase {
726
726
727
727
/// 'should resolve GraphQL error from source event stream'
728
728
// 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
+ }
729
762
}
730
763
731
764
// MARK: Types
@@ -844,17 +877,23 @@ class EmailDb {
844
877
func defaultSchema( ) -> GraphQLSchema {
845
878
return emailSchemaWithResolvers (
846
879
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
+ }
852
888
} ,
853
889
subscribe: { _, args, _, eventLoopGroup, _ throws -> EventLoopFuture < Any ? > in
854
890
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
+ }
858
897
}
859
898
return eventLoopGroup. next ( ) . makeSucceededFuture ( filtered)
860
899
}
0 commit comments