-
-
Notifications
You must be signed in to change notification settings - Fork 60
Description
Here's a well-structured GitHub issue you can log for graphql-codegen-typescript-validation-schema:
Bug Report: enumPrefix and typesPrefix incorrectly applies to enum imports
Description
When using typesPrefix: "I" with importFrom, the generated Zod schemas incorrectly import enums with the I prefix, even though:
- The source TypeScript file (specified in
importFrom) generates enums without the prefix enumPrefix: falseis explicitly set in the configurationenumsAsTypes: truehas been tried as an alternative
This causes a mismatch between the generated types and the Zod schema imports, resulting in TypeScript errors.
Expected Behavior
When typesPrefix: "I" is set:
- Interfaces/types should be imported as
IMyType - Enums should be imported as
MyEnum(without theIprefix)
The enumPrefix: false configuration should prevent the prefix from being applied to enum imports.
Current Behavior
Enums are incorrectly imported with the I prefix:
// Generated Zod file
import type { IMyEnum } from './generated-models'; // ❌ Wrong
// But the actual generated-models file has:
export enum MyEnum { ... } // Without 'I' prefixThis causes TypeScript error:
Module '"./generated-models"' has no exported member 'IMyEnum'
Configuration
generates:
./src/generated/graphql-types.ts:
plugins:
- typescript
config:
typesPrefix: I
enumsAsTypes: false
./src/generated/graphql-zod.ts:
plugins:
- typescript-validation-schema
config:
schema: zod
importFrom: ./graphql-types
typesPrefix: I
enumPrefix: false # ❌ Not working
enumsAsTypes: true # ❌ Also tried this, doesn't help
withObjectType: false
notAllowEmptyString: true
useTypeImports: true
scalarSchemas:
DateTime: z.string().datetime()
Date: z.string().date()
JSON: z.record(z.unknown())GraphQL Schema
enum UserRole {
ADMIN
USER
GUEST
}
input CreateUserInput {
name: String!
role: UserRole!
}Generated TypeScript Types (graphql-types.ts)
// ✅ Enum generated WITHOUT prefix (correct)
export enum UserRole {
Admin = 'ADMIN',
User = 'USER',
Guest = 'GUEST'
}
// ✅ Interface generated WITH prefix (correct)
export interface ICreateUserInput {
name: string;
role: UserRole;
}Generated Zod Schema (graphql-zod.ts)
import { z } from 'zod';
import type { IUserRole } from './graphql-types'; // ❌ Wrong! Should be UserRole
import type { ICreateUserInput } from './graphql-types'; // ✅ Correct
export const CreateUserInputSchema = z.object({
name: z.string(),
role: z.nativeEnum(IUserRole) // ❌ TypeScript error: IUserRole doesn't exist
});Expected Generated Zod Schema
import { z } from 'zod';
import type { UserRole } from './graphql-types'; // ✅ Correct - no prefix
import type { ICreateUserInput } from './graphql-types'; // ✅ Correct - with prefix
export const CreateUserInputSchema = z.object({
name: z.string(),
role: z.nativeEnum(UserRole) // ✅ Works correctly
});Environment
graphql-codegen-typescript-validation-schemaversion: [INSERT YOUR VERSION]@graphql-codegen/cliversion: [INSERT YOUR VERSION]@graphql-codegen/typescriptversion: [INSERT YOUR VERSION]- Node version: [INSERT YOUR VERSION]
- Package manager: npm/yarn/pnpm
Reproduction
- Configure codegen with
typesPrefix: "I"in thetypescriptplugin - Configure
typescript-validation-schemawithimportFrompointing to the types file - Set
enumPrefix: falseorenumsAsTypes: true - Generate code
- Observe that enum imports have the
Iprefix in the Zod file
Workarounds Attempted
- ✅ Tried: Setting
enumPrefix: false- Doesn't work - ✅ Tried: Setting
enumsAsTypes: true- Doesn't work - ✅ Tried: Setting both together - Doesn't work
⚠️ Current workaround: Manually find/replace enum imports after generation
Proposed Solution
The typescript-validation-schema plugin should respect the enumPrefix configuration or provide a separate configuration option like enumImportPrefix that defaults to false when enumPrefix: false is set.
Alternatively, the plugin could:
- Check if the imported type is an enum
- Apply
typesPrefixonly to non-enum types - Or provide a
skipPrefixForEnumsoption
Additional Context
This issue makes it difficult to use a consistent naming convention where:
- Interfaces have prefixes (e.g.,
IUser) - Enums don't have prefixes (e.g.,
UserRole)
This is a common TypeScript convention and the @graphql-codegen/typescript plugin supports it correctly with enumPrefix: false, but typescript-validation-schema doesn't respect this setting when importing.
Related Issues
- Similar to how
typesPrefixworks in@graphql-codegen/typescript - May be related to how imports are generated in the plugin