MichalLytek / MichalLytek/type-graphql

New config option which would generate Enum Values in schema instead of only keys

Open
#1,770 4 comments 0 reactions 0 assignees View on GitHub
Community :family_man_girl: Discussion :speech_balloon: Enhancement :new:
Dominant language
TypeScript
Stars
8.1k
Forks
672
PR merge metrics
No merged PRs in 30d

Description

At the moment by default type-graphql generates the enums in the schema files only using their keys e.g:
```
enum MyEnum {
FIRST
SECOND
}
```

While this is ok in most cases, since the values itself is passed to the code not the key, it covers most of the cases.

But...
A lot of developers use tools like [GraphQl CodeGen](https://the-guild.dev/graphql/codegen) to generate their types for Frontend from graphql schema, so that they do not need to write in UI these twice and just import from the generated file.

Now to use these generated types is impossible because code-generator knows from schema only the Keys of these enums and asumes the values are the same as the keys

And Given that GraphQL also allows different values for the enums I think we should add a Optional not breaking option to type-graphql to let the developer select if they want a different behavior and having the values in the gql schema as well, so that when they generate the TS types it gets generated correctly with values

Now a lot of peoples will say well why don't you use a monorepo and keep the types as a separate lib and the I'm port in UI as well
well this is impossible because the generated enums in schema as I said has only keys and when you send a value to as variable it expects this exact format but the enum in TS will resolve to its value
and if you have in TS something like
```
enum MyEnum {
FIRST = 'first'
SECOND = 'second'
}
```
If you'll try to use this as gql variable it won't work even if that is what you provided to type-graphql to register

Contributor guide

Open the contributing guide

Research direction

Start by tracing how type-graphql registers TypeScript enums and produces the GraphQL schema, then determine where an optional configuration setting could control enum value exposure. Done means the default key-only behavior remains compatible while the new option produces schemas that represent the configured enum values correctly for code generation.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, typescript
Domain
api, backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.