unlight/prisma-nestjs-graphql

Wrong WhereUniqueInput with previewFeature extendedWhereUnique

vskult opened this issue · 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;

    ...
}

I am also experiencing this issue.

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

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 Are saying that id should be graphql nullable, but non-nullable in typescript?

@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.