@@ -577,4 +577,124 @@ def post(id:, expires_in: nil)
577
577
expect ( ::Post ) . not_to have_received ( :all )
578
578
end
579
579
end
580
+
581
+ describe "union caching" do
582
+ let! ( :post ) { Post . create ( id : 1 , title : "Post #1" ) }
583
+ let! ( :user ) { User . create ( id : 2 , name : "User #2" ) }
584
+
585
+ let ( :schema ) do
586
+ build_schema do
587
+ query (
588
+ Class . new ( Types ::Query ) {
589
+ field :last_activity , Types ::Activity , null : false
590
+
591
+ define_method ( :last_activity , -> { ::Post . find ( 1 ) } )
592
+ }
593
+ )
594
+ end
595
+ end
596
+
597
+ let ( :query ) do
598
+ <<~GQL
599
+ query getLastActivity {
600
+ lastActivity {
601
+ ...on PostType {
602
+ id
603
+ cachedAvatarUrl
604
+ }
605
+ ...on UserType {
606
+ id
607
+ cachedAvatarUrl
608
+ }
609
+ }
610
+ }
611
+ GQL
612
+ end
613
+
614
+ it "returns cached data" do
615
+ expect ( execute_query . dig ( "data" ) ) . to eq (
616
+ "lastActivity" =>
617
+ { "cachedAvatarUrl" => "http://example.com/img/posts/#{ post . id } " , "id" => post . id . to_s }
618
+ )
619
+ end
620
+
621
+ context "when unions are nested" do
622
+ let ( :query ) do
623
+ <<~GQL
624
+ query getLastActivity {
625
+ lastActivity {
626
+ ...on PostType {
627
+ id
628
+ relatedActivity {
629
+ ...on PostType {
630
+ id
631
+ cachedAvatarUrl
632
+ }
633
+ ...on UserType {
634
+ id
635
+ cachedAvatarUrl
636
+ }
637
+ }
638
+ }
639
+ ...on UserType {
640
+ id
641
+ }
642
+ }
643
+ }
644
+ GQL
645
+ end
646
+
647
+ it "returns cached data" do
648
+ expect ( execute_query . dig ( "data" ) ) . to eq (
649
+ "lastActivity" => {
650
+ "id" => "1" ,
651
+ "relatedActivity" => {
652
+ "cachedAvatarUrl" => "http://example.com/img/posts/#{ user . id } " ,
653
+ "id" => user . id . to_s
654
+ }
655
+ }
656
+ )
657
+ end
658
+ end
659
+
660
+ context "when array of union typed objects is returned" do
661
+ let ( :schema ) do
662
+ build_schema do
663
+ query (
664
+ Class . new ( Types ::Query ) {
665
+ field :feed , [ Types ::Activity ] , null : false
666
+
667
+ define_method ( :feed , -> { ::Post . all + ::User . all } )
668
+ }
669
+ )
670
+ end
671
+ end
672
+
673
+ let ( :query ) do
674
+ <<~GQL
675
+ query getFeed {
676
+ feed {
677
+ ...on PostType {
678
+ id
679
+ cachedAvatarUrl
680
+ }
681
+ ...on UserType {
682
+ id
683
+ cachedAvatarUrl
684
+ }
685
+ }
686
+ }
687
+ GQL
688
+ end
689
+
690
+ it "returns cached data" do
691
+ expect ( execute_query . dig ( "data" ) ) . to eq (
692
+ "feed" => [
693
+ { "cachedAvatarUrl" => "http://example.com/img/posts/#{ post . id } " , "id" => post . id . to_s } ,
694
+ { "cachedAvatarUrl" => "http://example.com/img/users/#{ user . id } " , "id" => user . id . to_s }
695
+ ]
696
+ )
697
+ end
698
+ end
699
+ end
580
700
end
0 commit comments