@@ -13,7 +13,7 @@ import { TsVisitor } from '@graphql-codegen/typescript';
1313import { buildApi , formatDirectiveConfig } from '../directive' ;
1414
1515const importZod = `import { z } from 'zod'` ;
16- const anySchema = `definedNonNullAnySchema`
16+ const anySchema = `definedNonNullAnySchema` ;
1717
1818export const ZodSchemaVisitor = ( schema : GraphQLSchema , config : ValidationSchemaPluginConfig ) => {
1919 const tsVisitor = new TsVisitor ( schema , config ) ;
@@ -28,11 +28,12 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
2828 return [ importZod ] ;
2929 } ,
3030 initialEmit : ( ) : string =>
31+ '\n' +
3132 [
3233 // Unfortunately, zod doesn’t provide non-null defined any schema.
3334 // This is a temporary hack until it is fixed.
3435 // see: https://github.com/colinhacks/zod/issues/884
35- 'type definedNonNullAny = {}' ,
36+ new DeclarationBlock ( { } ) . asKind ( 'type' ) . withName ( ' definedNonNullAny' ) . withContent ( ' {}') . string ,
3637 new DeclarationBlock ( { } )
3738 . export ( )
3839 . asKind ( 'const' )
@@ -43,7 +44,7 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
4344 . asKind ( 'const' )
4445 . withName ( `${ anySchema } : z.ZodSchema<definedNonNullAny>` )
4546 . withContent ( `z.any().refine((v) => isDefinedNonNullAny(v))` ) . string ,
46- ] . join ( '\n\n ' ) ,
47+ ] . join ( '\n' ) ,
4748 InputObjectTypeDefinition : ( node : InputObjectTypeDefinitionNode ) => {
4849 const name = tsVisitor . convertName ( node . name . value ) ;
4950 importTypes . push ( name ) ;
@@ -115,22 +116,18 @@ const generateInputObjectFieldTypeZodSchema = (
115116 if ( isNamedType ( type ) ) {
116117 const gen = generateNameNodeZodSchema ( tsVisitor , schema , type . name ) ;
117118 if ( isNonNullType ( parentType ) ) {
118- return gen
119+ return gen ;
119120 }
120121 if ( isListType ( parentType ) ) {
121- return `${ gen } .nullable()`
122+ return `${ gen } .nullable()` ;
122123 }
123- return `${ gen } .nullish()`
124+ return `${ gen } .nullish()` ;
124125 }
125126 console . warn ( 'unhandled type:' , type ) ;
126127 return '' ;
127128} ;
128129
129- const generateNameNodeZodSchema = (
130- tsVisitor : TsVisitor ,
131- schema : GraphQLSchema ,
132- node : NameNode
133- ) : string => {
130+ const generateNameNodeZodSchema = ( tsVisitor : TsVisitor , schema : GraphQLSchema , node : NameNode ) : string => {
134131 const typ = schema . getType ( node . value ) ;
135132
136133 if ( typ && typ . astNode ?. kind === 'InputObjectTypeDefinition' ) {
0 commit comments