Prisma1: Prisma client - generate enum types

Created on 6 Nov 2018  路  7Comments  路  Source: prisma/prisma1

Is your feature request related to a problem? Please describe.
Prisma client should take advantage of TypeScript's enums instead of just using type

Describe the solution you'd like
I would like to make codegen module generate TypeScript enums from GraphQL's enums, instead of types.

Advantages:

  1. More obvious;
  2. Utilizes TypeScript language features;
  3. Improves compatibility with other libraries related to code generation.
kindiscussion rf1-draft areclients

Most helpful comment

Any progress on this? The difference in the way Prisma generates the enum (string unions) versus my GraphQL client's way (typescript enum) causes conflicts in the types.

All 7 comments

I am going to submit a PR with this change

PR: #3438

Thanks a lot for opening this issue and suggesting this change. This is a little bit tricky since the enum TS syntax and the currently supported string literal syntax are not compatible (this is by design - see this Github issue).

Your proposed change would result in a breaking change which would in most cases require a more explicit syntax. Consider the following example based on the giving datamodel.prisma snippet:

Datamodel:

enum Color { Red, Blue }

Resulting TS client types using literal string syntax (current implementation):

type Color = 'Red' | 'Blue'

// allows for the following
const c1 = 'Red' // ok
const c2 = 'Green' // compile error

Resulting TS client types using enum syntax (your suggestion):

enum Color {
  Red: 'Red',
  Blue: 'Blue'
}

// allows for the following
const c1 = 'Red' // compile error
const c2 = 'Green' // compile error
const c3 = Color.Red // ok

There is however a workaround described in the comments of the mentioned issue which would allow both the 'Red' and the Color.Red syntax. See the following:

export type Color = 'Red' | 'Blue'
export const Color: {
  Red: 'Red',
  Blue: 'Blue',
} = {
  Red: 'Red',
  Blue: 'Blue',
}

// allows for the following
const c1 = 'Red' // ok
const c2 = 'Green' // compile error
const c3 = Color.Red // ok

While my suggested workaround wouldn't use the canonical enum syntax you've described, it would still allow for both referencing syntaxes. Does this solve your requirements?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 10 days if no further activity occurs. Thank you for your contributions.

Any progress on this? The difference in the way Prisma generates the enum (string unions) versus my GraphQL client's way (typescript enum) causes conflicts in the types.

I would like to throw weight behind switching to canonical enum syntax here. I personally only see the explicitness as a positive especially since enum values should ideally be seen as opaque. This change would also help the generated types follow a favourite POLA! :)

Any progress on this? The difference in the way Prisma generates the enum (string unions) versus my GraphQL client's way (typescript enum) causes conflicts in the types.

I fixed the incompatibility between GraphQL Code Generator enums and prisma client enums by setting enumAsTypes:true in codegen.yml. See here for more details.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

notrab picture notrab  路  3Comments

ragnorc picture ragnorc  路  3Comments

thomaswright picture thomaswright  路  3Comments

nikolasburk picture nikolasburk  路  3Comments

marktani picture marktani  路  3Comments