Skip to content

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

@gherciu

Description

@gherciu

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 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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions