@@ -10,46 +10,45 @@ import {
1010 TypeNode ,
1111 UnionTypeDefinitionNode ,
1212} from 'graphql' ;
13+ import { BaseSchemaVisitor } from 'src/schema_visitor' ;
1314
1415import { ValidationSchemaPluginConfig } from '../config' ;
1516import { buildApi , formatDirectiveConfig } from '../directive' ;
16- import { SchemaVisitor } from '../types' ;
1717import { Visitor } from '../visitor' ;
1818import { isInput , isListType , isNamedType , isNonNullType , ObjectTypeDefinitionBuilder } from './../graphql' ;
1919
20- const importZod = `import * as myzod from 'myzod'` ;
2120const anySchema = `definedNonNullAnySchema` ;
2221
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+ }
2626
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 (
3833 '\n' +
3934 [
4035 new DeclarationBlock ( { } ) . export ( ) . asKind ( 'const' ) . withName ( `${ anySchema } ` ) . withContent ( `myzod.object({})` )
4136 . string ,
42- ...enumDeclarations ,
43- ] . join ( '\n' ) ,
44- InputObjectTypeDefinition : {
37+ ...this . enumDeclarations ,
38+ ] . join ( '\n' )
39+ ) ;
40+ }
41+
42+ get InputObjectTypeDefinition ( ) {
43+ return {
4544 leave : ( node : InputObjectTypeDefinitionNode ) => {
46- const visitor = new Visitor ( 'input' , schema , config ) ;
45+ const visitor = this . createVisitor ( 'input' ) ;
4746 const name = visitor . convertName ( node . name . value ) ;
48- importTypes . push ( name ) ;
47+ this . importTypes . push ( name ) ;
4948
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' ) ;
5150
52- switch ( config . validationSchemaExportType ) {
51+ switch ( this . config . validationSchemaExportType ) {
5352 case 'const' :
5453 return new DeclarationBlock ( { } )
5554 . export ( )
@@ -66,19 +65,22 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
6665 . withBlock ( [ indent ( `return myzod.object({` ) , shape , indent ( '})' ) ] . join ( '\n' ) ) . string ;
6766 }
6867 } ,
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' ) ;
7375 const name = visitor . convertName ( node . name . value ) ;
74- importTypes . push ( name ) ;
76+ this . importTypes . push ( name ) ;
7577
7678 // Building schema for field arguments.
7779 const argumentBlocks = visitor . buildArgumentsSchemaBlock ( node , ( typeName , field ) => {
78- importTypes . push ( typeName ) ;
80+ this . importTypes . push ( typeName ) ;
7981 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 ) {
8284 case 'const' :
8385 return new DeclarationBlock ( { } )
8486 . export ( )
@@ -98,9 +100,9 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
98100 const appendArguments = argumentBlocks ? '\n' + argumentBlocks : '' ;
99101
100102 // 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' ) ;
102104
103- switch ( config . validationSchemaExportType ) {
105+ switch ( this . config . validationSchemaExportType ) {
104106 case 'const' :
105107 return (
106108 new DeclarationBlock ( { } )
@@ -135,16 +137,19 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
135137 ) ;
136138 }
137139 } ) ,
138- } ,
139- EnumTypeDefinition : {
140+ } ;
141+ }
142+
143+ get EnumTypeDefinition ( ) {
144+ return {
140145 leave : ( node : EnumTypeDefinitionNode ) => {
141- const visitor = new Visitor ( 'both' , schema , config ) ;
146+ const visitor = this . createVisitor ( 'both' ) ;
142147 const enumname = visitor . convertName ( node . name . value ) ;
143- importTypes . push ( enumname ) ;
148+ this . importTypes . push ( enumname ) ;
144149 // z.enum are basically myzod.literals
145150 // hoist enum declarations
146- enumDeclarations . push (
147- config . enumsAsTypes
151+ this . enumDeclarations . push (
152+ this . config . enumsAsTypes
148153 ? new DeclarationBlock ( { } )
149154 . export ( )
150155 . asKind ( 'type' )
@@ -159,12 +164,15 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
159164 . withContent ( `myzod.enum(${ enumname } )` ) . string
160165 ) ;
161166 } ,
162- } ,
163- UnionTypeDefinition : {
167+ } ;
168+ }
169+
170+ get UnionTypeDefinition ( ) {
171+ return {
164172 leave : ( node : UnionTypeDefinitionNode ) => {
165- if ( ! node . types || ! config . withObjectType ) return ;
173+ if ( ! node . types || ! this . config . withObjectType ) return ;
166174
167- const visitor = new Visitor ( 'output' , schema , config ) ;
175+ const visitor = this . createVisitor ( 'output' ) ;
168176
169177 const unionName = visitor . convertName ( node . name . value ) ;
170178 const unionElements = node . types
@@ -174,7 +182,7 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
174182 if ( typ ?. astNode ?. kind === 'EnumTypeDefinition' ) {
175183 return `${ element } Schema` ;
176184 }
177- switch ( config . validationSchemaExportType ) {
185+ switch ( this . config . validationSchemaExportType ) {
178186 case 'const' :
179187 return `${ element } Schema` ;
180188 case 'function' :
@@ -187,7 +195,7 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
187195
188196 const union = unionElementsCount > 1 ? `myzod.union([${ unionElements } ])` : unionElements ;
189197
190- switch ( config . validationSchemaExportType ) {
198+ switch ( this . config . validationSchemaExportType ) {
191199 case 'const' :
192200 return new DeclarationBlock ( { } ) . export ( ) . asKind ( 'const' ) . withName ( `${ unionName } Schema` ) . withContent ( union )
193201 . string ;
@@ -200,9 +208,9 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
200208 . withBlock ( indent ( `return ${ union } ` ) ) . string ;
201209 }
202210 } ,
203- } ,
204- } ;
205- } ;
211+ } ;
212+ }
213+ }
206214
207215const generateFieldMyZodSchema = (
208216 config : ValidationSchemaPluginConfig ,
0 commit comments