prisma-nestjs-graphql icon indicating copy to clipboard operation
prisma-nestjs-graphql copied to clipboard

Wrong WhereUniqueInput with previewFeature extendedWhereUnique

Open vskult opened this issue 2 years ago • 5 comments

Problem

Considering the following Prisma schema file:

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["extendedWhereUnique"]
}

generator nestgraphql {
  provider        = "node node_modules/prisma-nestjs-graphql"
  previewFeatures = ["extendedWhereUnique"]
  output          = "../src/_generated"
  reExport        = Directories
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model X {
  id                 String             @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  name          String             
}

I noticed there is an issue with the generated XWhereUniqueInput (X being any Prisma model). All fields are generated as optional Typescript fields by the lib but the PrismaClient actually generates this:

export type XWhereUniqueInput = Prisma.AtLeast<{
    id?: string
    AND?: Enumerable<NotificationWhereInput>
    OR?: Enumerable<NotificationWhereInput>
    NOT?: Enumerable<NotificationWhereInput>
    name?: string
  }, "id">

So the id appears optional but the MappedType Prisma.AtLeast<{}, "id"> ensures that the id is not optional.

Expected behaviour

I would expect the prisma-nestjs-graphql generated XWhereUniqueInput to be identical to the one of the Prisma client.

Temporary workaround

I manually modified the generated file like so:

@InputType()
export class XWhereUniqueInput {

    @Field(() => String, {nullable:true})
    id: string;

    ...
}

vskult avatar May 05 '23 07:05 vskult

I am also experiencing this issue.

Zoot01 avatar May 13 '23 14:05 Zoot01

Did not check yet. But how XWhereUniqueInput is look like? And what behind the Prisma.AtLeast, to what it resolves?

unlight avatar May 14 '23 20:05 unlight

Here is a copy of the AtLeast type provided by Prisma:

  // this type assumes the passed object is entirely optional
  type AtLeast<O extends object, K extends string> = NoExpand<
    O extends unknown
    ? | (K extends keyof O ? { [P in K]: O[P] } & O : O)
      | {[P in keyof O as P extends K ? K : never]-?: O[P]} & O
    : never>;

Basically, it expects that at least the key provided in second params are provided otherwise an Error is thrown.

Here is what is generated by your generator:

@InputType()
export class XWhereUniqueInput {

    @Field(() => String, {nullable:true})
    id?: string;

    ...
}

So here the id field is optional because of id?: string; when at least the id should be specified, so it should not be optional (as in my workaround).

vskult avatar May 15 '23 09:05 vskult

@vskult Are saying that id should be graphql nullable, but non-nullable in typescript?

unlight avatar May 15 '23 12:05 unlight

@unlight I admit I'm not even sure if the graphQL id should be nullable or not.

What I can state for sure (hence the issue) is that the Typescript type should be non-nullable because now Typescript types generated via @prisma/client do not match those generated by prisma-nestjs-graphql when using the preview feature extendedWhereUnique causing a Typescript error.

vskult avatar May 15 '23 13:05 vskult