Skip to content

When using typesPrefix: "I" we want to avoid prefix for enum by adding "enumPrefix": false #1275

@veeramarni

Description

@veeramarni

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:

  1. The source TypeScript file (specified in importFrom) generates enums without the prefix
  2. enumPrefix: false is explicitly set in the configuration
  3. enumsAsTypes: true has 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 the I prefix)

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' prefix

This 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-schema version: [INSERT YOUR VERSION]
  • @graphql-codegen/cli version: [INSERT YOUR VERSION]
  • @graphql-codegen/typescript version: [INSERT YOUR VERSION]
  • Node version: [INSERT YOUR VERSION]
  • Package manager: npm/yarn/pnpm

Reproduction

  1. Configure codegen with typesPrefix: "I" in the typescript plugin
  2. Configure typescript-validation-schema with importFrom pointing to the types file
  3. Set enumPrefix: false or enumsAsTypes: true
  4. Generate code
  5. Observe that enum imports have the I prefix in the Zod file

Workarounds Attempted

  1. Tried: Setting enumPrefix: false - Doesn't work
  2. Tried: Setting enumsAsTypes: true - Doesn't work
  3. Tried: Setting both together - Doesn't work
  4. ⚠️ 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:

  1. Check if the imported type is an enum
  2. Apply typesPrefix only to non-enum types
  3. Or provide a skipPrefixForEnums option

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 typesPrefix works in @graphql-codegen/typescript
  • May be related to how imports are generated in the plugin

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions