MichalLytek / typegraphql-prisma

Prisma generator to emit TypeGraphQL types and CRUD resolvers from your Prisma schema
https://prisma.typegraphql.com
MIT License
887 stars 113 forks source link

Using String field for id with @default(uuid()) doesn't omit the id field in the create input #189

Closed alainfonhof closed 2 years ago

alainfonhof commented 2 years ago

Describe the Bug When defining a model:

model User {
 id String @id @default(uuid())
 label String
}

This will result in:

input UserCreateInput {
 id String
 label String
}

While in the example in the docs Hiding Prisma model field in GraphQL schema it shows the id field is omitted. When defining a User model with id Int @id @default(autoincrement()) it does seem to result in the expected behaviour.

Environment:

MichalLytek commented 2 years ago

Please check UserCreateInput type from Prisma Client, e.g. when you call prisma.user.create().

typegraphql-prisma only translates Prisma DMMF into classes, it doesn't add or hide properties when it's autoincrement or uuid.

alainfonhof commented 2 years ago

Maybe I don't understand it correctly but when I have the following prisma.schema:

generator typegraphql {
  provider = "typegraphql-prisma"
  output   = "../prisma/generated/type-graphql"
}

model UserA {
  id    Int    @id @default(autoincrement())
  label String
}

model UserB {
  id    String @id @default(uuid())
  label String
}

After running npx prisma generate it results in the following types in the prisma-client:

export type UserACreateInput = {
 label: string
 }
export type UserACreateManyInput = {
 id?: number
 label: string
 }
export type UserBCreateInput = {
 id?: string
 label: string
 }
export type UserBCreateManyInput = {
 id?: string
 label: string
 }

The type UserACreateInput has id omitted while for UserB this isn't the case, right? It also seems to have inconsistency between UserACreateInput and UserACreateManyInput.

MichalLytek commented 2 years ago

It's a correct behaviour for normal create and create many.

But yes, it looks like a Prisma issue that id still exist for the default uuid id string field. Please report on theirs repo.

alainfonhof commented 2 years ago

What would be the best approach to hide the id field from certain input types like UserCreateManyInput, UserCreateInput and UserUpdateInput? The doc line /// @TypeGraphQL.omit(input: true) will not work because it also omits the id field from UserWhereUniqueInput. I have been looking into Additional decorators for CRUD resolvers and Prisma classes and fields and Omit but it is not clear to me if this is the right approach. How would I go about hiding the id field for certain input types without editing the generated type-graphl?

MichalLytek commented 2 years ago

It's not possible for now.

MichalLytek commented 2 years ago

@alainfonhof Have you reported the bug with id field in create input on Prisma repo?

ShawkiS commented 2 years ago

hey!! any updates on this?