@@ -5,6 +5,7 @@ import type {
5
5
GraphQLSchema ,
6
6
InputObjectTypeDefinitionNode ,
7
7
InputValueDefinitionNode ,
8
+ InterfaceTypeDefinitionNode ,
8
9
NameNode ,
9
10
ObjectTypeDefinitionNode ,
10
11
TypeNode ,
@@ -17,8 +18,15 @@ import {
17
18
import type { ValidationSchemaPluginConfig } from '../config' ;
18
19
import { buildApi , formatDirectiveConfig } from '../directive' ;
19
20
import { 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' ;
22
30
23
31
const anySchema = `definedNonNullAnySchema` ;
24
32
@@ -53,6 +61,51 @@ export class MyZodSchemaVisitor extends BaseSchemaVisitor {
53
61
} ;
54
62
}
55
63
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
+
56
109
get ObjectTypeDefinition ( ) {
57
110
return {
58
111
leave : ObjectTypeDefinitionBuilder ( this . config . withObjectType , ( node : ObjectTypeDefinitionNode ) => {
@@ -266,6 +319,7 @@ function generateNameNodeMyZodSchema(config: ValidationSchemaPluginConfig, visit
266
319
const converter = visitor . getNameNodeConverter ( node ) ;
267
320
268
321
switch ( converter ?. targetKind ) {
322
+ case 'InterfaceTypeDefinition' :
269
323
case 'InputObjectTypeDefinition' :
270
324
case 'ObjectTypeDefinition' :
271
325
case 'UnionTypeDefinition' :
@@ -279,7 +333,12 @@ function generateNameNodeMyZodSchema(config: ValidationSchemaPluginConfig, visit
279
333
}
280
334
case 'EnumTypeDefinition' :
281
335
return `${ converter . convertName ( ) } Schema` ;
336
+ case 'ScalarTypeDefinition' :
337
+ return myzod4Scalar ( config , visitor , node . value ) ;
282
338
default :
339
+ if ( converter ?. targetKind ) {
340
+ console . warn ( 'Unknown target kind' , converter . targetKind ) ;
341
+ }
283
342
return myzod4Scalar ( config , visitor , node . value ) ;
284
343
}
285
344
}
0 commit comments