@@ -12,15 +12,13 @@ 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 importYup = `import * as yup from 'yup'` ;
20
20
21
21
export const YupSchemaVisitor = ( schema : GraphQLSchema , config : ValidationSchemaPluginConfig ) : SchemaVisitor => {
22
- const tsVisitor = new TsVisitor ( schema , config ) ;
23
-
24
22
const importTypes : string [ ] = [ ] ;
25
23
26
24
return {
@@ -48,12 +46,13 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
48
46
} ,
49
47
InputObjectTypeDefinition : {
50
48
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 ) ;
52
51
importTypes . push ( name ) ;
53
52
54
53
const shape = node . fields
55
54
?. map ( field => {
56
- const fieldSchema = generateFieldYupSchema ( config , tsVisitor , schema , field , 2 ) ;
55
+ const fieldSchema = generateFieldYupSchema ( config , visitor , field , 2 ) ;
57
56
return isNonNullType ( field . type ) ? fieldSchema : `${ fieldSchema } .optional()` ;
58
57
} )
59
58
. join ( ',\n' ) ;
@@ -67,12 +66,13 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
67
66
} ,
68
67
ObjectTypeDefinition : {
69
68
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 ) ;
71
71
importTypes . push ( name ) ;
72
72
73
73
const shape = node . fields
74
74
?. map ( field => {
75
- const fieldSchema = generateFieldYupSchema ( config , tsVisitor , schema , field , 2 ) ;
75
+ const fieldSchema = generateFieldYupSchema ( config , visitor , field , 2 ) ;
76
76
return isNonNullType ( field . type ) ? fieldSchema : `${ fieldSchema } .optional()` ;
77
77
} )
78
78
. join ( ',\n' ) ;
@@ -93,7 +93,8 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
93
93
} ,
94
94
EnumTypeDefinition : {
95
95
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 ) ;
97
98
importTypes . push ( enumname ) ;
98
99
99
100
if ( config . enumsAsTypes ) {
@@ -109,7 +110,7 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
109
110
const values = node . values
110
111
?. map (
111
112
enumOption =>
112
- `${ enumname } .${ tsVisitor . convertName ( enumOption . name , {
113
+ `${ enumname } .${ visitor . convertName ( enumOption . name , {
113
114
useTypesPrefix : false ,
114
115
transformUnderscore : true ,
115
116
} ) } `
@@ -125,14 +126,15 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
125
126
UnionTypeDefinition : {
126
127
leave : ( node : UnionTypeDefinitionNode ) => {
127
128
if ( ! node . types || ! config . withObjectType ) return ;
129
+ const visitor = new Visitor ( 'output' , schema , config ) ;
128
130
129
- const unionName = tsVisitor . convertName ( node . name . value ) ;
131
+ const unionName = visitor . convertName ( node . name . value ) ;
130
132
importTypes . push ( unionName ) ;
131
133
132
134
const unionElements = node . types
133
135
?. 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 ) ;
136
138
if ( typ ?. astNode ?. kind === 'EnumTypeDefinition' ) {
137
139
return `${ element } Schema` ;
138
140
}
@@ -172,12 +174,11 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
172
174
173
175
const generateFieldYupSchema = (
174
176
config : ValidationSchemaPluginConfig ,
175
- tsVisitor : TsVisitor ,
176
- schema : GraphQLSchema ,
177
+ visitor : Visitor ,
177
178
field : InputValueDefinitionNode | FieldDefinitionNode ,
178
179
indentCount : number
179
180
) : string => {
180
- let gen = generateFieldTypeYupSchema ( config , tsVisitor , schema , field . type ) ;
181
+ let gen = generateFieldTypeYupSchema ( config , visitor , field . type ) ;
181
182
if ( config . directives && field . directives ) {
182
183
const formatted = formatDirectiveConfig ( config . directives ) ;
183
184
gen += buildApi ( formatted , field . directives ) ;
@@ -187,32 +188,31 @@ const generateFieldYupSchema = (
187
188
188
189
const generateFieldTypeYupSchema = (
189
190
config : ValidationSchemaPluginConfig ,
190
- tsVisitor : TsVisitor ,
191
- schema : GraphQLSchema ,
191
+ visitor : Visitor ,
192
192
type : TypeNode ,
193
193
parentType ?: TypeNode
194
194
) : string => {
195
195
if ( isListType ( type ) ) {
196
- const gen = generateFieldTypeYupSchema ( config , tsVisitor , schema , type . type , type ) ;
196
+ const gen = generateFieldTypeYupSchema ( config , visitor , type . type , type ) ;
197
197
if ( ! isNonNullType ( parentType ) ) {
198
198
return `yup.array(${ maybeLazy ( type . type , gen ) } ).defined().nullable()` ;
199
199
}
200
200
return `yup.array(${ maybeLazy ( type . type , gen ) } ).defined()` ;
201
201
}
202
202
if ( isNonNullType ( type ) ) {
203
- const gen = generateFieldTypeYupSchema ( config , tsVisitor , schema , type . type , type ) ;
203
+ const gen = generateFieldTypeYupSchema ( config , visitor , type . type , type ) ;
204
204
return maybeLazy ( type . type , gen ) ;
205
205
}
206
206
if ( isNamedType ( type ) ) {
207
- const gen = generateNameNodeYupSchema ( config , tsVisitor , schema , type . name ) ;
207
+ const gen = generateNameNodeYupSchema ( config , visitor , type . name ) ;
208
208
if ( isNonNullType ( parentType ) ) {
209
209
if ( config . notAllowEmptyString === true ) {
210
- const tsType = tsVisitor . scalars [ type . name . value ] ;
210
+ const tsType = visitor . getScalarType ( type . name . value ) ;
211
211
if ( tsType === 'string' ) return `${ gen } .required()` ;
212
212
}
213
213
return `${ gen } .nonNullable()` ;
214
214
}
215
- const typ = schema . getType ( type . name . value ) ;
215
+ const typ = visitor . getType ( type . name . value ) ;
216
216
if ( typ ?. astNode ?. kind === 'InputObjectTypeDefinition' ) {
217
217
return `${ gen } ` ;
218
218
}
@@ -222,35 +222,30 @@ const generateFieldTypeYupSchema = (
222
222
return '' ;
223
223
} ;
224
224
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 ) ;
232
227
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()` ;
236
231
}
237
232
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()` ;
241
236
}
242
237
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` ;
246
241
}
247
242
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()` ;
251
246
}
252
247
253
- const primitive = yup4Scalar ( config , tsVisitor , node . value ) ;
248
+ const primitive = yup4Scalar ( config , visitor , node . value ) ;
254
249
return primitive ;
255
250
} ;
256
251
@@ -262,11 +257,11 @@ const maybeLazy = (type: TypeNode, schema: string): string => {
262
257
return schema ;
263
258
} ;
264
259
265
- const yup4Scalar = ( config : ValidationSchemaPluginConfig , tsVisitor : TsVisitor , scalarName : string ) : string => {
260
+ const yup4Scalar = ( config : ValidationSchemaPluginConfig , visitor : Visitor , scalarName : string ) : string => {
266
261
if ( config . scalarSchemas ?. [ scalarName ] ) {
267
262
return `${ config . scalarSchemas [ scalarName ] } .defined()` ;
268
263
}
269
- const tsType = tsVisitor . scalars [ scalarName ] ;
264
+ const tsType = visitor . getScalarType ( scalarName ) ;
270
265
switch ( tsType ) {
271
266
case 'string' :
272
267
return `yup.string().defined()` ;
0 commit comments