dotansimha / dotansimha/graphql-code-generator

typescript plugin does not respect typesPrefix option when enumValues option is also passed in configuration

Open
#9,520 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
11.3k
Forks
1.4k
Avg merge
1d 1h
Merged PRs (30d)
23

Description

EDIT: this issue has been edited from it's original version. The issue as originally filed assumed the wrong root issue, and can be found in it's entirety below:

Original Issue

> ### Which packages are impacted by your issue?
>
> @graphql-codegen/typescript-operations
>
> ### Describe the bug
>
> When providing the `enumValues` configuration option to the `typescript-operations` plugin, it appears to have no effect on the produced output. I would expect the behavior to match that of the `typescript` plugin, where the provided enum is used instead of a generated one. The [documentation](https://the-guild.dev/graphql/codegen/plugins/typescript/typescript-operations#enumvalues) indicates that this option should produce that behavior.
>
> ### Your Example Website or App
>
> Minimum reproducible example: https://codesandbox.io/p/sandbox/graphql-codegen-typescript-operations-enum-values-l3zf49
>
> ### Steps to Reproduce the Bug or Issue
>
> Using the aforementioned example, view the `types.ts` generated file. You'll notice that there's an error on line 41 where the generated code attempts to reference an enum type (`GqlMyEnum`) that doesn't exist.
>
> ### Expected behavior
>
> I would expect to see the generated operations type reference the enum provided in the `enumValues` configuration option. In the aforementioned example, this would mean using the `MyEnum` type imported from `./enums` in the same manner as the code generated from the `typescript` plugin.
>
> ### Platform
>
> NodeJS: 16.17.0
>
> ```json
> "@graphql-codegen/add": "^5.0.0",
> "@graphql-codegen/cli": "^4.0.1",
> "@graphql-codegen/typescript": "^4.0.1",
> "@graphql-codegen/typescript-operations": "^4.0.1",
> "graphql": "^16.2.0"
> ```
>
> ### Additional Example
>
> Here's another example with the `import-types` preset, where it not only uses the wrong value but attempts to import it from the wrong file along with the other generated types: https://codesandbox.io/p/sandbox/graphql-codegen-typescript-operations-enum-values-import-types-9sqrg9

### Which packages are impacted by your issue?

@graphql-codegen/typescript

### Describe the bug

When providing the `enumValues` configuration option alongside the `typesPrefix` option to the `typescript` plugin, the types prefix is not used when aliasing imported variables (there's also no alias used if the imported enum name matches that of the graphql enum). On it's own, this isn't necessarily problematic as the generated types are perfectly valid. However, this is problematic when combined with additional plugins such as `typescript-operations`, since they'll attempt to use a variable with the incorrect name.

It would be reasonable to assume that this also happens when the `typesSuffix` configuration option is provided.

### Your Example Website or App

Minimum reproducible example: https://codesandbox.io/p/sandbox/graphql-codegen-typescript-operations-enum-values-l3zf49
Additional example that shows an alias, but an incorrect one: https://codesandbox.io/p/sandbox/graphql-codegen-typescript-operations-enum-values-alias-kdsljq

### Steps to Reproduce the Bug or Issue

Using the first example, view the `types.ts` generated file. You'll notice that there's an error on line 41 where the generated code attempts to reference an enum type (`GqlMyEnum`) that doesn't exist.

### Expected behavior

I would expect to see the generated types alias the enum provided in the `enumValues` configuration option to match the naming scheme used . In the aforementioned example, this would mean updating the import to look like the following: `import { MyEnum as GqlMyEnum } from './enums';`.

### Platform

NodeJS: 16.17.0

```json
"@graphql-codegen/add": "^5.0.0",
"@graphql-codegen/cli": "^4.0.1",
"@graphql-codegen/typescript": "^4.0.1",
"@graphql-codegen/typescript-operations": "^4.0.1",
"graphql": "^16.2.0"
```

### Additional Example

Here's another example with the `import-types` preset, where it not only uses the wrong value but attempts to import it from the wrong file along with the other generated types: https://codesandbox.io/p/sandbox/graphql-codegen-typescript-operations-enum-values-import-types-9sqrg9

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.