66 TypeNode ,
77 GraphQLSchema ,
88 InputObjectTypeDefinitionNode ,
9+ ObjectTypeDefinitionNode ,
910 EnumTypeDefinitionNode ,
11+ FieldDefinitionNode ,
1012} from 'graphql' ;
1113import { DeclarationBlock , indent } from '@graphql-codegen/visitor-plugin-common' ;
1214import { TsVisitor } from '@graphql-codegen/typescript' ;
@@ -38,7 +40,7 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
3840 importTypes . push ( name ) ;
3941
4042 const shape = node . fields
41- ?. map ( field => generateInputObjectFieldMyZodSchema ( config , tsVisitor , schema , field , 2 ) )
43+ ?. map ( field => generateFieldMyZodSchema ( config , tsVisitor , schema , field , 2 ) )
4244 . join ( ',\n' ) ;
4345
4446 return new DeclarationBlock ( { } )
@@ -47,6 +49,27 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
4749 . withName ( `${ name } Schema(): myzod.Type<${ name } >` )
4850 . withBlock ( [ indent ( `return myzod.object({` ) , shape , indent ( '})' ) ] . join ( '\n' ) ) . string ;
4951 } ,
52+ ObjectTypeDefinition : ( node : ObjectTypeDefinitionNode ) => {
53+ if ( ! config . useObjectTypes ) return ;
54+ const name = tsVisitor . convertName ( node . name . value ) ;
55+ importTypes . push ( name ) ;
56+
57+ const shape = node . fields
58+ ?. map ( field => generateFieldMyZodSchema ( config , tsVisitor , schema , field , 2 ) )
59+ . join ( ',\n' ) ;
60+
61+ return new DeclarationBlock ( { } )
62+ . export ( )
63+ . asKind ( 'function' )
64+ . withName ( `${ name } Schema(): myzod.Type<${ name } >` )
65+ . withBlock (
66+ [
67+ indent ( `return myzod.object({` ) ,
68+ ` __typename: myzod.literal('${ node . name . value } ').optional(),\n${ shape } ` ,
69+ indent ( '})' ) ,
70+ ] . join ( '\n' )
71+ ) . string ;
72+ } ,
5073 EnumTypeDefinition : ( node : EnumTypeDefinitionNode ) => {
5174 const enumname = tsVisitor . convertName ( node . name . value ) ;
5275 importTypes . push ( enumname ) ;
@@ -69,27 +92,27 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
6992 } ;
7093} ;
7194
72- const generateInputObjectFieldMyZodSchema = (
95+ const generateFieldMyZodSchema = (
7396 config : ValidationSchemaPluginConfig ,
7497 tsVisitor : TsVisitor ,
7598 schema : GraphQLSchema ,
76- field : InputValueDefinitionNode ,
99+ field : InputValueDefinitionNode | FieldDefinitionNode ,
77100 indentCount : number
78101) : string => {
79- const gen = generateInputObjectFieldTypeMyZodSchema ( config , tsVisitor , schema , field , field . type ) ;
102+ const gen = generateFieldTypeMyZodSchema ( config , tsVisitor , schema , field , field . type ) ;
80103 return indent ( `${ field . name . value } : ${ maybeLazy ( field . type , gen ) } ` , indentCount ) ;
81104} ;
82105
83- const generateInputObjectFieldTypeMyZodSchema = (
106+ const generateFieldTypeMyZodSchema = (
84107 config : ValidationSchemaPluginConfig ,
85108 tsVisitor : TsVisitor ,
86109 schema : GraphQLSchema ,
87- field : InputValueDefinitionNode ,
110+ field : InputValueDefinitionNode | FieldDefinitionNode ,
88111 type : TypeNode ,
89112 parentType ?: TypeNode
90113) : string => {
91114 if ( isListType ( type ) ) {
92- const gen = generateInputObjectFieldTypeMyZodSchema ( config , tsVisitor , schema , field , type . type , type ) ;
115+ const gen = generateFieldTypeMyZodSchema ( config , tsVisitor , schema , field , type . type , type ) ;
93116 if ( ! isNonNullType ( parentType ) ) {
94117 const arrayGen = `myzod.array(${ maybeLazy ( type . type , gen ) } )` ;
95118 const maybeLazyGen = applyDirectives ( config , field , arrayGen ) ;
@@ -98,7 +121,7 @@ const generateInputObjectFieldTypeMyZodSchema = (
98121 return `myzod.array(${ maybeLazy ( type . type , gen ) } )` ;
99122 }
100123 if ( isNonNullType ( type ) ) {
101- const gen = generateInputObjectFieldTypeMyZodSchema ( config , tsVisitor , schema , field , type . type , type ) ;
124+ const gen = generateFieldTypeMyZodSchema ( config , tsVisitor , schema , field , type . type , type ) ;
102125 return maybeLazy ( type . type , gen ) ;
103126 }
104127 if ( isNamedType ( type ) ) {
@@ -125,7 +148,7 @@ const generateInputObjectFieldTypeMyZodSchema = (
125148
126149const applyDirectives = (
127150 config : ValidationSchemaPluginConfig ,
128- field : InputValueDefinitionNode ,
151+ field : InputValueDefinitionNode | FieldDefinitionNode ,
129152 gen : string
130153) : string => {
131154 if ( config . directives && field . directives ) {
0 commit comments