@@ -10,46 +10,45 @@ import {
10
10
TypeNode ,
11
11
UnionTypeDefinitionNode ,
12
12
} from 'graphql' ;
13
+ import { BaseSchemaVisitor } from 'src/schema_visitor' ;
13
14
14
15
import { ValidationSchemaPluginConfig } from '../config' ;
15
16
import { buildApi , formatDirectiveConfig } from '../directive' ;
16
- import { SchemaVisitor } from '../types' ;
17
17
import { Visitor } from '../visitor' ;
18
18
import { isInput , isListType , isNamedType , isNonNullType , ObjectTypeDefinitionBuilder } from './../graphql' ;
19
19
20
- const importZod = `import * as myzod from 'myzod'` ;
21
20
const anySchema = `definedNonNullAnySchema` ;
22
21
23
- export const MyZodSchemaVisitor = ( schema : GraphQLSchema , config : ValidationSchemaPluginConfig ) : SchemaVisitor => {
24
- const importTypes : string [ ] = [ ] ;
25
- const enumDeclarations : string [ ] = [ ] ;
22
+ export class MyZodSchemaVisitor extends BaseSchemaVisitor {
23
+ constructor ( schema : GraphQLSchema , config : ValidationSchemaPluginConfig ) {
24
+ super ( schema , config ) ;
25
+ }
26
26
27
- return {
28
- buildImports : ( ) : string [ ] => {
29
- if ( config . importFrom && importTypes . length > 0 ) {
30
- return [
31
- importZod ,
32
- `import ${ config . useTypeImports ? 'type ' : '' } { ${ importTypes . join ( ', ' ) } } from '${ config . importFrom } '` ,
33
- ] ;
34
- }
35
- return [ importZod ] ;
36
- } ,
37
- initialEmit : ( ) : string =>
27
+ importValidationSchema ( ) : string {
28
+ return `import * as myzod from 'myzod'` ;
29
+ }
30
+
31
+ initialEmit ( ) : string {
32
+ return (
38
33
'\n' +
39
34
[
40
35
new DeclarationBlock ( { } ) . export ( ) . asKind ( 'const' ) . withName ( `${ anySchema } ` ) . withContent ( `myzod.object({})` )
41
36
. string ,
42
- ...enumDeclarations ,
43
- ] . join ( '\n' ) ,
44
- InputObjectTypeDefinition : {
37
+ ...this . enumDeclarations ,
38
+ ] . join ( '\n' )
39
+ ) ;
40
+ }
41
+
42
+ get InputObjectTypeDefinition ( ) {
43
+ return {
45
44
leave : ( node : InputObjectTypeDefinitionNode ) => {
46
- const visitor = new Visitor ( 'input' , schema , config ) ;
45
+ const visitor = this . createVisitor ( 'input' ) ;
47
46
const name = visitor . convertName ( node . name . value ) ;
48
- importTypes . push ( name ) ;
47
+ this . importTypes . push ( name ) ;
49
48
50
- const shape = node . fields ?. map ( field => generateFieldMyZodSchema ( config , visitor , field , 2 ) ) . join ( ',\n' ) ;
49
+ const shape = node . fields ?. map ( field => generateFieldMyZodSchema ( this . config , visitor , field , 2 ) ) . join ( ',\n' ) ;
51
50
52
- switch ( config . validationSchemaExportType ) {
51
+ switch ( this . config . validationSchemaExportType ) {
53
52
case 'const' :
54
53
return new DeclarationBlock ( { } )
55
54
. export ( )
@@ -66,19 +65,22 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
66
65
. withBlock ( [ indent ( `return myzod.object({` ) , shape , indent ( '})' ) ] . join ( '\n' ) ) . string ;
67
66
}
68
67
} ,
69
- } ,
70
- ObjectTypeDefinition : {
71
- leave : ObjectTypeDefinitionBuilder ( config . withObjectType , ( node : ObjectTypeDefinitionNode ) => {
72
- const visitor = new Visitor ( 'output' , schema , config ) ;
68
+ } ;
69
+ }
70
+
71
+ get ObjectTypeDefinition ( ) {
72
+ return {
73
+ leave : ObjectTypeDefinitionBuilder ( this . config . withObjectType , ( node : ObjectTypeDefinitionNode ) => {
74
+ const visitor = this . createVisitor ( 'output' ) ;
73
75
const name = visitor . convertName ( node . name . value ) ;
74
- importTypes . push ( name ) ;
76
+ this . importTypes . push ( name ) ;
75
77
76
78
// Building schema for field arguments.
77
79
const argumentBlocks = visitor . buildArgumentsSchemaBlock ( node , ( typeName , field ) => {
78
- importTypes . push ( typeName ) ;
80
+ this . importTypes . push ( typeName ) ;
79
81
const args = field . arguments ?? [ ] ;
80
- const shape = args . map ( field => generateFieldMyZodSchema ( config , visitor , field , 2 ) ) . join ( ',\n' ) ;
81
- switch ( config . validationSchemaExportType ) {
82
+ const shape = args . map ( field => generateFieldMyZodSchema ( this . config , visitor , field , 2 ) ) . join ( ',\n' ) ;
83
+ switch ( this . config . validationSchemaExportType ) {
82
84
case 'const' :
83
85
return new DeclarationBlock ( { } )
84
86
. export ( )
@@ -98,9 +100,9 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
98
100
const appendArguments = argumentBlocks ? '\n' + argumentBlocks : '' ;
99
101
100
102
// Building schema for fields.
101
- const shape = node . fields ?. map ( field => generateFieldMyZodSchema ( config , visitor , field , 2 ) ) . join ( ',\n' ) ;
103
+ const shape = node . fields ?. map ( field => generateFieldMyZodSchema ( this . config , visitor , field , 2 ) ) . join ( ',\n' ) ;
102
104
103
- switch ( config . validationSchemaExportType ) {
105
+ switch ( this . config . validationSchemaExportType ) {
104
106
case 'const' :
105
107
return (
106
108
new DeclarationBlock ( { } )
@@ -135,16 +137,19 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
135
137
) ;
136
138
}
137
139
} ) ,
138
- } ,
139
- EnumTypeDefinition : {
140
+ } ;
141
+ }
142
+
143
+ get EnumTypeDefinition ( ) {
144
+ return {
140
145
leave : ( node : EnumTypeDefinitionNode ) => {
141
- const visitor = new Visitor ( 'both' , schema , config ) ;
146
+ const visitor = this . createVisitor ( 'both' ) ;
142
147
const enumname = visitor . convertName ( node . name . value ) ;
143
- importTypes . push ( enumname ) ;
148
+ this . importTypes . push ( enumname ) ;
144
149
// z.enum are basically myzod.literals
145
150
// hoist enum declarations
146
- enumDeclarations . push (
147
- config . enumsAsTypes
151
+ this . enumDeclarations . push (
152
+ this . config . enumsAsTypes
148
153
? new DeclarationBlock ( { } )
149
154
. export ( )
150
155
. asKind ( 'type' )
@@ -159,12 +164,15 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
159
164
. withContent ( `myzod.enum(${ enumname } )` ) . string
160
165
) ;
161
166
} ,
162
- } ,
163
- UnionTypeDefinition : {
167
+ } ;
168
+ }
169
+
170
+ get UnionTypeDefinition ( ) {
171
+ return {
164
172
leave : ( node : UnionTypeDefinitionNode ) => {
165
- if ( ! node . types || ! config . withObjectType ) return ;
173
+ if ( ! node . types || ! this . config . withObjectType ) return ;
166
174
167
- const visitor = new Visitor ( 'output' , schema , config ) ;
175
+ const visitor = this . createVisitor ( 'output' ) ;
168
176
169
177
const unionName = visitor . convertName ( node . name . value ) ;
170
178
const unionElements = node . types
@@ -174,7 +182,7 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
174
182
if ( typ ?. astNode ?. kind === 'EnumTypeDefinition' ) {
175
183
return `${ element } Schema` ;
176
184
}
177
- switch ( config . validationSchemaExportType ) {
185
+ switch ( this . config . validationSchemaExportType ) {
178
186
case 'const' :
179
187
return `${ element } Schema` ;
180
188
case 'function' :
@@ -187,7 +195,7 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
187
195
188
196
const union = unionElementsCount > 1 ? `myzod.union([${ unionElements } ])` : unionElements ;
189
197
190
- switch ( config . validationSchemaExportType ) {
198
+ switch ( this . config . validationSchemaExportType ) {
191
199
case 'const' :
192
200
return new DeclarationBlock ( { } ) . export ( ) . asKind ( 'const' ) . withName ( `${ unionName } Schema` ) . withContent ( union )
193
201
. string ;
@@ -200,9 +208,9 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
200
208
. withBlock ( indent ( `return ${ union } ` ) ) . string ;
201
209
}
202
210
} ,
203
- } ,
204
- } ;
205
- } ;
211
+ } ;
212
+ }
213
+ }
206
214
207
215
const generateFieldMyZodSchema = (
208
216
config : ValidationSchemaPluginConfig ,
0 commit comments