Skip to content

Commit 6ee7b36

Browse files
simonljusCode-Hex
authored andcommitted
add interface type test cases for zod
1 parent 0659a02 commit 6ee7b36

File tree

1 file changed

+141
-1
lines changed

1 file changed

+141
-1
lines changed

tests/zod.spec.ts

Lines changed: 141 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { buildClientSchema, buildSchema, introspectionFromSchema } from 'graphql';
1+
import { getCachedDocumentNodeFromSchema } from '@graphql-codegen/plugin-helpers';
2+
import { buildClientSchema, buildSchema, introspectionFromSchema, isSpecifiedScalarType } from 'graphql';
23
import { dedent } from 'ts-dedent';
34

45
import { plugin } from '../src/index';
@@ -588,6 +589,145 @@ describe('zod', () => {
588589
});
589590
});
590591

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+
591731
describe('with withObjectType', () => {
592732
it('not generate if withObjectType false', async () => {
593733
const schema = buildSchema(/* GraphQL */ `

0 commit comments

Comments
 (0)