Skip to content

Generated Enums from valid GraphQL Schema are not always valid Typescript #10411

@thekevinbrown

Description

@thekevinbrown

Which packages are impacted by your issue?

@graphql-codegen/typescript

Describe the bug

Our GraphQL schema includes an enum similar to this:

enum UserStatus {
    VALID
    _
}

When we run codegen we get the following output

export enum UserStatus {
  Valid = 'VALID',
   = '_'
}

Which is not syntactically valid.

Your Example Website or App

https://codesandbox.io/p/devbox/floral-dew-2qjqzk?workspaceId=ws_Kpmj5ZQq64qCxSwHJJhXdB

Steps to Reproduce the Bug or Issue

Go to the codesandbox link and check line 37 of types.ts.

Expected behavior

Because _ is actually a valid Typescript identifier for an enum value I expected Codegen to emit something like

export enum UserStatus {
  Valid = 'VALID',
  _ = '_'
}

Screenshots or Videos

Image

Platform

  • OS: Applicable to all. I am using MacOS.
  • NodeJS: v20.12.1
  • graphql version: 16.6.0
  • @graphql-codegen/* version(s):
    • @graphql-codegen/add: 5.0.0
    • @graphql-codegen/cli: 4.0.1
    • @graphql-codegen/core: 4.0.0
    • @graphql-codegen/plugin-helpers: 5.0.0
    • @graphql-codegen/schema-ast: 4.0.0
    • @graphql-codegen/typescript: 4.0.1
    • @graphql-codegen/typescript-operations: 4.0.1
    • @graphql-codegen/visitor-plugin-common: 4.0.1

Codegen Config File

import { CodegenConfig } from "@graphql-codegen/cli";

const config: CodegenConfig = {
  schema: "schema.graphql",
  documents: "document.graphql",
  generates: {
    "types.ts": { plugins: ["typescript", "typescript-operations"] },
  },
};

export default config;

Additional context

Looking at the code, it looks like underscores are being removed and not removed in various ways through that function to fix other bugs already. I've gone ahead and created a PR for this that would treat this as a special case and simply preserve the identifier if the entire string is made up of _ characters.

If that solution sounds good, let me know, otherwise happy to take your steer on what you think the best fix is. Our GraphQL schema needs to have the _ item in that enum, and we can't add to the name in the schema, so we would like to find some way to get a valid Typescript typings file out with this in place.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions