Skip to content

Conversation

@eddeee888
Copy link
Collaborator

Description

This is the continuation of #10508. We merged that PR early to unblock the enum-in-input scenario

Related #10496

Type of change

  • Add tests

How Has This Been Tested?

  • Unit test

@changeset-bot
Copy link

changeset-bot bot commented Nov 27, 2025

🦋 Changeset detected

Latest commit: 24c5851

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 9 packages
Name Type
@graphql-codegen/visitor-plugin-common Patch
@graphql-codegen/typescript-operations Patch
@graphql-codegen/typescript Patch
@graphql-codegen/typescript-resolvers Patch
@graphql-codegen/client-preset Patch
@graphql-codegen/typescript-document-nodes Patch
@graphql-codegen/gql-tag-operations Patch
@graphql-codegen/typed-document-node Patch
@graphql-codegen/graphql-modules-preset Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Comment on lines -814 to -865
protected _buildTypeImport(identifier: string, source: string, asDefault = false): string {
const { useTypeImports } = this.config;
if (asDefault) {
if (useTypeImports) {
return `import type { default as ${identifier} } from '${source}';`;
}
return `import ${identifier} from '${source}';`;
}
return `import${useTypeImports ? ' type' : ''} { ${identifier} } from '${source}';`;
}

protected handleEnumValueMapper(
typeIdentifier: string,
importIdentifier: string | null,
sourceIdentifier: string | null,
sourceFile: string | null
): string[] {
if (importIdentifier !== sourceIdentifier) {
// use namespace import to dereference nested enum
// { enumValues: { MyEnum: './my-file#NS.NestedEnum' } }
return [
this._buildTypeImport(importIdentifier || sourceIdentifier, sourceFile),
`import ${typeIdentifier} = ${sourceIdentifier};`,
];
}
if (sourceIdentifier !== typeIdentifier) {
return [this._buildTypeImport(`${sourceIdentifier} as ${typeIdentifier}`, sourceFile)];
}
return [this._buildTypeImport(importIdentifier || sourceIdentifier, sourceFile)];
}

public getEnumsImports(): string[] {
return Object.keys(this.config.enumValues)
.flatMap(enumName => {
const mappedValue = this.config.enumValues[enumName];

if (mappedValue.sourceFile) {
if (mappedValue.isDefault) {
return [this._buildTypeImport(mappedValue.typeIdentifier, mappedValue.sourceFile, true)];
}

return this.handleEnumValueMapper(
mappedValue.typeIdentifier,
mappedValue.importIdentifier,
mappedValue.sourceIdentifier,
mappedValue.sourceFile
);
}

return [];
})
.filter(Boolean);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These utility functions are now abstracted to be shared between base-types-visitor and typescript-operations

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is huge! And it is all the reasonable tests I could find related to how an enum is generated

Comment on lines -167 to -175
describe('TypeScript Operations Plugin - Enum', () => {
it.todo('does not generate unused enum in variables and result');
it.todo('handles native numeric enum correctly');
it.todo('handles const enum correctly');
it.todo('handles native const enum correctly');
it.todo('handles native enum correctly');
it.todo('handles EnumValues correctly');
// Bring over tests from https://github.com/dotansimha/graphql-code-generator/blob/accdab69106605241933e9d66d64dc7077656f30/packages/plugins/typescript/typescript/tests/typescript.spec.ts
});
Copy link
Collaborator Author

@eddeee888 eddeee888 Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests are moved to packages/plugins/typescript/operations/tests/ts-documents.standalone.enum.spec.ts since there are so many!

@eddeee888 eddeee888 changed the title [typescript-operations] Add missing enum tests for standalone approach [typescript-operations] Add missing enum tests & fix enum generating logic for standalone approach Dec 3, 2025
@eddeee888 eddeee888 marked this pull request as ready for review December 3, 2025 14:03
@eddeee888 eddeee888 merged commit ad00ac8 into master-next Dec 3, 2025
16 checks passed
@eddeee888 eddeee888 deleted the add-enum-tests branch December 3, 2025 14:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants