@@ -538,6 +538,104 @@ describe('myzod', () => {
538
538
expect ( result . content ) . not . toContain ( wantNotContain ) ;
539
539
}
540
540
} ) ;
541
+
542
+ it ( 'generate union types' , async ( ) => {
543
+ const schema = buildSchema ( /* GraphQL */ `
544
+ type Square {
545
+ size: Int
546
+ }
547
+ type Circle {
548
+ radius: Int
549
+ }
550
+ union Shape = Circle | Square
551
+ ` ) ;
552
+
553
+ const result = await plugin (
554
+ schema ,
555
+ [ ] ,
556
+ {
557
+ schema : 'myzod' ,
558
+ withObjectType : true ,
559
+ } ,
560
+ { }
561
+ ) ;
562
+
563
+ const wantContains = [
564
+ // Shape Schema
565
+ 'export function ShapeSchema() {' ,
566
+ 'myzod.union([CircleSchema(), SquareSchema()])' ,
567
+ '}' ,
568
+ ] ;
569
+ for ( const wantContain of wantContains ) {
570
+ expect ( result . content ) . toContain ( wantContain ) ;
571
+ }
572
+ } ) ;
573
+
574
+ it ( 'generate union types with single element' , async ( ) => {
575
+ const schema = buildSchema ( /* GraphQL */ `
576
+ type Square {
577
+ size: Int
578
+ }
579
+ type Circle {
580
+ radius: Int
581
+ }
582
+ union Shape = Circle | Square
583
+
584
+ type Geometry {
585
+ shape: Shape
586
+ }
587
+ ` ) ;
588
+
589
+ const result = await plugin (
590
+ schema ,
591
+ [ ] ,
592
+ {
593
+ schema : 'myzod' ,
594
+ withObjectType : true ,
595
+ } ,
596
+ { }
597
+ ) ;
598
+
599
+ const wantContains = [
600
+ 'export function GeometrySchema(): myzod.Type<Geometry> {' ,
601
+ 'return myzod.object({' ,
602
+ "__typename: myzod.literal('Geometry').optional()," ,
603
+ 'shape: ShapeSchema().optional().nullable()' ,
604
+ '}' ,
605
+ ] ;
606
+ for ( const wantContain of wantContains ) {
607
+ expect ( result . content ) . toContain ( wantContain ) ;
608
+ }
609
+ } ) ;
610
+
611
+ it ( 'correctly reference generated union types' , async ( ) => {
612
+ const schema = buildSchema ( /* GraphQL */ `
613
+ type Circle {
614
+ radius: Int
615
+ }
616
+ union Shape = Circle
617
+ ` ) ;
618
+
619
+ const result = await plugin (
620
+ schema ,
621
+ [ ] ,
622
+ {
623
+ schema : 'myzod' ,
624
+ withObjectType : true ,
625
+ } ,
626
+ { }
627
+ ) ;
628
+
629
+ const wantContains = [
630
+ // Shape Schema
631
+ 'export function ShapeSchema() {' ,
632
+ 'CircleSchema()' ,
633
+ '}' ,
634
+ ] ;
635
+ for ( const wantContain of wantContains ) {
636
+ expect ( result . content ) . toContain ( wantContain ) ;
637
+ }
638
+ } ) ;
541
639
} ) ;
542
640
543
641
it ( 'properly generates custom directive values' , async ( ) => {
0 commit comments