-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[typescript-operations] Add missing enum tests & fix enum generating logic for standalone approach #10525
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
🦋 Changeset detectedLatest commit: 24c5851 The changes in this PR will be included in the next version bump. This PR includes changesets to release 9 packages
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 |
| 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); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
| 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 | ||
| }); |
There was a problem hiding this comment.
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!
Description
This is the continuation of #10508. We merged that PR early to unblock the enum-in-input scenario
Related #10496
Type of change
How Has This Been Tested?