@@ -13,7 +13,7 @@ import { TsVisitor } from '@graphql-codegen/typescript';
13
13
import { buildApi , formatDirectiveConfig } from '../directive' ;
14
14
15
15
const importZod = `import { z } from 'zod'` ;
16
- const anySchema = `definedNonNullAnySchema`
16
+ const anySchema = `definedNonNullAnySchema` ;
17
17
18
18
export const ZodSchemaVisitor = ( schema : GraphQLSchema , config : ValidationSchemaPluginConfig ) => {
19
19
const tsVisitor = new TsVisitor ( schema , config ) ;
@@ -28,11 +28,12 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
28
28
return [ importZod ] ;
29
29
} ,
30
30
initialEmit : ( ) : string =>
31
+ '\n' +
31
32
[
32
33
// Unfortunately, zod doesn’t provide non-null defined any schema.
33
34
// This is a temporary hack until it is fixed.
34
35
// see: https://github.com/colinhacks/zod/issues/884
35
- 'type definedNonNullAny = {}' ,
36
+ new DeclarationBlock ( { } ) . asKind ( 'type' ) . withName ( ' definedNonNullAny' ) . withContent ( ' {}') . string ,
36
37
new DeclarationBlock ( { } )
37
38
. export ( )
38
39
. asKind ( 'const' )
@@ -43,7 +44,7 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
43
44
. asKind ( 'const' )
44
45
. withName ( `${ anySchema } : z.ZodSchema<definedNonNullAny>` )
45
46
. withContent ( `z.any().refine((v) => isDefinedNonNullAny(v))` ) . string ,
46
- ] . join ( '\n\n ' ) ,
47
+ ] . join ( '\n' ) ,
47
48
InputObjectTypeDefinition : ( node : InputObjectTypeDefinitionNode ) => {
48
49
const name = tsVisitor . convertName ( node . name . value ) ;
49
50
importTypes . push ( name ) ;
@@ -115,22 +116,18 @@ const generateInputObjectFieldTypeZodSchema = (
115
116
if ( isNamedType ( type ) ) {
116
117
const gen = generateNameNodeZodSchema ( tsVisitor , schema , type . name ) ;
117
118
if ( isNonNullType ( parentType ) ) {
118
- return gen
119
+ return gen ;
119
120
}
120
121
if ( isListType ( parentType ) ) {
121
- return `${ gen } .nullable()`
122
+ return `${ gen } .nullable()` ;
122
123
}
123
- return `${ gen } .nullish()`
124
+ return `${ gen } .nullish()` ;
124
125
}
125
126
console . warn ( 'unhandled type:' , type ) ;
126
127
return '' ;
127
128
} ;
128
129
129
- const generateNameNodeZodSchema = (
130
- tsVisitor : TsVisitor ,
131
- schema : GraphQLSchema ,
132
- node : NameNode
133
- ) : string => {
130
+ const generateNameNodeZodSchema = ( tsVisitor : TsVisitor , schema : GraphQLSchema , node : NameNode ) : string => {
134
131
const typ = schema . getType ( node . value ) ;
135
132
136
133
if ( typ && typ . astNode ?. kind === 'InputObjectTypeDefinition' ) {
0 commit comments