@@ -5,6 +5,7 @@ import type {
55 GraphQLSchema ,
66 InputObjectTypeDefinitionNode ,
77 InputValueDefinitionNode ,
8+ InterfaceTypeDefinitionNode ,
89 NameNode ,
910 ObjectTypeDefinitionNode ,
1011 TypeNode ,
@@ -18,7 +19,14 @@ import type { ValidationSchemaPluginConfig } from '../config';
1819import { buildApi , formatDirectiveConfig } from '../directive' ;
1920import { BaseSchemaVisitor } from '../schema_visitor' ;
2021import type { Visitor } from '../visitor' ;
21- import { ObjectTypeDefinitionBuilder , isInput , isListType , isNamedType , isNonNullType } from './../graphql' ;
22+ import {
23+ InterfaceTypeDefinitionBuilder ,
24+ ObjectTypeDefinitionBuilder ,
25+ isInput ,
26+ isListType ,
27+ isNamedType ,
28+ isNonNullType ,
29+ } from './../graphql' ;
2230
2331const anySchema = `definedNonNullAnySchema` ;
2432
@@ -53,6 +61,44 @@ export class MyZodSchemaVisitor extends BaseSchemaVisitor {
5361 } ;
5462 }
5563
64+ get InterfaceTypeDefinition ( ) {
65+ return {
66+ leave : InterfaceTypeDefinitionBuilder ( this . config . withInterfaceType , ( node : InterfaceTypeDefinitionNode ) => {
67+ const visitor = this . createVisitor ( 'output' ) ;
68+ const name = visitor . convertName ( node . name . value ) ;
69+ this . importTypes . push ( name ) ;
70+
71+ // Building schema for field arguments.
72+ const argumentBlocks = this . buildTypeDefinitionArguments ( node , visitor ) ;
73+ const appendArguments = argumentBlocks ? `\n${ argumentBlocks } ` : '' ;
74+
75+ // Building schema for fields.
76+ const shape = node . fields ?. map ( field => generateFieldMyZodSchema ( this . config , visitor , field , 2 ) ) . join ( ',\n' ) ;
77+
78+ switch ( this . config . validationSchemaExportType ) {
79+ case 'const' :
80+ return (
81+ new DeclarationBlock ( { } )
82+ . export ( )
83+ . asKind ( 'const' )
84+ . withName ( `${ name } Schema: myzod.Type<${ name } >` )
85+ . withContent ( [ `myzod.object({` , shape , '})' ] . join ( '\n' ) ) . string + appendArguments
86+ ) ;
87+
88+ case 'function' :
89+ default :
90+ return (
91+ new DeclarationBlock ( { } )
92+ . export ( )
93+ . asKind ( 'function' )
94+ . withName ( `${ name } Schema(): myzod.Type<${ name } >` )
95+ . withBlock ( [ indent ( `return myzod.object({` ) , shape , indent ( '})' ) ] . join ( '\n' ) ) . string + appendArguments
96+ ) ;
97+ }
98+ } ) ,
99+ } ;
100+ }
101+
56102 get ObjectTypeDefinition ( ) {
57103 return {
58104 leave : ObjectTypeDefinitionBuilder ( this . config . withObjectType , ( node : ObjectTypeDefinitionNode ) => {
@@ -61,7 +107,7 @@ export class MyZodSchemaVisitor extends BaseSchemaVisitor {
61107 this . importTypes . push ( name ) ;
62108
63109 // Building schema for field arguments.
64- const argumentBlocks = this . buildObjectTypeDefinitionArguments ( node , visitor ) ;
110+ const argumentBlocks = this . buildTypeDefinitionArguments ( node , visitor ) ;
65111 const appendArguments = argumentBlocks ? `\n${ argumentBlocks } ` : '' ;
66112
67113 // Building schema for fields.
@@ -266,6 +312,7 @@ function generateNameNodeMyZodSchema(config: ValidationSchemaPluginConfig, visit
266312 const converter = visitor . getNameNodeConverter ( node ) ;
267313
268314 switch ( converter ?. targetKind ) {
315+ case 'InterfaceTypeDefinition' :
269316 case 'InputObjectTypeDefinition' :
270317 case 'ObjectTypeDefinition' :
271318 case 'UnionTypeDefinition' :
@@ -279,7 +326,12 @@ function generateNameNodeMyZodSchema(config: ValidationSchemaPluginConfig, visit
279326 }
280327 case 'EnumTypeDefinition' :
281328 return `${ converter . convertName ( ) } Schema` ;
329+ case 'ScalarTypeDefinition' :
330+ return myzod4Scalar ( config , visitor , node . value ) ;
282331 default :
332+ if ( converter ?. targetKind )
333+ console . warn ( 'Unknown target kind' , converter . targetKind ) ;
334+
283335 return myzod4Scalar ( config , visitor , node . value ) ;
284336 }
285337}
0 commit comments