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

Prisma generates enums as string while NestJS GraphQL generates as real enum. #12659

Open
killjoy2013 opened this issue Apr 4, 2022 · 12 comments
Labels
kind/improvement An improvement to existing feature and code. team/client Issue for team Client. topic: client types Types in Prisma Client topic: enum "type"/block `enum` topic: NestJS

Comments

@killjoy2013
Copy link

Problem

enums are scalar types in Prisma & generated like this;

export const Continent: {
  Asia: 'Asia',
  Europe: 'Europe',
  America: 'America',
  Africa: 'Africa'
};

export type Continent = (typeof Continent)[keyof typeof Continent]

However, for instance in Nestjs GraphQL, the same enum is generated as enum like this;

export enum Continent {
    Asia = "Asia",
    Europe = "Europe",
    America = "America",
    Africa = "Africa",
    Australia = "Australia"
}

As a result, these two Continents are not assignable, Prisma one is string, GraphQL one is enum. This causes problems is resolvers when we return them.

Suggested solution

In my opinion, Prisma should create enums as a real enum! At least, we sould be able to overwrite the default enum generation in Prisma. (If it's not already available. I hope it is!)

@janpio janpio added kind/feature A request for a new feature. topic: client types Types in Prisma Client team/client Issue for team Client. kind/improvement An improvement to existing feature and code. topic: enum "type"/block `enum` and removed kind/feature A request for a new feature. topic: client types Types in Prisma Client labels Apr 4, 2022
@jeckvld
Copy link

jeckvld commented Apr 14, 2022

have exactly the same issue!

@kokokenada
Copy link

Same issue here. Maybe prisma could also generate an Enum* form to keep everyone happy and maintain compatibility. E.g. add export EnumContinent { ... Alternatively nestjs could update registerEnumType to also take the const form that prisma generates. Until then, I can't seem to find any work around except for duplicating the definition in enum form.

@bulatkhu
Copy link

bulatkhu commented Jul 8, 2022

Completely agree on this!

@pasha-vuiko
Copy link

Same issue for me!

@praisegeek
Copy link

Same issue.

@matteopolak
Copy link

matteopolak commented Dec 12, 2022

Same for MongoDB, it converts enum names to strings instead of scalars, despite the documentation stating that they're scalars.

@janpio
Copy link
Member

janpio commented Dec 12, 2022

Can you point out that part of documentation @matteopolak?

@matteopolak
Copy link

Can you point out that part of documentation @matteopolak?

It looks like the official documentation says scalar (includes string), but in a few GitHub issues like this one says they're stored as integers. However, the generated enum values in prisma-client-js are strings, and they're stored in the database as strings too. It doesn't look like there's a way (at least that I could find) to represent them as integers, unless I define the enums in TypeScript and use an Int field in my Prisma schema.

@janpio
Copy link
Member

janpio commented Dec 12, 2022

Enums being stored as integers is an internal implementation detail of the databases. Prisma just uses the enum implementation of a database usually. "Prisma scalars" in the context of the documentation differentiates them from relations.

This issue is about changing what Prisma Client returns from strings to "real" JS/TS enums as suggested in the original issue description. I don't think the documentation currently defines either behavior.

@sohamnandi77
Copy link

Any update on this?

@vadimoarturo
Copy link

Have exactly the same issue!

Maybe exists solutions?

@ibash
Copy link

ibash commented Jul 31, 2023

If using graphql-codegent setting enumsAsConst can help: https://stackoverflow.com/a/70158253/418739
However if using client-preset then you need to patch the package to allow that option to be set: dotansimha/graphql-code-generator#8562 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/improvement An improvement to existing feature and code. team/client Issue for team Client. topic: client types Types in Prisma Client topic: enum "type"/block `enum` topic: NestJS
Projects
None yet
Development

No branches or pull requests