Skip to content

[discussion]: Automatically transforms enums to CAPITALIZED_WITH_UNDERSCORES style #1832

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

Closed
1 task done
kasvith opened this issue Oct 28, 2021 · 4 comments
Closed
1 task done
Labels

Comments

@kasvith
Copy link

kasvith commented Oct 28, 2021

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe it

In GraphQL world, enums are represented as CAPITALIZED_WITH_UNDERSCORES. This is described in the GraphQL spec as well.

GraphQL

enum CatStatus {
    HAPPY_AND_HEALTHY
    SAD_AND_HEALTHY
}

In the typescript world, it's common to use PascalCase for enum representations.

Typescript

enum CatStatus {
   HappyAndHealthy,
   SadAndHealthy
}

Code-First approach in the docs suggests using CAPITALIZED_WITH_UNDERSCORES as enum values. But that doesn't play nice with Typescript world.

Describe the solution you'd like

Automatically transform AnyCase, anyCase , any_case to the respective GraphQL enum representation on code generation and map to correct internal representation in NestJS side.

So,

Typescript

enum CatStatus {
   happy_AndHealthy,
   SadAndHealthy
}

Will become

GraphQL

enum CatStatus {
    HAPPY_AND_HEALTHY
    SAD_AND_HEALTHY
}

in GraphQL

This will allow us to follow best practices in both worlds.

For example, Elixir Absinthe follows this pattern

Teachability, documentation, adoption, migration strategy

This can be a breaking change, so it can be added as an optional argument to registerEnumType

What is the motivation / use case for changing the behavior?

Enforcing best practices in both GraphQL and Typescript

@kasvith
Copy link
Author

kasvith commented Nov 3, 2021

I will start working on this

@kasvith kasvith changed the title Automatically transforms enums to CAPITALIZED_WITH_UNDERSCORES style [discussion]: Automatically transforms enums to CAPITALIZED_WITH_UNDERSCORES style Nov 3, 2021
@kasvith
Copy link
Author

kasvith commented Nov 3, 2021

So the idea is as follows

Since we do the mapping to internal value representation anyways, it makes no difference representing them in different ways in the schema.

enum Directions{
    Up = 'up',
    Down = 'down',
    Left = 'left',
    Right = 'right'
}

enum DirectionsCapitalized{
    UP = 'up',
    DOWN = 'down',
    LEFT = 'left',
    RIGHT = 'right'
}

console.log('up' === Directions.Up) // true
console.log('up' === DirectionsCapitalized.UP) // true

we can safely map any Enum representation into the correct case respected in graphql world, and use the appropriate value in Typescript world

@kasvith
Copy link
Author

kasvith commented Nov 3, 2021

If we enforce this automatically, it would break only client-side implementations as server-side would remain the same

@kamilmysliwiec
Copy link
Member

Let's track this here #1852

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants