dotansimha / dotansimha/graphql-code-generator

Invalid enum identifiers generated when enumsAsConst is enabled

Open
#9,149 1 comment 0 reactions 0 assignees View on GitHub
stage/5-alpha-release-testing
Dominant language
TypeScript
Stars
11.3k
Forks
1.4k
Avg merge
1d 1h
Merged PRs (30d)
23

Description

### Which packages are impacted by your issue?

_No response_

### Describe the bug

When generating types with `enumsAsConst: true` enabled, it can generate invalid types if the graphql enum starts with an underscore and digit digit, e.g. `_401K`. The generated type looks like:

```tsx
export const SomeType = {
401K: '_401K',
Other: 'Other'
}
```

This does not compile. I would expect it to generate an enum like:

```tsx
export const SomeType = {
'401K': '_401K',
Other: 'Other'
}
```

### Your Example Website or App

.

### Steps to Reproduce the Bug or Issue

1. Create a GraphQL enum type that includes at least one enum that starts with a digit, e.g.

```
enum SomeType {
_401K
Other
}
```

2. Run graphql-codegen with enumsAsConst enabled

Get the following generated type:

```
export const SomeType = {
401K: '_401K',
Other: 'Other'
} as const
```
, which is invalid TypeScript

### Expected behavior

It would generate something that looks like

```tsx
export const SomeType = {
'401K': '_401K',
Other: 'Other'
} as const
```

### Screenshots or Videos

_No response_

### Platform

macOS, node 16.18.1

### Codegen Config File

extensions:
codegen:
generates:
./src/generated/types.ts:
- add:
content: //@ts-nocheck
- typescript:
enumsAsConst: true
strictScalars: true
scalars:
DateTime: string
ChatDateTime: string

### Additional context

I think on [this line](https://github.com/dotansimha/graphql-code-generator/blob/a39d7a0bb65c5bc91ba9254435fae5bca0f484e7/packages/plugins/typescript/typescript/src/visitor.ts#L429) there needs to be a call to `this.makeValidEnumIdentifier`

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.