@@ -12,16 +12,14 @@ import {
12
12
UnionTypeDefinitionNode ,
13
13
} from 'graphql' ;
14
14
import { DeclarationBlock , indent } from '@graphql-codegen/visitor-plugin-common' ;
15
- import { TsVisitor } from '@graphql-codegen/typescript ' ;
15
+ import { Visitor } from '../visitor ' ;
16
16
import { buildApi , formatDirectiveConfig } from '../directive' ;
17
17
import { SchemaVisitor } from '../types' ;
18
18
19
19
const importZod = `import * as myzod from 'myzod'` ;
20
20
const anySchema = `definedNonNullAnySchema` ;
21
21
22
22
export const MyZodSchemaVisitor = ( schema : GraphQLSchema , config : ValidationSchemaPluginConfig ) : SchemaVisitor => {
23
- const tsVisitor = new TsVisitor ( schema , config ) ;
24
-
25
23
const importTypes : string [ ] = [ ] ;
26
24
27
25
return {
@@ -39,12 +37,11 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
39
37
] . join ( '\n' ) ,
40
38
InputObjectTypeDefinition : {
41
39
leave : ( node : InputObjectTypeDefinitionNode ) => {
42
- const name = tsVisitor . convertName ( node . name . value ) ;
40
+ const visitor = new Visitor ( 'input' , schema , config ) ;
41
+ const name = visitor . convertName ( node . name . value ) ;
43
42
importTypes . push ( name ) ;
44
43
45
- const shape = node . fields
46
- ?. map ( field => generateFieldMyZodSchema ( config , tsVisitor , schema , field , 2 ) )
47
- . join ( ',\n' ) ;
44
+ const shape = node . fields ?. map ( field => generateFieldMyZodSchema ( config , visitor , field , 2 ) ) . join ( ',\n' ) ;
48
45
49
46
return new DeclarationBlock ( { } )
50
47
. export ( )
@@ -55,12 +52,11 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
55
52
} ,
56
53
ObjectTypeDefinition : {
57
54
leave : ObjectTypeDefinitionBuilder ( config . withObjectType , ( node : ObjectTypeDefinitionNode ) => {
58
- const name = tsVisitor . convertName ( node . name . value ) ;
55
+ const visitor = new Visitor ( 'output' , schema , config ) ;
56
+ const name = visitor . convertName ( node . name . value ) ;
59
57
importTypes . push ( name ) ;
60
58
61
- const shape = node . fields
62
- ?. map ( field => generateFieldMyZodSchema ( config , tsVisitor , schema , field , 2 ) )
63
- . join ( ',\n' ) ;
59
+ const shape = node . fields ?. map ( field => generateFieldMyZodSchema ( config , visitor , field , 2 ) ) . join ( ',\n' ) ;
64
60
65
61
return new DeclarationBlock ( { } )
66
62
. export ( )
@@ -78,7 +74,8 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
78
74
} ,
79
75
EnumTypeDefinition : {
80
76
leave : ( node : EnumTypeDefinitionNode ) => {
81
- const enumname = tsVisitor . convertName ( node . name . value ) ;
77
+ const visitor = new Visitor ( 'both' , schema , config ) ;
78
+ const enumname = visitor . convertName ( node . name . value ) ;
82
79
importTypes . push ( enumname ) ;
83
80
// z.enum are basically myzod.literals
84
81
if ( config . enumsAsTypes ) {
@@ -101,11 +98,13 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
101
98
leave : ( node : UnionTypeDefinitionNode ) => {
102
99
if ( ! node . types || ! config . withObjectType ) return ;
103
100
104
- const unionName = tsVisitor . convertName ( node . name . value ) ;
101
+ const visitor = new Visitor ( 'output' , schema , config ) ;
102
+
103
+ const unionName = visitor . convertName ( node . name . value ) ;
105
104
const unionElements = node . types
106
105
?. map ( t => {
107
- const element = tsVisitor . convertName ( t . name . value ) ;
108
- const typ = schema . getType ( t . name . value ) ;
106
+ const element = visitor . convertName ( t . name . value ) ;
107
+ const typ = visitor . getType ( t . name . value ) ;
109
108
if ( typ ?. astNode ?. kind === 'EnumTypeDefinition' ) {
110
109
return `${ element } Schema` ;
111
110
}
@@ -126,25 +125,23 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
126
125
127
126
const generateFieldMyZodSchema = (
128
127
config : ValidationSchemaPluginConfig ,
129
- tsVisitor : TsVisitor ,
130
- schema : GraphQLSchema ,
128
+ visitor : Visitor ,
131
129
field : InputValueDefinitionNode | FieldDefinitionNode ,
132
130
indentCount : number
133
131
) : string => {
134
- const gen = generateFieldTypeMyZodSchema ( config , tsVisitor , schema , field , field . type ) ;
132
+ const gen = generateFieldTypeMyZodSchema ( config , visitor , field , field . type ) ;
135
133
return indent ( `${ field . name . value } : ${ maybeLazy ( field . type , gen ) } ` , indentCount ) ;
136
134
} ;
137
135
138
136
const generateFieldTypeMyZodSchema = (
139
137
config : ValidationSchemaPluginConfig ,
140
- tsVisitor : TsVisitor ,
141
- schema : GraphQLSchema ,
138
+ visitor : Visitor ,
142
139
field : InputValueDefinitionNode | FieldDefinitionNode ,
143
140
type : TypeNode ,
144
141
parentType ?: TypeNode
145
142
) : string => {
146
143
if ( isListType ( type ) ) {
147
- const gen = generateFieldTypeMyZodSchema ( config , tsVisitor , schema , field , type . type , type ) ;
144
+ const gen = generateFieldTypeMyZodSchema ( config , visitor , field , type . type , type ) ;
148
145
if ( ! isNonNullType ( parentType ) ) {
149
146
const arrayGen = `myzod.array(${ maybeLazy ( type . type , gen ) } )` ;
150
147
const maybeLazyGen = applyDirectives ( config , field , arrayGen ) ;
@@ -153,18 +150,18 @@ const generateFieldTypeMyZodSchema = (
153
150
return `myzod.array(${ maybeLazy ( type . type , gen ) } )` ;
154
151
}
155
152
if ( isNonNullType ( type ) ) {
156
- const gen = generateFieldTypeMyZodSchema ( config , tsVisitor , schema , field , type . type , type ) ;
153
+ const gen = generateFieldTypeMyZodSchema ( config , visitor , field , type . type , type ) ;
157
154
return maybeLazy ( type . type , gen ) ;
158
155
}
159
156
if ( isNamedType ( type ) ) {
160
- const gen = generateNameNodeMyZodSchema ( config , tsVisitor , schema , type . name ) ;
157
+ const gen = generateNameNodeMyZodSchema ( config , visitor , type . name ) ;
161
158
if ( isListType ( parentType ) ) {
162
159
return `${ gen } .nullable()` ;
163
160
}
164
161
const appliedDirectivesGen = applyDirectives ( config , field , gen ) ;
165
162
if ( isNonNullType ( parentType ) ) {
166
163
if ( config . notAllowEmptyString === true ) {
167
- const tsType = tsVisitor . scalars [ type . name . value ] ;
164
+ const tsType = visitor . getScalarType ( type . name . value ) ;
168
165
if ( tsType === 'string' ) return `${ gen } .min(1)` ;
169
166
}
170
167
return appliedDirectivesGen ;
@@ -192,33 +189,32 @@ const applyDirectives = (
192
189
193
190
const generateNameNodeMyZodSchema = (
194
191
config : ValidationSchemaPluginConfig ,
195
- tsVisitor : TsVisitor ,
196
- schema : GraphQLSchema ,
192
+ visitor : Visitor ,
197
193
node : NameNode
198
194
) : string => {
199
- const typ = schema . getType ( node . value ) ;
195
+ const converter = visitor . getNameNodeConverter ( node ) ;
200
196
201
- if ( typ ?. astNode ?. kind === 'InputObjectTypeDefinition' ) {
202
- const enumName = tsVisitor . convertName ( typ . astNode . name . value ) ;
203
- return `${ enumName } Schema()` ;
197
+ if ( converter ?. targetKind === 'InputObjectTypeDefinition' ) {
198
+ const name = converter . convertName ( ) ;
199
+ return `${ name } Schema()` ;
204
200
}
205
201
206
- if ( typ ?. astNode ?. kind === 'ObjectTypeDefinition' ) {
207
- const enumName = tsVisitor . convertName ( typ . astNode . name . value ) ;
208
- return `${ enumName } Schema()` ;
202
+ if ( converter ?. targetKind === 'ObjectTypeDefinition' ) {
203
+ const name = converter . convertName ( ) ;
204
+ return `${ name } Schema()` ;
209
205
}
210
206
211
- if ( typ ?. astNode ?. kind === 'EnumTypeDefinition' ) {
212
- const enumName = tsVisitor . convertName ( typ . astNode . name . value ) ;
213
- return `${ enumName } Schema` ;
207
+ if ( converter ?. targetKind === 'EnumTypeDefinition' ) {
208
+ const name = converter . convertName ( ) ;
209
+ return `${ name } Schema` ;
214
210
}
215
211
216
- if ( typ ?. astNode ?. kind === 'UnionTypeDefinition' ) {
217
- const enumName = tsVisitor . convertName ( typ . astNode . name . value ) ;
218
- return `${ enumName } Schema()` ;
212
+ if ( converter ?. targetKind === 'UnionTypeDefinition' ) {
213
+ const name = converter . convertName ( ) ;
214
+ return `${ name } Schema()` ;
219
215
}
220
216
221
- return myzod4Scalar ( config , tsVisitor , node . value ) ;
217
+ return myzod4Scalar ( config , visitor , node . value ) ;
222
218
} ;
223
219
224
220
const maybeLazy = ( type : TypeNode , schema : string ) : string => {
@@ -228,11 +224,11 @@ const maybeLazy = (type: TypeNode, schema: string): string => {
228
224
return schema ;
229
225
} ;
230
226
231
- const myzod4Scalar = ( config : ValidationSchemaPluginConfig , tsVisitor : TsVisitor , scalarName : string ) : string => {
227
+ const myzod4Scalar = ( config : ValidationSchemaPluginConfig , visitor : Visitor , scalarName : string ) : string => {
232
228
if ( config . scalarSchemas ?. [ scalarName ] ) {
233
229
return config . scalarSchemas [ scalarName ] ;
234
230
}
235
- const tsType = tsVisitor . scalars [ scalarName ] ;
231
+ const tsType = visitor . getScalarType ( scalarName ) ;
236
232
switch ( tsType ) {
237
233
case 'string' :
238
234
return `myzod.string()` ;
0 commit comments