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 55 GraphQLSchema ,
66 InputObjectTypeDefinitionNode ,
77 InputValueDefinitionNode ,
8+ Kind ,
89 NameNode ,
910 ObjectTypeDefinitionNode ,
1011 TypeNode ,
@@ -234,7 +235,19 @@ const generateFieldTypeMyZodSchema = (
234235 if ( isListType ( parentType ) ) {
235236 return `${ gen } .nullable()` ;
236237 }
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+
238251 if ( isNonNullType ( parentType ) ) {
239252 if ( visitor . shouldEmitAsNotAllowEmptyString ( type . name . value ) ) {
240253 return `${ gen } .min(1)` ;
Original file line number Diff line number Diff line change @@ -991,4 +991,38 @@ describe('myzod', () => {
991991 }` ;
992992 expect ( result . content ) . toContain ( wantContain ) ;
993993 } ) ;
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+ } ) ;
9941028} ) ;
You can’t perform that action at this time.
0 commit comments