File tree Expand file tree Collapse file tree 2 files changed +48
-1
lines changed Expand file tree Collapse file tree 2 files changed +48
-1
lines changed Original file line number Diff line number Diff line change 5
5
GraphQLSchema ,
6
6
InputObjectTypeDefinitionNode ,
7
7
InputValueDefinitionNode ,
8
+ Kind ,
8
9
NameNode ,
9
10
ObjectTypeDefinitionNode ,
10
11
TypeNode ,
@@ -234,7 +235,19 @@ const generateFieldTypeMyZodSchema = (
234
235
if ( isListType ( parentType ) ) {
235
236
return `${ gen } .nullable()` ;
236
237
}
237
- const appliedDirectivesGen = applyDirectives ( config , field , gen ) ;
238
+ let appliedDirectivesGen = applyDirectives ( config , field , gen ) ;
239
+
240
+ if ( field . kind === Kind . INPUT_VALUE_DEFINITION ) {
241
+ const { defaultValue } = field ;
242
+
243
+ if ( defaultValue ?. kind === Kind . INT || defaultValue ?. kind === Kind . FLOAT || defaultValue ?. kind === Kind . BOOLEAN ) {
244
+ appliedDirectivesGen = `${ appliedDirectivesGen } .default(${ defaultValue . value } )` ;
245
+ }
246
+ if ( ( defaultValue ?. kind === Kind . STRING ) || ( defaultValue ?. kind === Kind . ENUM ) ) {
247
+ appliedDirectivesGen = `${ appliedDirectivesGen } .default("${ defaultValue . value } ")` ;
248
+ }
249
+ }
250
+
238
251
if ( isNonNullType ( parentType ) ) {
239
252
if ( visitor . shouldEmitAsNotAllowEmptyString ( type . name . value ) ) {
240
253
return `${ gen } .min(1)` ;
Original file line number Diff line number Diff line change @@ -991,4 +991,38 @@ describe('myzod', () => {
991
991
}` ;
992
992
expect ( result . content ) . toContain ( wantContain ) ;
993
993
} ) ;
994
+
995
+ it ( 'with default input values' , async ( ) => {
996
+ const schema = buildSchema ( /* GraphQL */ `
997
+ enum PageType {
998
+ PUBLIC
999
+ BASIC_AUTH
1000
+ }
1001
+ input PageInput {
1002
+ pageType: PageType! = PUBLIC
1003
+ greeting: String = "Hello"
1004
+ score: Int = 100
1005
+ ratio: Float = 0.5
1006
+ isMember: Boolean = true
1007
+ }
1008
+ ` ) ;
1009
+ const result = await plugin (
1010
+ schema ,
1011
+ [ ] ,
1012
+ {
1013
+ schema : 'myzod' ,
1014
+ importFrom : './types' ,
1015
+ } ,
1016
+ { }
1017
+ ) ;
1018
+
1019
+ expect ( result . content ) . toContain ( 'export const PageTypeSchema = myzod.enum(PageType)' ) ;
1020
+ expect ( result . content ) . toContain ( 'export function PageInputSchema(): myzod.Type<PageInput>' ) ;
1021
+
1022
+ expect ( result . content ) . toContain ( 'pageType: PageTypeSchema.default("PUBLIC")' ) ;
1023
+ expect ( result . content ) . toContain ( 'greeting: myzod.string().default("Hello").optional().nullable()' ) ;
1024
+ expect ( result . content ) . toContain ( 'score: myzod.number().default(100).optional().nullable()' ) ;
1025
+ expect ( result . content ) . toContain ( 'ratio: myzod.number().default(0.5).optional().nullable()' ) ;
1026
+ expect ( result . content ) . toContain ( 'isMember: myzod.boolean().default(true).optional().nullable()' ) ;
1027
+ } ) ;
994
1028
} ) ;
You can’t perform that action at this time.
0 commit comments