diff --git a/src/zod/index.ts b/src/zod/index.ts index 3d01931c..b844ed1c 100644 --- a/src/zod/index.ts +++ b/src/zod/index.ts @@ -5,6 +5,7 @@ import { GraphQLSchema, InputObjectTypeDefinitionNode, InputValueDefinitionNode, + Kind, NameNode, ObjectTypeDefinitionNode, TypeNode, @@ -130,16 +131,16 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema enumDeclarations.push( config.enumsAsTypes ? new DeclarationBlock({}) - .export() - .asKind('const') - .withName(`${enumname}Schema`) - .withContent(`z.enum([${node.values?.map(enumOption => `'${enumOption.name.value}'`).join(', ')}])`) - .string + .export() + .asKind('const') + .withName(`${enumname}Schema`) + .withContent(`z.enum([${node.values?.map(enumOption => `'${enumOption.name.value}'`).join(', ')}])`) + .string : new DeclarationBlock({}) - .export() - .asKind('const') - .withName(`${enumname}Schema`) - .withContent(`z.nativeEnum(${enumname})`).string + .export() + .asKind('const') + .withName(`${enumname}Schema`) + .withContent(`z.nativeEnum(${enumname})`).string ); }, }, @@ -220,7 +221,19 @@ const generateFieldTypeZodSchema = ( if (isListType(parentType)) { return `${gen}.nullable()`; } - const appliedDirectivesGen = applyDirectives(config, field, gen); + let appliedDirectivesGen = applyDirectives(config, field, gen); + + if (field.kind === Kind.INPUT_VALUE_DEFINITION) { + const { defaultValue } = field; + + if (defaultValue?.kind === Kind.INT || defaultValue?.kind === Kind.FLOAT || defaultValue?.kind === Kind.BOOLEAN) { + appliedDirectivesGen = `${appliedDirectivesGen}.default(${defaultValue.value})`; + } + if ((defaultValue?.kind === Kind.STRING) || (defaultValue?.kind === Kind.ENUM)) { + appliedDirectivesGen = `${appliedDirectivesGen}.default("${defaultValue.value}")`; + } + } + if (isNonNullType(parentType)) { if (visitor.shouldEmitAsNotAllowEmptyString(type.name.value)) { return `${appliedDirectivesGen}.min(1)`; diff --git a/tests/zod.spec.ts b/tests/zod.spec.ts index 67a676c2..701af8f8 100644 --- a/tests/zod.spec.ts +++ b/tests/zod.spec.ts @@ -403,6 +403,40 @@ describe('zod', () => { expect(result.content).toContain('export function SayISchema(): z.ZodObject> {'); }); + it('with default input values', async () => { + const schema = buildSchema(/* GraphQL */ ` + enum PageType { + PUBLIC + BASIC_AUTH + } + input PageInput { + pageType: PageType! = PUBLIC + greeting: String = "Hello" + score: Int = 100 + ratio: Float = 0.5 + isMember: Boolean = true + } + `); + const result = await plugin( + schema, + [], + { + schema: 'zod', + importFrom: './types', + }, + {} + ); + + expect(result.content).toContain('export const PageTypeSchema = z.nativeEnum(PageType)'); + expect(result.content).toContain('export function PageInputSchema(): z.ZodObject>'); + + expect(result.content).toContain('pageType: PageTypeSchema.default("PUBLIC")'); + expect(result.content).toContain('greeting: z.string().default("Hello").nullish()'); + expect(result.content).toContain('score: z.number().default(100).nullish()'); + expect(result.content).toContain('ratio: z.number().default(0.5).nullish()'); + expect(result.content).toContain('isMember: z.boolean().default(true).nullish()'); + }); + describe('issues #19', () => { it('string field', async () => { const schema = buildSchema(/* GraphQL */ `