@@ -502,6 +502,83 @@ class SubscriptionTests : XCTestCase {
502
502
] ]
503
503
) )
504
504
}
505
+
506
+ /// Tests that subscriptions use arguments correctly.
507
+ /// This is not in the graphql-js tests.
508
+ func testArguments( ) throws {
509
+ let db = EmailDb ( )
510
+ let subscription = try db. subscription ( query: """
511
+ subscription ($priority: Int = 5) {
512
+ importantEmail(priority: $priority) {
513
+ email {
514
+ from
515
+ subject
516
+ }
517
+ inbox {
518
+ unread
519
+ total
520
+ }
521
+ }
522
+ }
523
+ """ )
524
+
525
+ var currentResult = GraphQLResult ( )
526
+ let _ = subscription. subscribe { event in
527
+ currentResult = try ! event. element!. wait ( )
528
+ } . disposed ( by: db. disposeBag)
529
+
530
+ db. trigger ( email: Email (
531
+
532
+ subject: " Alright " ,
533
+ message: " Tests are good " ,
534
+ unread: true ,
535
+ priority: 7
536
+ ) )
537
+ let firstMessageExpected = GraphQLResult (
538
+ data: [ " importantEmail " : [
539
+ " inbox " : [
540
+ " total " : 2 ,
541
+ " unread " : 1
542
+ ] ,
543
+ " email " : [
544
+ " subject " : " Alright " ,
545
+
546
+ ]
547
+ ] ]
548
+ )
549
+ XCTAssertEqual ( currentResult, firstMessageExpected)
550
+
551
+ // Low priority email shouldn't trigger an event
552
+ db. trigger ( email: Email (
553
+
554
+ subject: " Not Important " ,
555
+ message: " Ignore this email " ,
556
+ unread: true ,
557
+ priority: 2
558
+ ) )
559
+ XCTAssertEqual ( currentResult, firstMessageExpected)
560
+
561
+ // Higher priority one should trigger again
562
+ db. trigger ( email: Email (
563
+
564
+ subject: " Tools " ,
565
+ message: " I <3 making things " ,
566
+ unread: true ,
567
+ priority: 5
568
+ ) )
569
+ XCTAssertEqual ( currentResult, GraphQLResult (
570
+ data: [ " importantEmail " : [
571
+ " inbox " : [
572
+ " total " : 4 ,
573
+ " unread " : 3
574
+ ] ,
575
+ " email " : [
576
+ " subject " : " Tools " ,
577
+
578
+ ]
579
+ ] ]
580
+ ) )
581
+ }
505
582
506
583
/// 'should not trigger when subscription is already done'
507
584
func testNoTriggerAfterDone( ) throws {
@@ -657,6 +734,15 @@ struct Email : Encodable {
657
734
let subject : String
658
735
let message : String
659
736
let unread : Bool
737
+ let priority : Int
738
+
739
+ init ( from: String , subject: String , message: String , unread: Bool , priority: Int = 0 ) {
740
+ self . from = from
741
+ self . subject = subject
742
+ self . message = message
743
+ self . unread = unread
744
+ self . priority = priority
745
+ }
660
746
}
661
747
662
748
struct Inbox : Encodable {
@@ -764,8 +850,13 @@ class EmailDb {
764
850
inbox: Inbox ( emails: self . emails)
765
851
) )
766
852
} ,
767
- subscribe: { _, _, _, eventLoopGroup, _ throws -> EventLoopFuture < Any ? > in
768
- return eventLoopGroup. next ( ) . makeSucceededFuture ( self . publisher)
853
+ subscribe: { _, args, _, eventLoopGroup, _ throws -> EventLoopFuture < Any ? > in
854
+ 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
858
+ }
859
+ return eventLoopGroup. next ( ) . makeSucceededFuture ( filtered)
769
860
}
770
861
)
771
862
}
0 commit comments