@@ -5,6 +5,7 @@ import type {
55 GraphQLSchema ,
66 InputObjectTypeDefinitionNode ,
77 InputValueDefinitionNode ,
8+ InterfaceTypeDefinitionNode ,
89 NameNode ,
910 ObjectTypeDefinitionNode ,
1011 TypeNode ,
@@ -17,8 +18,15 @@ import {
1718import type { ValidationSchemaPluginConfig } from '../config' ;
1819import { buildApi , formatDirectiveConfig } from '../directive' ;
1920import { BaseSchemaVisitor } from '../schema_visitor' ;
20- import type { Visitor } from '../visitor' ;
21- import { ObjectTypeDefinitionBuilder , isInput , isListType , isNamedType , isNonNullType } from './../graphql' ;
21+ import { Visitor } from '../visitor' ;
22+ import {
23+ InterfaceTypeDefinitionBuilder ,
24+ isInput ,
25+ isListType ,
26+ isNamedType ,
27+ isNonNullType ,
28+ ObjectTypeDefinitionBuilder ,
29+ } from './../graphql' ;
2230
2331const anySchema = `definedNonNullAnySchema` ;
2432
@@ -53,6 +61,51 @@ 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 (
96+ [
97+ indent ( `return myzod.object({` ) ,
98+ indent ( `__typename: myzod.literal('${ node . name . value } ').optional(),` , 2 ) ,
99+ shape ,
100+ indent ( '})' ) ,
101+ ] . join ( '\n' )
102+ ) . string + appendArguments
103+ ) ;
104+ }
105+ } ) ,
106+ } ;
107+ }
108+
56109 get ObjectTypeDefinition ( ) {
57110 return {
58111 leave : ObjectTypeDefinitionBuilder ( this . config . withObjectType , ( node : ObjectTypeDefinitionNode ) => {
@@ -266,6 +319,7 @@ function generateNameNodeMyZodSchema(config: ValidationSchemaPluginConfig, visit
266319 const converter = visitor . getNameNodeConverter ( node ) ;
267320
268321 switch ( converter ?. targetKind ) {
322+ case 'InterfaceTypeDefinition' :
269323 case 'InputObjectTypeDefinition' :
270324 case 'ObjectTypeDefinition' :
271325 case 'UnionTypeDefinition' :
@@ -279,7 +333,12 @@ function generateNameNodeMyZodSchema(config: ValidationSchemaPluginConfig, visit
279333 }
280334 case 'EnumTypeDefinition' :
281335 return `${ converter . convertName ( ) } Schema` ;
336+ case 'ScalarTypeDefinition' :
337+ return myzod4Scalar ( config , visitor , node . value ) ;
282338 default :
339+ if ( converter ?. targetKind ) {
340+ console . warn ( 'Unknown target kind' , converter . targetKind ) ;
341+ }
283342 return myzod4Scalar ( config , visitor , node . value ) ;
284343 }
285344}
0 commit comments