fix: prevent duplicate client type declarations with graphql-codegen v7#497
Merged
Merged
Conversation
Since graphql-codegen v7 the `typescript-operations` plugin re-emits every enum and input-object type used by an operation into its own output, which collided with the `typescript` plugin when both wrote to a single `client/index.ts` — declaring every used `*Where`/`Create*`/`Update*` input and every used enum twice (TS2300) and clashing enums as `enum` vs string-literal `type` alias (TS2567) in consumers' generated client types. Split the client output into `client/schema.ts` (full schema types via the `typescript` plugin) and `client/index.ts` (operation types via `typescript-operations`), wiring the latter to the former with `importSchemaTypesFrom` and re-exporting via `export * from './schema'` so the public import surface is unchanged. See dotansimha/graphql-code-generator#10782. Add a regression test asserting the generated client output has no duplicate declarations, and regenerate the test fixtures. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Map the `Time` scalar via its module (`@smartive/graphql-magic#Time`) and enable `useTypeImports`, instead of injecting the import with a hard-coded `add` plugin. The `add` import landed in the operations file (`client/index.ts`) unconditionally; a consumer with `noUnusedLocals` whose client operations never select a `Time` field would have hit `TS6133: 'Time' is declared but its value is never read` — a failure mode the previous single-file layout didn't have. graphql-codegen now emits the `Time` import only in the files that actually use it. The committed test fixtures are unchanged (their operations do use `Time`). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
🎉 This PR is included in version 28.0.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
After #490 (graphql-codegen monorepo → v7),
gqm generate's client output declares every type twice. In consumers (e.g. zwei-wealth-platform) the regeneratedsrc/generated/client/index.tsgets hundreds ofTS2300: Duplicate identifier(*Where/*SubWhere/Create*/Update*/enums) plusTS2567: Enum declarations can only merge with namespace or other enum declarations— breaking both type-check and the app build.Root cause
Since
@graphql-codegen/typescript-operationsv6 the operations plugin visits the schema AST itself and emits every enum + input-object type an operation uses (_usedSchemaTypes) directly into its own output — unlessimportSchemaTypesFromis set (typescript-operations/.../visitor.js,EnumTypeDefinition/InputObjectTypeDefinition:if (!used || importSchemaTypesFrom) return null). In v5 it emitted no schema types.generateGraphqlClientTypes()runs bothtypescript(full schema) andtypescript-operationsinto a singleclient/index.ts, so every used input/enum is now declared twice, andReactionType-style enums additionally clash as a realenum(typescript) vs a string-literaltypealias (operations).This is upstream dotansimha/graphql-code-generator#10782, closed not-planned; the maintainer-recommended fix is
importSchemaTypesFrom.Fix
Split the client output into two files, the idiomatic v7 way:
client/schema.ts—typescriptplugin → full schema typesclient/index.ts—typescript-operationsplugin withimportSchemaTypesFrom: '<gen>/client/schema'→ operation types only, referencing the shared schema typesexport * from './schema'is added toclient/index.tsso the public import surface is unchanged — consumers still get every schema type and operation type from the singlecliententrypoint. The resolver side (api/index.ts) was never affected (no operations plugin) and only sees benign v7 regen (dropped unused utility types,Upload: any→unknown).Regression test
tests/unit/generate-client-types.spec.tsasserts the generated client output has no duplicate declarations (and that historically-doubled types likeCreateReview/ReactionType/ReviewWhereappear exactly once). Verified it fails on the old single-file config and passes with the fix — the exact check that would have caught #490.Verification
tsc --stricton the output → 0 errorsnpm run test(lint + 121 tests + build)🤖 Generated with Claude Code