Enums with one key are treated as String literals
Is there an existing issue for this?
- [X] 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
-
yarn install -
yarn build - Look at the
_GRAPHQL_METADATA_FACTORYforClassAin./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?
- [X] macOS
- [ ] Windows
- [ ] Linux
Other
typescript: 5.1.6
Would you like to create a PR for this issue?
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.
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...