|
1 |
| -import { buildClientSchema, buildSchema, introspectionFromSchema } from 'graphql'; |
| 1 | +import { getCachedDocumentNodeFromSchema } from '@graphql-codegen/plugin-helpers'; |
| 2 | +import { buildClientSchema, buildSchema, introspectionFromSchema, isSpecifiedScalarType } from 'graphql'; |
2 | 3 | import { dedent } from 'ts-dedent';
|
3 | 4 |
|
4 | 5 | import { plugin } from '../src/index';
|
@@ -588,6 +589,145 @@ describe('zod', () => {
|
588 | 589 | });
|
589 | 590 | });
|
590 | 591 |
|
| 592 | + describe('with withInterfaceType', () => { |
| 593 | + it('not generate if withObjectType false', async () => { |
| 594 | + const schema = buildSchema(/* GraphQL */ ` |
| 595 | + interface User { |
| 596 | + id: ID! |
| 597 | + name: String |
| 598 | + } |
| 599 | + `); |
| 600 | + const result = await plugin( |
| 601 | + schema, |
| 602 | + [], |
| 603 | + { |
| 604 | + schema: 'zod', |
| 605 | + }, |
| 606 | + {} |
| 607 | + ); |
| 608 | + expect(result.content).not.toContain('export function UserSchema(): z.ZodObject<Properties<User>>'); |
| 609 | + }); |
| 610 | + |
| 611 | + it('generate interface type contains interface type', async () => { |
| 612 | + const schema = buildSchema(/* GraphQL */ ` |
| 613 | + interface Book { |
| 614 | + author: Author |
| 615 | + title: String |
| 616 | + } |
| 617 | +
|
| 618 | + interface Author { |
| 619 | + books: [Book] |
| 620 | + name: String |
| 621 | + } |
| 622 | + `); |
| 623 | + const result = await plugin( |
| 624 | + schema, |
| 625 | + [], |
| 626 | + { |
| 627 | + schema: 'zod', |
| 628 | + withInterfaceType: true, |
| 629 | + }, |
| 630 | + {} |
| 631 | + ); |
| 632 | + const wantContains = [ |
| 633 | + 'export function AuthorSchema(): z.ZodObject<Properties<Author>> {', |
| 634 | + 'books: z.array(BookSchema().nullable()).nullish(),', |
| 635 | + 'name: z.string().nullish()', |
| 636 | + |
| 637 | + 'export function BookSchema(): z.ZodObject<Properties<Book>> {', |
| 638 | + 'author: AuthorSchema().nullish(),', |
| 639 | + 'title: z.string().nullish()', |
| 640 | + ]; |
| 641 | + for (const wantContain of wantContains) { |
| 642 | + expect(result.content).toContain(wantContain); |
| 643 | + } |
| 644 | + }); |
| 645 | + |
| 646 | + it('generate object type contains interface type', async () => { |
| 647 | + const schema = buildSchema(/* GraphQL */ ` |
| 648 | + interface Book { |
| 649 | + title: String! |
| 650 | + author: Author! |
| 651 | + } |
| 652 | +
|
| 653 | + type Textbook implements Book { |
| 654 | + title: String! |
| 655 | + author: Author! |
| 656 | + courses: [String!]! |
| 657 | + } |
| 658 | +
|
| 659 | + type ColoringBook implements Book { |
| 660 | + title: String! |
| 661 | + author: Author! |
| 662 | + colors: [String!]! |
| 663 | + } |
| 664 | +
|
| 665 | + type Author { |
| 666 | + books: [Book!] |
| 667 | + name: String |
| 668 | + } |
| 669 | + `); |
| 670 | + const result = await plugin( |
| 671 | + schema, |
| 672 | + [], |
| 673 | + { |
| 674 | + schema: 'zod', |
| 675 | + withInterfaceType: true, |
| 676 | + withObjectType: true, |
| 677 | + }, |
| 678 | + {} |
| 679 | + ); |
| 680 | + const wantContains = [ |
| 681 | + [ |
| 682 | + 'export function BookSchema(): z.ZodObject<Properties<Book>> {', |
| 683 | + 'return z.object({', |
| 684 | + 'title: z.string(),', |
| 685 | + 'author: AuthorSchema()', |
| 686 | + '})', |
| 687 | + '}', |
| 688 | + ], |
| 689 | + |
| 690 | + [ |
| 691 | + 'export function TextbookSchema(): z.ZodObject<Properties<Textbook>> {', |
| 692 | + 'return z.object({', |
| 693 | + "__typename: z.literal('Textbook').optional(),", |
| 694 | + 'title: z.string(),', |
| 695 | + 'author: AuthorSchema(),', |
| 696 | + 'courses: z.array(z.string())', |
| 697 | + '})', |
| 698 | + '}', |
| 699 | + ], |
| 700 | + |
| 701 | + [ |
| 702 | + 'export function ColoringBookSchema(): z.ZodObject<Properties<ColoringBook>> {', |
| 703 | + 'return z.object({', |
| 704 | + "__typename: z.literal('ColoringBook').optional(),", |
| 705 | + 'title: z.string(),', |
| 706 | + 'author: AuthorSchema(),', |
| 707 | + 'colors: z.array(z.string())', |
| 708 | + '})', |
| 709 | + '}', |
| 710 | + ], |
| 711 | + |
| 712 | + [ |
| 713 | + 'export function AuthorSchema(): z.ZodObject<Properties<Author>> {', |
| 714 | + 'return z.object({', |
| 715 | + "__typename: z.literal('Author').optional()", |
| 716 | + 'books: z.array(BookSchema()).nullish()', |
| 717 | + 'name: z.string().nullish()', |
| 718 | + '})', |
| 719 | + '}', |
| 720 | + ], |
| 721 | + ]; |
| 722 | + |
| 723 | + for (const wantContain of wantContains) { |
| 724 | + for (const wantContainLine of wantContain) { |
| 725 | + expect(result.content).toContain(wantContainLine); |
| 726 | + } |
| 727 | + } |
| 728 | + }); |
| 729 | + }); |
| 730 | + |
591 | 731 | describe('with withObjectType', () => {
|
592 | 732 | it('not generate if withObjectType false', async () => {
|
593 | 733 | const schema = buildSchema(/* GraphQL */ `
|
|
0 commit comments