Skip to content

Commit ba0eee0

Browse files
committedMay 27, 2023
added own visitor and fixed build error for #373
·
v0.17.1v0.10.0
1 parent 4b80770 commit ba0eee0

File tree

4 files changed

+150
-133
lines changed

4 files changed

+150
-133
lines changed
 

‎src/myzod/index.ts

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,14 @@ import {
1212
UnionTypeDefinitionNode,
1313
} from 'graphql';
1414
import { DeclarationBlock, indent } from '@graphql-codegen/visitor-plugin-common';
15-
import { TsVisitor } from '@graphql-codegen/typescript';
15+
import { Visitor } from '../visitor';
1616
import { buildApi, formatDirectiveConfig } from '../directive';
1717
import { SchemaVisitor } from '../types';
1818

1919
const importZod = `import * as myzod from 'myzod'`;
2020
const anySchema = `definedNonNullAnySchema`;
2121

2222
export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchemaPluginConfig): SchemaVisitor => {
23-
const tsVisitor = new TsVisitor(schema, config);
24-
2523
const importTypes: string[] = [];
2624

2725
return {
@@ -39,12 +37,11 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
3937
].join('\n'),
4038
InputObjectTypeDefinition: {
4139
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);
4342
importTypes.push(name);
4443

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');
4845

4946
return new DeclarationBlock({})
5047
.export()
@@ -55,12 +52,11 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
5552
},
5653
ObjectTypeDefinition: {
5754
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);
5957
importTypes.push(name);
6058

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');
6460

6561
return new DeclarationBlock({})
6662
.export()
@@ -78,7 +74,8 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
7874
},
7975
EnumTypeDefinition: {
8076
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);
8279
importTypes.push(enumname);
8380
// z.enum are basically myzod.literals
8481
if (config.enumsAsTypes) {
@@ -101,11 +98,13 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
10198
leave: (node: UnionTypeDefinitionNode) => {
10299
if (!node.types || !config.withObjectType) return;
103100

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);
105104
const unionElements = node.types
106105
?.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);
109108
if (typ?.astNode?.kind === 'EnumTypeDefinition') {
110109
return `${element}Schema`;
111110
}
@@ -126,25 +125,23 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
126125

127126
const generateFieldMyZodSchema = (
128127
config: ValidationSchemaPluginConfig,
129-
tsVisitor: TsVisitor,
130-
schema: GraphQLSchema,
128+
visitor: Visitor,
131129
field: InputValueDefinitionNode | FieldDefinitionNode,
132130
indentCount: number
133131
): string => {
134-
const gen = generateFieldTypeMyZodSchema(config, tsVisitor, schema, field, field.type);
132+
const gen = generateFieldTypeMyZodSchema(config, visitor, field, field.type);
135133
return indent(`${field.name.value}: ${maybeLazy(field.type, gen)}`, indentCount);
136134
};
137135

138136
const generateFieldTypeMyZodSchema = (
139137
config: ValidationSchemaPluginConfig,
140-
tsVisitor: TsVisitor,
141-
schema: GraphQLSchema,
138+
visitor: Visitor,
142139
field: InputValueDefinitionNode | FieldDefinitionNode,
143140
type: TypeNode,
144141
parentType?: TypeNode
145142
): string => {
146143
if (isListType(type)) {
147-
const gen = generateFieldTypeMyZodSchema(config, tsVisitor, schema, field, type.type, type);
144+
const gen = generateFieldTypeMyZodSchema(config, visitor, field, type.type, type);
148145
if (!isNonNullType(parentType)) {
149146
const arrayGen = `myzod.array(${maybeLazy(type.type, gen)})`;
150147
const maybeLazyGen = applyDirectives(config, field, arrayGen);
@@ -153,18 +150,18 @@ const generateFieldTypeMyZodSchema = (
153150
return `myzod.array(${maybeLazy(type.type, gen)})`;
154151
}
155152
if (isNonNullType(type)) {
156-
const gen = generateFieldTypeMyZodSchema(config, tsVisitor, schema, field, type.type, type);
153+
const gen = generateFieldTypeMyZodSchema(config, visitor, field, type.type, type);
157154
return maybeLazy(type.type, gen);
158155
}
159156
if (isNamedType(type)) {
160-
const gen = generateNameNodeMyZodSchema(config, tsVisitor, schema, type.name);
157+
const gen = generateNameNodeMyZodSchema(config, visitor, type.name);
161158
if (isListType(parentType)) {
162159
return `${gen}.nullable()`;
163160
}
164161
const appliedDirectivesGen = applyDirectives(config, field, gen);
165162
if (isNonNullType(parentType)) {
166163
if (config.notAllowEmptyString === true) {
167-
const tsType = tsVisitor.scalars[type.name.value];
164+
const tsType = visitor.getScalarType(type.name.value);
168165
if (tsType === 'string') return `${gen}.min(1)`;
169166
}
170167
return appliedDirectivesGen;
@@ -192,33 +189,32 @@ const applyDirectives = (
192189

193190
const generateNameNodeMyZodSchema = (
194191
config: ValidationSchemaPluginConfig,
195-
tsVisitor: TsVisitor,
196-
schema: GraphQLSchema,
192+
visitor: Visitor,
197193
node: NameNode
198194
): string => {
199-
const typ = schema.getType(node.value);
195+
const converter = visitor.getNameNodeConverter(node);
200196

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()`;
204200
}
205201

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()`;
209205
}
210206

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`;
214210
}
215211

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()`;
219215
}
220216

221-
return myzod4Scalar(config, tsVisitor, node.value);
217+
return myzod4Scalar(config, visitor, node.value);
222218
};
223219

224220
const maybeLazy = (type: TypeNode, schema: string): string => {
@@ -228,11 +224,11 @@ const maybeLazy = (type: TypeNode, schema: string): string => {
228224
return schema;
229225
};
230226

231-
const myzod4Scalar = (config: ValidationSchemaPluginConfig, tsVisitor: TsVisitor, scalarName: string): string => {
227+
const myzod4Scalar = (config: ValidationSchemaPluginConfig, visitor: Visitor, scalarName: string): string => {
232228
if (config.scalarSchemas?.[scalarName]) {
233229
return config.scalarSchemas[scalarName];
234230
}
235-
const tsType = tsVisitor.scalars[scalarName];
231+
const tsType = visitor.getScalarType(scalarName);
236232
switch (tsType) {
237233
case 'string':
238234
return `myzod.string()`;

‎src/visitor.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { ValidationSchemaPluginConfig } from './config';
2+
import { TsVisitor } from '@graphql-codegen/typescript';
3+
import { NameNode, GraphQLSchema } from 'graphql';
4+
5+
export class Visitor extends TsVisitor {
6+
constructor(
7+
private scalarDirection: 'input' | 'output' | 'both',
8+
private schema: GraphQLSchema,
9+
config: ValidationSchemaPluginConfig
10+
) {
11+
super(schema, config);
12+
}
13+
14+
public getType(name: string) {
15+
return this.schema.getType(name);
16+
}
17+
18+
public getNameNodeConverter(node: NameNode) {
19+
const typ = this.schema.getType(node.value);
20+
const astNode = typ?.astNode;
21+
if (astNode === undefined || astNode === null) {
22+
return undefined;
23+
}
24+
return {
25+
targetKind: astNode.kind,
26+
convertName: () => this.convertName(astNode.name.value),
27+
};
28+
}
29+
30+
public getScalarType(scalarName: string): string | null {
31+
if (this.scalarDirection === 'both') {
32+
return null;
33+
}
34+
return this.scalars[scalarName][this.scalarDirection];
35+
}
36+
}

‎src/yup/index.ts

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,13 @@ import {
1212
UnionTypeDefinitionNode,
1313
} from 'graphql';
1414
import { DeclarationBlock, indent } from '@graphql-codegen/visitor-plugin-common';
15-
import { TsVisitor } from '@graphql-codegen/typescript';
15+
import { Visitor } from '../visitor';
1616
import { buildApi, formatDirectiveConfig } from '../directive';
1717
import { SchemaVisitor } from '../types';
1818

1919
const importYup = `import * as yup from 'yup'`;
2020

2121
export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchemaPluginConfig): SchemaVisitor => {
22-
const tsVisitor = new TsVisitor(schema, config);
23-
2422
const importTypes: string[] = [];
2523

2624
return {
@@ -48,12 +46,13 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
4846
},
4947
InputObjectTypeDefinition: {
5048
leave: (node: InputObjectTypeDefinitionNode) => {
51-
const name = tsVisitor.convertName(node.name.value);
49+
const visitor = new Visitor('input', schema, config);
50+
const name = visitor.convertName(node.name.value);
5251
importTypes.push(name);
5352

5453
const shape = node.fields
5554
?.map(field => {
56-
const fieldSchema = generateFieldYupSchema(config, tsVisitor, schema, field, 2);
55+
const fieldSchema = generateFieldYupSchema(config, visitor, field, 2);
5756
return isNonNullType(field.type) ? fieldSchema : `${fieldSchema}.optional()`;
5857
})
5958
.join(',\n');
@@ -67,12 +66,13 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
6766
},
6867
ObjectTypeDefinition: {
6968
leave: ObjectTypeDefinitionBuilder(config.withObjectType, (node: ObjectTypeDefinitionNode) => {
70-
const name = tsVisitor.convertName(node.name.value);
69+
const visitor = new Visitor('output', schema, config);
70+
const name = visitor.convertName(node.name.value);
7171
importTypes.push(name);
7272

7373
const shape = node.fields
7474
?.map(field => {
75-
const fieldSchema = generateFieldYupSchema(config, tsVisitor, schema, field, 2);
75+
const fieldSchema = generateFieldYupSchema(config, visitor, field, 2);
7676
return isNonNullType(field.type) ? fieldSchema : `${fieldSchema}.optional()`;
7777
})
7878
.join(',\n');
@@ -93,7 +93,8 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
9393
},
9494
EnumTypeDefinition: {
9595
leave: (node: EnumTypeDefinitionNode) => {
96-
const enumname = tsVisitor.convertName(node.name.value);
96+
const visitor = new Visitor('both', schema, config);
97+
const enumname = visitor.convertName(node.name.value);
9798
importTypes.push(enumname);
9899

99100
if (config.enumsAsTypes) {
@@ -109,7 +110,7 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
109110
const values = node.values
110111
?.map(
111112
enumOption =>
112-
`${enumname}.${tsVisitor.convertName(enumOption.name, {
113+
`${enumname}.${visitor.convertName(enumOption.name, {
113114
useTypesPrefix: false,
114115
transformUnderscore: true,
115116
})}`
@@ -125,14 +126,15 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
125126
UnionTypeDefinition: {
126127
leave: (node: UnionTypeDefinitionNode) => {
127128
if (!node.types || !config.withObjectType) return;
129+
const visitor = new Visitor('output', schema, config);
128130

129-
const unionName = tsVisitor.convertName(node.name.value);
131+
const unionName = visitor.convertName(node.name.value);
130132
importTypes.push(unionName);
131133

132134
const unionElements = node.types
133135
?.map(t => {
134-
const element = tsVisitor.convertName(t.name.value);
135-
const typ = schema.getType(t.name.value);
136+
const element = visitor.convertName(t.name.value);
137+
const typ = visitor.getType(t.name.value);
136138
if (typ?.astNode?.kind === 'EnumTypeDefinition') {
137139
return `${element}Schema`;
138140
}
@@ -172,12 +174,11 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
172174

173175
const generateFieldYupSchema = (
174176
config: ValidationSchemaPluginConfig,
175-
tsVisitor: TsVisitor,
176-
schema: GraphQLSchema,
177+
visitor: Visitor,
177178
field: InputValueDefinitionNode | FieldDefinitionNode,
178179
indentCount: number
179180
): string => {
180-
let gen = generateFieldTypeYupSchema(config, tsVisitor, schema, field.type);
181+
let gen = generateFieldTypeYupSchema(config, visitor, field.type);
181182
if (config.directives && field.directives) {
182183
const formatted = formatDirectiveConfig(config.directives);
183184
gen += buildApi(formatted, field.directives);
@@ -187,32 +188,31 @@ const generateFieldYupSchema = (
187188

188189
const generateFieldTypeYupSchema = (
189190
config: ValidationSchemaPluginConfig,
190-
tsVisitor: TsVisitor,
191-
schema: GraphQLSchema,
191+
visitor: Visitor,
192192
type: TypeNode,
193193
parentType?: TypeNode
194194
): string => {
195195
if (isListType(type)) {
196-
const gen = generateFieldTypeYupSchema(config, tsVisitor, schema, type.type, type);
196+
const gen = generateFieldTypeYupSchema(config, visitor, type.type, type);
197197
if (!isNonNullType(parentType)) {
198198
return `yup.array(${maybeLazy(type.type, gen)}).defined().nullable()`;
199199
}
200200
return `yup.array(${maybeLazy(type.type, gen)}).defined()`;
201201
}
202202
if (isNonNullType(type)) {
203-
const gen = generateFieldTypeYupSchema(config, tsVisitor, schema, type.type, type);
203+
const gen = generateFieldTypeYupSchema(config, visitor, type.type, type);
204204
return maybeLazy(type.type, gen);
205205
}
206206
if (isNamedType(type)) {
207-
const gen = generateNameNodeYupSchema(config, tsVisitor, schema, type.name);
207+
const gen = generateNameNodeYupSchema(config, visitor, type.name);
208208
if (isNonNullType(parentType)) {
209209
if (config.notAllowEmptyString === true) {
210-
const tsType = tsVisitor.scalars[type.name.value];
210+
const tsType = visitor.getScalarType(type.name.value);
211211
if (tsType === 'string') return `${gen}.required()`;
212212
}
213213
return `${gen}.nonNullable()`;
214214
}
215-
const typ = schema.getType(type.name.value);
215+
const typ = visitor.getType(type.name.value);
216216
if (typ?.astNode?.kind === 'InputObjectTypeDefinition') {
217217
return `${gen}`;
218218
}
@@ -222,35 +222,30 @@ const generateFieldTypeYupSchema = (
222222
return '';
223223
};
224224

225-
const generateNameNodeYupSchema = (
226-
config: ValidationSchemaPluginConfig,
227-
tsVisitor: TsVisitor,
228-
schema: GraphQLSchema,
229-
node: NameNode
230-
): string => {
231-
const typ = schema.getType(node.value);
225+
const generateNameNodeYupSchema = (config: ValidationSchemaPluginConfig, visitor: Visitor, node: NameNode): string => {
226+
const converter = visitor.getNameNodeConverter(node);
232227

233-
if (typ?.astNode?.kind === 'InputObjectTypeDefinition') {
234-
const enumName = tsVisitor.convertName(typ.astNode.name.value);
235-
return `${enumName}Schema()`;
228+
if (converter?.targetKind === 'InputObjectTypeDefinition') {
229+
const name = converter.convertName();
230+
return `${name}Schema()`;
236231
}
237232

238-
if (typ?.astNode?.kind === 'ObjectTypeDefinition') {
239-
const enumName = tsVisitor.convertName(typ.astNode.name.value);
240-
return `${enumName}Schema()`;
233+
if (converter?.targetKind === 'ObjectTypeDefinition') {
234+
const name = converter.convertName();
235+
return `${name}Schema()`;
241236
}
242237

243-
if (typ?.astNode?.kind === 'EnumTypeDefinition') {
244-
const enumName = tsVisitor.convertName(typ.astNode.name.value);
245-
return `${enumName}Schema`;
238+
if (converter?.targetKind === 'EnumTypeDefinition') {
239+
const name = converter.convertName();
240+
return `${name}Schema`;
246241
}
247242

248-
if (typ?.astNode?.kind === 'UnionTypeDefinition') {
249-
const enumName = tsVisitor.convertName(typ.astNode.name.value);
250-
return `${enumName}Schema()`;
243+
if (converter?.targetKind === 'UnionTypeDefinition') {
244+
const name = converter.convertName();
245+
return `${name}Schema()`;
251246
}
252247

253-
const primitive = yup4Scalar(config, tsVisitor, node.value);
248+
const primitive = yup4Scalar(config, visitor, node.value);
254249
return primitive;
255250
};
256251

@@ -262,11 +257,11 @@ const maybeLazy = (type: TypeNode, schema: string): string => {
262257
return schema;
263258
};
264259

265-
const yup4Scalar = (config: ValidationSchemaPluginConfig, tsVisitor: TsVisitor, scalarName: string): string => {
260+
const yup4Scalar = (config: ValidationSchemaPluginConfig, visitor: Visitor, scalarName: string): string => {
266261
if (config.scalarSchemas?.[scalarName]) {
267262
return `${config.scalarSchemas[scalarName]}.defined()`;
268263
}
269-
const tsType = tsVisitor.scalars[scalarName];
264+
const tsType = visitor.getScalarType(scalarName);
270265
switch (tsType) {
271266
case 'string':
272267
return `yup.string().defined()`;

‎src/zod/index.ts

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,14 @@ import {
1212
FieldDefinitionNode,
1313
} from 'graphql';
1414
import { DeclarationBlock, indent } from '@graphql-codegen/visitor-plugin-common';
15-
import { TsVisitor } from '@graphql-codegen/typescript';
15+
import { Visitor } from '../visitor';
1616
import { buildApi, formatDirectiveConfig } from '../directive';
1717
import { SchemaVisitor } from '../types';
1818

1919
const importZod = `import { z } from 'zod'`;
2020
const anySchema = `definedNonNullAnySchema`;
2121

2222
export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchemaPluginConfig): SchemaVisitor => {
23-
const tsVisitor = new TsVisitor(schema, config);
24-
2523
const importTypes: string[] = [];
2624

2725
return {
@@ -55,12 +53,11 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
5553
].join('\n'),
5654
InputObjectTypeDefinition: {
5755
leave: (node: InputObjectTypeDefinitionNode) => {
58-
const name = tsVisitor.convertName(node.name.value);
56+
const visitor = new Visitor('input', schema, config);
57+
const name = visitor.convertName(node.name.value);
5958
importTypes.push(name);
6059

61-
const shape = node.fields
62-
?.map(field => generateFieldZodSchema(config, tsVisitor, schema, field, 2))
63-
.join(',\n');
60+
const shape = node.fields?.map(field => generateFieldZodSchema(config, visitor, field, 2)).join(',\n');
6461

6562
return new DeclarationBlock({})
6663
.export()
@@ -71,12 +68,11 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
7168
},
7269
ObjectTypeDefinition: {
7370
leave: ObjectTypeDefinitionBuilder(config.withObjectType, (node: ObjectTypeDefinitionNode) => {
74-
const name = tsVisitor.convertName(node.name.value);
71+
const visitor = new Visitor('output', schema, config);
72+
const name = visitor.convertName(node.name.value);
7573
importTypes.push(name);
7674

77-
const shape = node.fields
78-
?.map(field => generateFieldZodSchema(config, tsVisitor, schema, field, 2))
79-
.join(',\n');
75+
const shape = node.fields?.map(field => generateFieldZodSchema(config, visitor, field, 2)).join(',\n');
8076

8177
return new DeclarationBlock({})
8278
.export()
@@ -94,7 +90,8 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
9490
},
9591
EnumTypeDefinition: {
9692
leave: (node: EnumTypeDefinitionNode) => {
97-
const enumname = tsVisitor.convertName(node.name.value);
93+
const visitor = new Visitor('both', schema, config);
94+
const enumname = visitor.convertName(node.name.value);
9895
importTypes.push(enumname);
9996

10097
if (config.enumsAsTypes) {
@@ -115,12 +112,12 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
115112
UnionTypeDefinition: {
116113
leave: (node: UnionTypeDefinitionNode) => {
117114
if (!node.types || !config.withObjectType) return;
118-
119-
const unionName = tsVisitor.convertName(node.name.value);
115+
const visitor = new Visitor('output', schema, config);
116+
const unionName = visitor.convertName(node.name.value);
120117
const unionElements = node.types
121118
.map(t => {
122-
const element = tsVisitor.convertName(t.name.value);
123-
const typ = schema.getType(t.name.value);
119+
const element = visitor.convertName(t.name.value);
120+
const typ = visitor.getType(t.name.value);
124121
if (typ?.astNode?.kind === 'EnumTypeDefinition') {
125122
return `${element}Schema`;
126123
}
@@ -141,25 +138,23 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
141138

142139
const generateFieldZodSchema = (
143140
config: ValidationSchemaPluginConfig,
144-
tsVisitor: TsVisitor,
145-
schema: GraphQLSchema,
141+
visitor: Visitor,
146142
field: InputValueDefinitionNode | FieldDefinitionNode,
147143
indentCount: number
148144
): string => {
149-
const gen = generateFieldTypeZodSchema(config, tsVisitor, schema, field, field.type);
145+
const gen = generateFieldTypeZodSchema(config, visitor, field, field.type);
150146
return indent(`${field.name.value}: ${maybeLazy(field.type, gen)}`, indentCount);
151147
};
152148

153149
const generateFieldTypeZodSchema = (
154150
config: ValidationSchemaPluginConfig,
155-
tsVisitor: TsVisitor,
156-
schema: GraphQLSchema,
151+
visitor: Visitor,
157152
field: InputValueDefinitionNode | FieldDefinitionNode,
158153
type: TypeNode,
159154
parentType?: TypeNode
160155
): string => {
161156
if (isListType(type)) {
162-
const gen = generateFieldTypeZodSchema(config, tsVisitor, schema, field, type.type, type);
157+
const gen = generateFieldTypeZodSchema(config, visitor, field, type.type, type);
163158
if (!isNonNullType(parentType)) {
164159
const arrayGen = `z.array(${maybeLazy(type.type, gen)})`;
165160
const maybeLazyGen = applyDirectives(config, field, arrayGen);
@@ -168,18 +163,18 @@ const generateFieldTypeZodSchema = (
168163
return `z.array(${maybeLazy(type.type, gen)})`;
169164
}
170165
if (isNonNullType(type)) {
171-
const gen = generateFieldTypeZodSchema(config, tsVisitor, schema, field, type.type, type);
166+
const gen = generateFieldTypeZodSchema(config, visitor, field, type.type, type);
172167
return maybeLazy(type.type, gen);
173168
}
174169
if (isNamedType(type)) {
175-
const gen = generateNameNodeZodSchema(config, tsVisitor, schema, type.name);
170+
const gen = generateNameNodeZodSchema(config, visitor, type.name);
176171
if (isListType(parentType)) {
177172
return `${gen}.nullable()`;
178173
}
179174
const appliedDirectivesGen = applyDirectives(config, field, gen);
180175
if (isNonNullType(parentType)) {
181176
if (config.notAllowEmptyString === true) {
182-
const tsType = tsVisitor.scalars[type.name.value];
177+
const tsType = visitor.getScalarType(type.name.value);
183178
if (tsType === 'string') return `${appliedDirectivesGen}.min(1)`;
184179
}
185180
return appliedDirectivesGen;
@@ -205,35 +200,30 @@ const applyDirectives = (
205200
return gen;
206201
};
207202

208-
const generateNameNodeZodSchema = (
209-
config: ValidationSchemaPluginConfig,
210-
tsVisitor: TsVisitor,
211-
schema: GraphQLSchema,
212-
node: NameNode
213-
): string => {
214-
const typ = schema.getType(node.value);
203+
const generateNameNodeZodSchema = (config: ValidationSchemaPluginConfig, visitor: Visitor, node: NameNode): string => {
204+
const converter = visitor.getNameNodeConverter(node);
215205

216-
if (typ?.astNode?.kind === 'InputObjectTypeDefinition') {
217-
const enumName = tsVisitor.convertName(typ.astNode.name.value);
218-
return `${enumName}Schema()`;
206+
if (converter?.targetKind === 'InputObjectTypeDefinition') {
207+
const name = converter.convertName();
208+
return `${name}Schema()`;
219209
}
220210

221-
if (typ?.astNode?.kind === 'ObjectTypeDefinition') {
222-
const enumName = tsVisitor.convertName(typ.astNode.name.value);
223-
return `${enumName}Schema()`;
211+
if (converter?.targetKind === 'ObjectTypeDefinition') {
212+
const name = converter.convertName();
213+
return `${name}Schema()`;
224214
}
225215

226-
if (typ?.astNode?.kind === 'EnumTypeDefinition') {
227-
const enumName = tsVisitor.convertName(typ.astNode.name.value);
228-
return `${enumName}Schema`;
216+
if (converter?.targetKind === 'EnumTypeDefinition') {
217+
const name = converter.convertName();
218+
return `${name}Schema`;
229219
}
230220

231-
if (typ?.astNode?.kind === 'UnionTypeDefinition') {
232-
const enumName = tsVisitor.convertName(typ.astNode.name.value);
233-
return `${enumName}Schema()`;
221+
if (converter?.targetKind === 'UnionTypeDefinition') {
222+
const name = converter.convertName();
223+
return `${name}Schema()`;
234224
}
235225

236-
return zod4Scalar(config, tsVisitor, node.value);
226+
return zod4Scalar(config, visitor, node.value);
237227
};
238228

239229
const maybeLazy = (type: TypeNode, schema: string): string => {
@@ -243,11 +233,11 @@ const maybeLazy = (type: TypeNode, schema: string): string => {
243233
return schema;
244234
};
245235

246-
const zod4Scalar = (config: ValidationSchemaPluginConfig, tsVisitor: TsVisitor, scalarName: string): string => {
236+
const zod4Scalar = (config: ValidationSchemaPluginConfig, visitor: Visitor, scalarName: string): string => {
247237
if (config.scalarSchemas?.[scalarName]) {
248238
return config.scalarSchemas[scalarName];
249239
}
250-
const tsType = tsVisitor.scalars[scalarName];
240+
const tsType = visitor.getScalarType(scalarName);
251241
switch (tsType) {
252242
case 'string':
253243
return `z.string()`;

0 commit comments

Comments
 (0)
Please sign in to comment.