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

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

Open
gherciu opened this issue Dec 1, 2024 · 4 comments
Labels
Community 👨‍👧 Something initiated by a community Discussion 💬 Brainstorm about the idea Enhancement 🆕 New feature or request

Comments

@gherciu
Copy link

gherciu commented Dec 1, 2024

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

@gherciu
Copy link
Author

gherciu commented Dec 1, 2024

I know some devs opened this as a BUG, I'll keep as a feature request if the author things that the default should be different
My proposal is just a new config option, so that devs can choose the desired behavior

@MichalLytek
Copy link
Owner

MichalLytek commented Dec 1, 2024

That's why it's recommended to keep it simple:

enum MyEnum {
  FIRST = 'FIRST',
  SECOND = 'SECOND',
}

@MichalLytek MichalLytek added Enhancement 🆕 New feature or request Community 👨‍👧 Something initiated by a community Discussion 💬 Brainstorm about the idea labels Dec 1, 2024
@gherciu
Copy link
Author

gherciu commented Dec 1, 2024

Yes I agree but imagine this examples:

  FIRST_AND_SECOND = 'First & Second',
  YES = 1,
  NO = 0,

This is just an example I guess there may be more similar, developers have different use cases

But at the end if GraphQL allows for such a thing, I mean I think it is a cool feature to have

In My case I was migratingg a project to type-graphql and I was not following the standard you mentioned and it was not a problem, Now I had to refactor the whole code due to this
I mean is a cool feature also to support peoples coming to type-graphql from a existing code base

cc @MichalLytek

@MichalLytek
Copy link
Owner

Now I had to refactor the whole code due to this

That's why we use enums - you can easily just change runtime value of enum member and that's all, no need for search and replace string.

In your case 'First & Second' is not a valid graphql name (spaces) so it wouldn't work anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community 👨‍👧 Something initiated by a community Discussion 💬 Brainstorm about the idea Enhancement 🆕 New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants