graphql-nexus / nexus-plugin-prisma

Deprecated
MIT License
828 stars 118 forks source link

Adding any updateOneXXX mutation breaks nexus schema validation #847

Open sfratini opened 4 years ago

sfratini commented 4 years ago

Nexus Report

The report is breaking:

TypeError: Object.fromEntries is not a function

Screenshot

Package.json

  "dependencies": {
    "cors": "2.8.5",
    "nexus": "0.26.1",
    "nexus-plugin-prisma": "0.17.0",
    "serverless-http": "2.5.0"
  },
  "devDependencies": {
    "@types/cors": "2.8.7",
    "serverless-offline": "6.5.0",
    "serverless-plugin-typescript": "1.1.9"
  }

Mutations:

import {schema} from 'nexus'

schema.mutationType({
    definition(t) {
      t.crud.createOneCategory()
      //t.crud.updateOneCategory() //This one will triger the issue

      t.crud.createOneCompany()
      //t.crud.updateOneCompany()  //This one will triger the issue

      t.crud.createOneCollection()

      t.crud.createOneUser()
      t.crud.updateOneUser()  //This one will triger the issue

    }
  })

Error on nexus dev:

✕ nexus:schema Input Object type CompanyUserUpdateManyDataInput must define one or more fields.

Input Object type PoiCategoryUpdateManyDataInput must define one or more fields.

Schema Prisma:

model CompanyUser {
  id         Int     @default(autoincrement()) @id
  company_id Int
  user_id    Int
  Company    Company @relation(fields: [company_id], references: [id])
  User       User    @relation(fields: [user_id], references: [id])

  @@unique([company_id, user_id], name: "unique_company_user")
}

model PoiCategory {
  id          Int      @default(autoincrement()) @id
  poi_id      Int
  category_id Int
  Category    Category @relation(fields: [category_id], references: [id])
  Poi         Poi      @relation(fields: [poi_id], references: [id])
}

Prisma client with an empty array of fields for this object:

{\"name\":\"CompanyUserUpdateManyDataInput\",\"isOneOf\":false,\"fields\":[]}

Description

Basically, I cannot add none of the opdateOne methods, because it will complain that the ManyDataInput are empty. This does not happen with all of them, as some have fields (other XXXManyDataInput objects).

I am unable to determine why this models are not being generated.

Any information I can attach, let me know.

bkiac commented 4 years ago

I have a similar issue with relation tables. This is my Prisma schema:

model User {
  id        Int    @default(autoincrement()) @id
  email     String @unique

  relationship1 Relationship[] @relation("Relationship_userId1ToUser")
  relationship2 Relationship[] @relation("Relationship_userId2ToUser")
}

model Relationship {
  userId1 Int
  userId2 Int

  user1 User @relation("Relationship_userId1ToUser", fields: [userId1], references: [id])
  user2 User @relation("Relationship_userId2ToUser", fields: [userId2], references: [id])

  @@id([userId1, userId2])
}

These are my generated Prisma client types:

export type UserWhereUniqueInput = {
  id?: number
  email?: string
}

export type UserCreateOneWithoutRelationship1Input = {
  create?: UserCreateWithoutRelationship1Input
  connect?: UserWhereUniqueInput
}

export type UserCreateOneWithoutRelationship2Input = {
  create?: UserCreateWithoutRelationship2Input
  connect?: UserWhereUniqueInput
}

export type RelationshipCreateInput = {
  user1: UserCreateOneWithoutRelationship1Input
  user2: UserCreateOneWithoutRelationship2Input
}

It seems like Nexus validation fails when RelationshipCreateInput has no other fields except relational inputs. If I add a empty: String? field to my User schema, then the generated Prisma type will be:

export type RelationshipCreateInput = {
  empty?: string | null
  user1: UserCreateOneWithoutRelationship1Input
  user2: UserCreateOneWithoutRelationship2Input
}

and Nexus validation passes.

sfratini commented 4 years ago

I can also add this happens if you add any of the upsert methods. In my case, I cannot find any combination that would make the validation pass. It has become a blocking issue for me.