|
5 | 5 | GraphQLSchema,
|
6 | 6 | InputObjectTypeDefinitionNode,
|
7 | 7 | InputValueDefinitionNode,
|
| 8 | + Kind, |
8 | 9 | NameNode,
|
9 | 10 | ObjectTypeDefinitionNode,
|
10 | 11 | TypeNode,
|
@@ -129,16 +130,16 @@ export class ZodSchemaVisitor extends BaseSchemaVisitor {
|
129 | 130 | this.enumDeclarations.push(
|
130 | 131 | this.config.enumsAsTypes
|
131 | 132 | ? new DeclarationBlock({})
|
132 |
| - .export() |
133 |
| - .asKind('const') |
134 |
| - .withName(`${enumname}Schema`) |
135 |
| - .withContent(`z.enum([${node.values?.map(enumOption => `'${enumOption.name.value}'`).join(', ')}])`) |
136 |
| - .string |
| 133 | + .export() |
| 134 | + .asKind('const') |
| 135 | + .withName(`${enumname}Schema`) |
| 136 | + .withContent(`z.enum([${node.values?.map(enumOption => `'${enumOption.name.value}'`).join(', ')}])`) |
| 137 | + .string |
137 | 138 | : new DeclarationBlock({})
|
138 |
| - .export() |
139 |
| - .asKind('const') |
140 |
| - .withName(`${enumname}Schema`) |
141 |
| - .withContent(`z.nativeEnum(${enumname})`).string |
| 139 | + .export() |
| 140 | + .asKind('const') |
| 141 | + .withName(`${enumname}Schema`) |
| 142 | + .withContent(`z.nativeEnum(${enumname})`).string |
142 | 143 | );
|
143 | 144 | },
|
144 | 145 | };
|
@@ -247,7 +248,19 @@ const generateFieldTypeZodSchema = (
|
247 | 248 | if (isListType(parentType)) {
|
248 | 249 | return `${gen}.nullable()`;
|
249 | 250 | }
|
250 |
| - const appliedDirectivesGen = applyDirectives(config, field, gen); |
| 251 | + let appliedDirectivesGen = applyDirectives(config, field, gen); |
| 252 | + |
| 253 | + if (field.kind === Kind.INPUT_VALUE_DEFINITION) { |
| 254 | + const { defaultValue } = field; |
| 255 | + |
| 256 | + if (defaultValue?.kind === Kind.INT || defaultValue?.kind === Kind.FLOAT || defaultValue?.kind === Kind.BOOLEAN) { |
| 257 | + appliedDirectivesGen = `${appliedDirectivesGen}.default(${defaultValue.value})`; |
| 258 | + } |
| 259 | + if ((defaultValue?.kind === Kind.STRING) || (defaultValue?.kind === Kind.ENUM)) { |
| 260 | + appliedDirectivesGen = `${appliedDirectivesGen}.default("${defaultValue.value}")`; |
| 261 | + } |
| 262 | + } |
| 263 | + |
251 | 264 | if (isNonNullType(parentType)) {
|
252 | 265 | if (visitor.shouldEmitAsNotAllowEmptyString(type.name.value)) {
|
253 | 266 | return `${appliedDirectivesGen}.min(1)`;
|
|
0 commit comments