-
-
Notifications
You must be signed in to change notification settings - Fork 56
Description
Issue Description
When using graphql-codegen-typescript-validation-schema in a TypeScript project with the isolatedDeclarations: true
compiler option, the build fails. The root cause is that the Zod schemas generated for GraphQL enums do not have explicit type annotations, which is a requirement under isolatedDeclarations.
This forces us to either disable a valuable TypeScript compiler feature or avoid using the plugin for enum validation.
Environment
- graphql-codegen: 5.0.7
- graphql-codegen-typescript-validation-schema: 0.17.1
- typescript: 5.6.2
- zod: 3.25.67
- tsconfig.json with
"isolatedDeclarations": true
Steps to Reproduce
- Define a GraphQL schema with an enum:
enum Status {
DRAFT
PUBLISHED
ARCHIVED
}
input PostInput {
status: Status\!
}
- Configure codegen.ts to use the typescript-validation-schema plugin:
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
schema: 'schema.graphql',
generates: {
'./src/validation.ts': {
plugins: [
{
'typescript-validation-schema': {
schema: 'zod',
// ... other options
},
},
],
},
},
};
export default config;
- Run the code generator.
Problematic Output
The plugin generates the following code for the enum, which lacks a type annotation:
// validation.ts
import { z } from 'zod';
export const StatusSchema = z.enum(['DRAFT', 'PUBLISHED', 'ARCHIVED']);
Expected Output
For compatibility with isolatedDeclarations, the generated code should include an explicit type annotation:
// validation.ts
import { z } from 'zod';
export const StatusSchema: z.ZodEnum<["DRAFT", "PUBLISHED", "ARCHIVED"]> = z.enum(['DRAFT', 'PUBLISHED', 'ARCHIVED']);
Compiler Error
When another file imports StatusSchema, the TypeScript compiler throws an error:
error TS9023: Variable 'StatusSchema' must have an explicit type annotation with --isolatedDeclarations.
Suggested Solution
The plugin should be updated to add explicit z.ZodEnum<...>
type annotations to all exported enum schemas it generates. This would make the output compatible with modern TypeScript project configurations that enable isolatedDeclarations.