-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Description
Which packages are impacted by your issue?
@graphql-codegen/cli, @graphql-codegen/typescript-operations, @graphql-codegen/typescript
Describe the bug
Description
When using the enumValues configuration to import an external enum with a different name than what appears in the GraphQL schema, the generated code has two issues:
- Invalid re-export syntax: The codegen generates invalid TypeScript/JavaScript when it tries to re-export an import alias
- Name mismatch: The
typescriptandtypescript-operationsplugins use different names for the same enum
Current Behavior
Given this configuration:
config: {
enumValues: {
LicenseSKU: '../shared-generated-enums#LicenseSku',
}
}Where the GraphQL schema has:
enum LicenseSKU {
BASIC
ADVANCED
# ...
}The codegen generates:
// At the top of the file
import { LicenseSku as LicenseSKU } from '@wiz-sec/tenant-shared/graphqlTypes';
// In type definitions from typescript-operations plugin
export type SomeQuery = {
license: {
sku: LicenseSku; // ❌ This type doesn't exist - it expects the imported name
}
}
// At the bottom of the file
export { LicenseSKU }; // Expected Behavior
When using enumValues with an external import, the codegen should use the schema name consistently: Both typescript and typescript-operations plugins should reference the enum by the same name
Suggested Solution
The enum should be aliased internally to match what would be generated if enumValues were not provided.
in our example, the import should have been:
import { LicenseSku as LicenseSku } from './shared';
// or just
import { LicenseSku } from './shared';Workaround
Currently we have to add a manual alias using the add plugin:
{
add: {
content: `export const TenantLicenseSku = TenantLicenseSKU; export type TenantLicenseSku = TenantLicenseSKU;`,
},
}Environment
@graphql-codegen/cli: ^5.0.7@graphql-codegen/typescript: ^4.1.6@graphql-codegen/typescript-operations: (latest)- Node version: 22.x
- OS: macOS
Your Example Website or App
https://codesandbox.io/p/sandbox/github/dotansimha/graphql-code-generator-issue-sandbox-template
Steps to Reproduce the Bug or Issue
just open types.ts, and see that import:
import { LicenseSku as LicenseSKU } from './shared';and LicenseQuery expects LicenseSku
export type LicenseQuery = {
__typename?: 'Query';
license: { __typename?: 'License'; sku: LicenseSku // wrong! };
};Expected behavior
When using enumValues with an external import, the codegen should use the schema name consistently: Both typescript and typescript-operations plugins should reference the enum by the same name
Screenshots or Videos
No response
Platform
- OS: [e.g. macOS, Windows, Linux]
- NodeJS: [e.g. 18.5.0]
graphqlversion: [e.g. 16.3.0]@graphql-codegen/*version(s): [e.g. 2.6.2]
Codegen Config File
No response
Additional context
No response