Skip to content

Commit a662acb

Browse files
committed
fixed initialEmit in zod
1 parent 959b95c commit a662acb

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

src/index.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,13 @@ export const plugin: PluginFunction<ValidationSchemaPluginConfig, Types.ComplexP
2323

2424
return {
2525
prepend: buildImports(),
26-
content: '\n' + [
27-
initialEmit(),
28-
...generated,
29-
].join('\n'),
26+
content: [initialEmit(), ...generated].join('\n'),
3027
};
3128
};
3229

3330
const schemaVisitor = (schema: GraphQLSchema, config: ValidationSchemaPluginConfig) => {
3431
if (config?.schema === 'zod') {
35-
return ZodSchemaVisitor(schema, config)
32+
return ZodSchemaVisitor(schema, config);
3633
}
37-
return YupSchemaVisitor(schema, config)
38-
}
34+
return YupSchemaVisitor(schema, config);
35+
};

src/zod/index.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { TsVisitor } from '@graphql-codegen/typescript';
1313
import { buildApi, formatDirectiveConfig } from '../directive';
1414

1515
const importZod = `import { z } from 'zod'`;
16-
const anySchema = `definedNonNullAnySchema`
16+
const anySchema = `definedNonNullAnySchema`;
1717

1818
export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchemaPluginConfig) => {
1919
const tsVisitor = new TsVisitor(schema, config);
@@ -28,11 +28,12 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
2828
return [importZod];
2929
},
3030
initialEmit: (): string =>
31+
'\n' +
3132
[
3233
// Unfortunately, zod doesn’t provide non-null defined any schema.
3334
// This is a temporary hack until it is fixed.
3435
// see: https://github.com/colinhacks/zod/issues/884
35-
'type definedNonNullAny = {}',
36+
new DeclarationBlock({}).asKind('type').withName('definedNonNullAny').withContent('{}').string,
3637
new DeclarationBlock({})
3738
.export()
3839
.asKind('const')
@@ -43,7 +44,7 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
4344
.asKind('const')
4445
.withName(`${anySchema}: z.ZodSchema<definedNonNullAny>`)
4546
.withContent(`z.any().refine((v) => isDefinedNonNullAny(v))`).string,
46-
].join('\n\n'),
47+
].join('\n'),
4748
InputObjectTypeDefinition: (node: InputObjectTypeDefinitionNode) => {
4849
const name = tsVisitor.convertName(node.name.value);
4950
importTypes.push(name);
@@ -115,22 +116,18 @@ const generateInputObjectFieldTypeZodSchema = (
115116
if (isNamedType(type)) {
116117
const gen = generateNameNodeZodSchema(tsVisitor, schema, type.name);
117118
if (isNonNullType(parentType)) {
118-
return gen
119+
return gen;
119120
}
120121
if (isListType(parentType)) {
121-
return `${gen}.nullable()`
122+
return `${gen}.nullable()`;
122123
}
123-
return `${gen}.nullish()`
124+
return `${gen}.nullish()`;
124125
}
125126
console.warn('unhandled type:', type);
126127
return '';
127128
};
128129

129-
const generateNameNodeZodSchema = (
130-
tsVisitor: TsVisitor,
131-
schema: GraphQLSchema,
132-
node: NameNode
133-
): string => {
130+
const generateNameNodeZodSchema = (tsVisitor: TsVisitor, schema: GraphQLSchema, node: NameNode): string => {
134131
const typ = schema.getType(node.value);
135132

136133
if (typ && typ.astNode?.kind === 'InputObjectTypeDefinition') {

0 commit comments

Comments
 (0)