Skip to content
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

Enums with one key are treated as String literals #3025

Open
2 of 4 tasks
IodizedGabe opened this issue Oct 13, 2023 · 3 comments
Open
2 of 4 tasks

Enums with one key are treated as String literals #3025

IodizedGabe opened this issue Oct 13, 2023 · 3 comments

Comments

@IodizedGabe
Copy link

IodizedGabe commented Oct 13, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

When defining a field for an object, using an enum with exactly 1 key will result in that field being seen as a String, rather than an enum.

Example

export enum MultipleEnum {
    One = 'One',
    Two = 'Two',
}

registerEnumType(MultipleEnum, {
    name: 'MultipleEnum',
    description: 'Enum with multiple keys',
});

export enum SingleEnum {
    Single = 'Single',
}

registerEnumType(SingleEnum, {
    name: 'SingleEnum',
    description: 'Enum with one key',
});

@ObjectType()
export class ClassA {
    multipleEnum: MultipleEnum;

    singleEnum: SingleEnum;
}

Where the resulting schema will provide a reference to MultipleEnum, but see singleEnum as just a string.

The type information for this the single-keyed enum field sets its flags as 1152, or 1024 | 128, or EnumLiteral | StringLiteral.

When it's then parsed, it will check if it's a string first before checking if it's an enum.

I can also see that (with local edits of the plugin) removing this check restores original functionality, although I imagine it's still useful to have.

Minimum reproduction code

https://github.com/IodizedGabe/nestjs-graphql-enum-as-string

Steps to reproduce

  1. yarn install
  2. yarn build
  3. Look at the _GRAPHQL_METADATA_FACTORY for ClassA in ./dist/graphql.entity.js

For me, I see:

let ClassA = class ClassA {
    static _GRAPHQL_METADATA_FACTORY() {
        return { multipleEnum: { type: () => require("./graphql.entity").MultipleEnum }, singleEnum: { type: () => String } };
    }
};

Expected behavior

An enum with one key should be treated like any other enum and given a proper type / reference in the schema.

Package version

12.0.9

Graphql version

graphql: 16.8.1

NestJS version

10.2.7

Node.js version

18.15.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

typescript: 5.1.6

@kamilmysliwiec
Copy link
Member

Would you like to create a PR for this issue?

@IodizedGabe
Copy link
Author

I could, but I'm not exactly sure of the best way to work around this. The simplest change would be to add another flag check here, but I'm not sure if that's the best way to go about it.

@GuillaumeSD
Copy link

GuillaumeSD commented Jun 3, 2024

This is a regression, it was fine with v11.0.5 but I'm getting this issue with v12.0.0 and onward. It makes us unable to update to Nest v10...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants