dotansimha / graphql-code-generator

A tool for generating code based on a GraphQL schema and GraphQL operations (query/mutation/subscription), with flexible support for custom plugins.
https://the-guild.dev/graphql/codegen/
MIT License
10.88k stars 1.34k forks source link

Invalid enum identifiers generated when enumsAsConst is enabled #9149

Open ryanliljestrom opened 1 year ago

ryanliljestrom commented 1 year ago

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:

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

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

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
}
  1. 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

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:

Additional context

I think on this line there needs to be a call to this.makeValidEnumIdentifier

rliljest commented 1 year ago

Fixed with @graphql-codegen/typescript-operations@3.0.3