@@ -578,6 +578,83 @@ describe('readFragment', () => {
578
578
type document = getDocumentNode < fragment , schema > ;
579
579
const result = readFragment ( { } as document , { } as FragmentOf < document > ) ;
580
580
expectTypeOf < typeof result > ( ) . toEqualTypeOf < ResultOf < document > > ( ) ;
581
+
582
+ const narrowInput = { } as FragmentOf < document > & { __typename ?: 'BigTodo' } ;
583
+ const narrowed = readFragment ( { } as document , narrowInput ) ;
584
+ expectTypeOf < typeof narrowed > ( ) . toEqualTypeOf < {
585
+ __typename ?: 'BigTodo' ;
586
+ id : unknown ;
587
+ wallOfText : unknown ;
588
+ } > ( ) ;
589
+ } ) ;
590
+
591
+ it ( 'unmasks fragments of interfaces while narrowing types using input' , ( ) => {
592
+ type fragment = parseDocument < `
593
+ fragment Fields on ITodo {
594
+ id
595
+ ... on BigTodo {
596
+ wallOfText
597
+ }
598
+ ... on SmallTodo {
599
+ maxLength
600
+ }
601
+ }
602
+ `> ;
603
+
604
+ type document = getDocumentNode < fragment , schema > ;
605
+
606
+ const data : FragmentOf < document > & { __typename ?: 'SmallTodo' } = { } as any ;
607
+ const result = readFragment ( { } as document , data ) ;
608
+ expectTypeOf < typeof result > ( ) . toEqualTypeOf < {
609
+ __typename ?: 'SmallTodo' ;
610
+ id : unknown ;
611
+ maxLength : unknown ;
612
+ } > ( ) ;
613
+ } ) ;
614
+
615
+ it ( 'should allow for gradual narrowing' , ( ) => {
616
+ type childFragment = parseDocument < `
617
+ fragment Fields on ITodo {
618
+ id
619
+ ... on BigTodo {
620
+ wallOfText
621
+ }
622
+ ... on SmallTodo {
623
+ maxLength
624
+ }
625
+ }
626
+ `> ;
627
+
628
+ type parentFragment = parseDocument < `
629
+ fragment Parent on ITodo {
630
+ __typename
631
+ ...Fields
632
+ }
633
+ `> ;
634
+
635
+ type childFragmentDoc = getDocumentNode < childFragment , schema > ;
636
+ type parentFragmentDoc = getDocumentNode <
637
+ parentFragment ,
638
+ schema ,
639
+ getFragmentsOfDocuments < [ childFragmentDoc ] >
640
+ > ;
641
+
642
+ const input : ResultOf < parentFragmentDoc > = { } as any ;
643
+ if ( input . __typename === 'SmallTodo' ) {
644
+ const result = readFragment ( { } as childFragmentDoc , input ) ;
645
+ expectTypeOf < typeof result > ( ) . toEqualTypeOf < {
646
+ __typename ?: 'SmallTodo' ;
647
+ id : unknown ;
648
+ maxLength : unknown ;
649
+ } > ( ) ;
650
+ } else if ( input . __typename === 'BigTodo' ) {
651
+ const result = readFragment ( { } as childFragmentDoc , input ) ;
652
+ expectTypeOf < typeof result > ( ) . toEqualTypeOf < {
653
+ __typename ?: 'BigTodo' ;
654
+ id : unknown ;
655
+ wallOfText : unknown ;
656
+ } > ( ) ;
657
+ }
581
658
} ) ;
582
659
583
660
it ( 'unmasks fragments of interfaces with optional spreads' , ( ) => {
0 commit comments