@@ -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
@@ -69,6 +77,44 @@ export class ZodSchemaVisitor extends BaseSchemaVisitor {
69
77
} ;
70
78
}
71
79
80
+ get InterfaceTypeDefinition ( ) {
81
+ return {
82
+ leave : InterfaceTypeDefinitionBuilder ( this . config . withInterfaceType , ( node : InterfaceTypeDefinitionNode ) => {
83
+ const visitor = this . createVisitor ( 'output' ) ;
84
+ const name = visitor . convertName ( node . name . value ) ;
85
+ this . importTypes . push ( name ) ;
86
+
87
+ // Building schema for field arguments.
88
+ const argumentBlocks = this . buildTypeDefinitionArguments ( node , visitor ) ;
89
+ const appendArguments = argumentBlocks ? '\n' + argumentBlocks : '' ;
90
+
91
+ // Building schema for fields.
92
+ const shape = node . fields ?. map ( field => generateFieldZodSchema ( this . config , visitor , field , 2 ) ) . join ( ',\n' ) ;
93
+
94
+ switch ( this . config . validationSchemaExportType ) {
95
+ case 'const' :
96
+ return (
97
+ new DeclarationBlock ( { } )
98
+ . export ( )
99
+ . asKind ( 'const' )
100
+ . withName ( `${ name } Schema: z.ZodObject<Properties<${ name } >>` )
101
+ . withContent ( [ `z.object({` , shape , '})' ] . join ( '\n' ) ) . string + appendArguments
102
+ ) ;
103
+
104
+ case 'function' :
105
+ default :
106
+ return (
107
+ new DeclarationBlock ( { } )
108
+ . export ( )
109
+ . asKind ( 'function' )
110
+ . withName ( `${ name } Schema(): z.ZodObject<Properties<${ name } >>` )
111
+ . withBlock ( [ indent ( `return z.object({` ) , shape , indent ( '})' ) ] . join ( '\n' ) ) . string + appendArguments
112
+ ) ;
113
+ }
114
+ } ) ,
115
+ } ;
116
+ }
117
+
72
118
get ObjectTypeDefinition ( ) {
73
119
return {
74
120
leave : ObjectTypeDefinitionBuilder ( this . config . withObjectType , ( node : ObjectTypeDefinitionNode ) => {
0 commit comments