You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
Is there an existing issue for this?
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
Where the resulting schema will provide a reference to
MultipleEnum
, but seesingleEnum
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
_GRAPHQL_METADATA_FACTORY
forClassA
in./dist/graphql.entity.js
For me, I see:
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.1NestJS version
10.2.7
Node.js version
18.15.0
In which operating systems have you tested?
Other
typescript
: 5.1.6The text was updated successfully, but these errors were encountered: