@@ -14,10 +14,11 @@ import {
14
14
import { DeclarationBlock , indent } from '@graphql-codegen/visitor-plugin-common' ;
15
15
import { TsVisitor } from '@graphql-codegen/typescript' ;
16
16
import { buildApi , formatDirectiveConfig } from '../directive' ;
17
+ import { SchemaVisitor } from '../types' ;
17
18
18
19
const importYup = `import * as yup from 'yup'` ;
19
20
20
- export const YupSchemaVisitor = ( schema : GraphQLSchema , config : ValidationSchemaPluginConfig ) => {
21
+ export const YupSchemaVisitor = ( schema : GraphQLSchema , config : ValidationSchemaPluginConfig ) : SchemaVisitor => {
21
22
const tsVisitor = new TsVisitor ( schema , config ) ;
22
23
23
24
const importTypes : string [ ] = [ ] ;
@@ -45,80 +46,92 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
45
46
) . string
46
47
) ;
47
48
} ,
48
- InputObjectTypeDefinition : ( node : InputObjectTypeDefinitionNode ) => {
49
- const name = tsVisitor . convertName ( node . name . value ) ;
50
- importTypes . push ( name ) ;
49
+ InputObjectTypeDefinition : {
50
+ leave : ( node : InputObjectTypeDefinitionNode ) => {
51
+ const name = tsVisitor . convertName ( node . name . value ) ;
52
+ importTypes . push ( name ) ;
51
53
52
- const shape = node . fields ?. map ( field => generateFieldYupSchema ( config , tsVisitor , schema , field , 2 ) ) . join ( ',\n' ) ;
54
+ const shape = node . fields
55
+ ?. map ( field => generateFieldYupSchema ( config , tsVisitor , schema , field , 2 ) )
56
+ . join ( ',\n' ) ;
53
57
54
- return new DeclarationBlock ( { } )
55
- . export ( )
56
- . asKind ( 'function' )
57
- . withName ( `${ name } Schema(): yup.SchemaOf<${ name } >` )
58
- . withBlock ( [ indent ( `return yup.object({` ) , shape , indent ( '})' ) ] . join ( '\n' ) ) . string ;
58
+ return new DeclarationBlock ( { } )
59
+ . export ( )
60
+ . asKind ( 'function' )
61
+ . withName ( `${ name } Schema(): yup.SchemaOf<${ name } >` )
62
+ . withBlock ( [ indent ( `return yup.object({` ) , shape , indent ( '})' ) ] . join ( '\n' ) ) . string ;
63
+ } ,
59
64
} ,
60
- ObjectTypeDefinition : ObjectTypeDefinitionBuilder ( config . withObjectType , ( node : ObjectTypeDefinitionNode ) => {
61
- const name = tsVisitor . convertName ( node . name . value ) ;
62
- importTypes . push ( name ) ;
65
+ ObjectTypeDefinition : {
66
+ leave : ObjectTypeDefinitionBuilder ( config . withObjectType , ( node : ObjectTypeDefinitionNode ) => {
67
+ const name = tsVisitor . convertName ( node . name . value ) ;
68
+ importTypes . push ( name ) ;
63
69
64
- const shape = node . fields ?. map ( field => generateFieldYupSchema ( config , tsVisitor , schema , field , 2 ) ) . join ( ',\n' ) ;
70
+ const shape = node . fields
71
+ ?. map ( field => generateFieldYupSchema ( config , tsVisitor , schema , field , 2 ) )
72
+ . join ( ',\n' ) ;
65
73
66
- return new DeclarationBlock ( { } )
67
- . export ( )
68
- . asKind ( 'function' )
69
- . withName ( `${ name } Schema(): yup.SchemaOf<${ name } >` )
70
- . withBlock (
71
- [
72
- indent ( `return yup.object({` ) ,
73
- indent ( `__typename: yup.mixed().oneOf(['${ node . name . value } ', undefined]),` , 2 ) ,
74
- shape ,
75
- indent ( '})' ) ,
76
- ] . join ( '\n' )
77
- ) . string ;
78
- } ) ,
79
- EnumTypeDefinition : ( node : EnumTypeDefinitionNode ) => {
80
- const enumname = tsVisitor . convertName ( node . name . value ) ;
81
- importTypes . push ( enumname ) ;
74
+ return new DeclarationBlock ( { } )
75
+ . export ( )
76
+ . asKind ( 'function' )
77
+ . withName ( `${ name } Schema(): yup.SchemaOf<${ name } >` )
78
+ . withBlock (
79
+ [
80
+ indent ( `return yup.object({` ) ,
81
+ indent ( `__typename: yup.mixed().oneOf(['${ node . name . value } ', undefined]),` , 2 ) ,
82
+ shape ,
83
+ indent ( '})' ) ,
84
+ ] . join ( '\n' )
85
+ ) . string ;
86
+ } ) ,
87
+ } ,
88
+ EnumTypeDefinition : {
89
+ leave : ( node : EnumTypeDefinitionNode ) => {
90
+ const enumname = tsVisitor . convertName ( node . name . value ) ;
91
+ importTypes . push ( enumname ) ;
92
+
93
+ if ( config . enumsAsTypes ) {
94
+ return new DeclarationBlock ( { } )
95
+ . export ( )
96
+ . asKind ( 'const' )
97
+ . withName ( `${ enumname } Schema` )
98
+ . withContent (
99
+ `yup.mixed().oneOf([${ node . values ?. map ( enumOption => `'${ enumOption . name . value } '` ) . join ( ', ' ) } ])`
100
+ ) . string ;
101
+ }
82
102
83
- if ( config . enumsAsTypes ) {
103
+ const values = node . values
104
+ ?. map (
105
+ enumOption =>
106
+ `${ enumname } .${ tsVisitor . convertName ( enumOption . name , {
107
+ useTypesPrefix : false ,
108
+ transformUnderscore : true ,
109
+ } ) } `
110
+ )
111
+ . join ( ', ' ) ;
84
112
return new DeclarationBlock ( { } )
85
113
. export ( )
86
114
. asKind ( 'const' )
87
115
. withName ( `${ enumname } Schema` )
88
- . withContent (
89
- `yup.mixed().oneOf([${ node . values ?. map ( enumOption => `'${ enumOption . name . value } '` ) . join ( ', ' ) } ])`
90
- ) . string ;
91
- }
92
-
93
- const values = node . values
94
- ?. map (
95
- enumOption =>
96
- `${ enumname } .${ tsVisitor . convertName ( enumOption . name , {
97
- useTypesPrefix : false ,
98
- transformUnderscore : true ,
99
- } ) } `
100
- )
101
- . join ( ', ' ) ;
102
- return new DeclarationBlock ( { } )
103
- . export ( )
104
- . asKind ( 'const' )
105
- . withName ( `${ enumname } Schema` )
106
- . withContent ( `yup.mixed().oneOf([${ values } ])` ) . string ;
116
+ . withContent ( `yup.mixed().oneOf([${ values } ])` ) . string ;
117
+ } ,
107
118
} ,
108
- UnionTypeDefinition : ( node : UnionTypeDefinitionNode ) => {
109
- if ( ! node . types || ! config . withObjectType ) return ;
119
+ UnionTypeDefinition : {
120
+ leave : ( node : UnionTypeDefinitionNode ) => {
121
+ if ( ! node . types || ! config . withObjectType ) return ;
110
122
111
- const unionName = tsVisitor . convertName ( node . name . value ) ;
112
- importTypes . push ( unionName ) ;
123
+ const unionName = tsVisitor . convertName ( node . name . value ) ;
124
+ importTypes . push ( unionName ) ;
113
125
114
- const unionElements = node . types ?. map ( t => `${ tsVisitor . convertName ( t . name . value ) } Schema()` ) . join ( ', ' ) ;
115
- const union = indent ( `return union<${ unionName } >(${ unionElements } )` ) ;
126
+ const unionElements = node . types ?. map ( t => `${ tsVisitor . convertName ( t . name . value ) } Schema()` ) . join ( ', ' ) ;
127
+ const union = indent ( `return union<${ unionName } >(${ unionElements } )` ) ;
116
128
117
- return new DeclarationBlock ( { } )
118
- . export ( )
119
- . asKind ( 'function' )
120
- . withName ( `${ unionName } Schema(): yup.BaseSchema<${ unionName } >` )
121
- . withBlock ( union ) . string ;
129
+ return new DeclarationBlock ( { } )
130
+ . export ( )
131
+ . asKind ( 'function' )
132
+ . withName ( `${ unionName } Schema(): yup.BaseSchema<${ unionName } >` )
133
+ . withBlock ( union ) . string ;
134
+ } ,
122
135
} ,
123
136
// ScalarTypeDefinition: (node) => {
124
137
// const decl = new DeclarationBlock({})
0 commit comments