输入对象类型 XXX 必须在 prisma 2.0 中定义一个或多个字段

Input Object type XXX must define one or more fields in prisma 2.0

我有以下 schema.prisma 文件:

model Account {
  id Int @default(autoincrement()) @id
  name String
  transactions Transaction[]
}

model Transaction {
  id Int @default(autoincrement()) @id
  accountId Int
  account Account @relation(fields: [accountId], references: [id])
}

我可以执行

npx prisma migrate save --experimental
npx prisma migrate up --experimental --verbose
npx prisma generate

没有错误,数据库看起来没问题(这张截图也包含货币,但对这个问题没有影响)。

但是当我执行使用 nexusPrismaPluginApolloServer 时出现错误:

Using ts-node version 8.9.0, typescript version 3.8.3
Error: Input Object type TransactionCreateWithoutAccountInput must define one or more fields.
    at assertValidSchema (/home/daniel/pro/cash/core/node_modules/graphql/type/validate.js:71:11)
    at assertValidExecutionArguments (/home/daniel/pro/cash/core/node_modules/graphql/execution/execute.js:136:35)
    at executeImpl (/home/daniel/pro/cash/core/node_modules/graphql/execution/execute.js:86:3)
    at Object.execute (/home/daniel/pro/cash/core/node_modules/graphql/execution/execute.js:64:63)
    at Object.generateSchemaHash (/home/daniel/pro/cash/core/node_modules/apollo-server-core/src/utils/schemaHash.ts:11:18)
    at ApolloServer.generateSchemaDerivedData (/home/daniel/pro/cash/core/node_modules/apollo-server-core/src/ApolloServer.ts:541:24)
    at new ApolloServerBase (/home/daniel/pro/cash/core/node_modules/apollo-server-core/src/ApolloServer.ts:400:32)
    at new ApolloServer (/home/daniel/pro/cash/core/node_modules/apollo-server-express/src/ApolloServer.ts:88:5)
    at new ApolloServer (/home/daniel/pro/cash/core/node_modules/apollo-server/src/index.ts:36:5)
    at Object.<anonymous> (/home/daniel/pro/cash/core/src/server.ts:5:1)
[ERROR] 01:01:32 Error: Input Object type TransactionCreateWithoutAccountInput must define one or more fields.

我的代码不包含与交易相关的任何内容。如果我删除 Transaction 一切正常。

在生成的代码中,我可以在文件中看到:

node_modules/@prisma/client/index.d.ts

export type TransactionCreateWithoutAccountInput = {

}

...

export type TransactionCreateManyWithoutAccountInput = {
  create?: Enumerable<TransactionCreateWithoutAccountInput> | null
  connect?: Enumerable<TransactionWhereUniqueInput> | null
}

...

export type TransactionUpdateManyWithoutAccountInput = {
  create?: Enumerable<TransactionCreateWithoutAccountInput> | null
  connect?: Enumerable<TransactionWhereUniqueInput> | null
  set?: Enumerable<TransactionWhereUniqueInput> | null
  disconnect?: Enumerable<TransactionWhereUniqueInput> | null
  delete?: Enumerable<TransactionWhereUniqueInput> | null
  update?: Enumerable<TransactionUpdateWithWhereUniqueWithoutAccountInput> | null
  updateMany?: Enumerable<TransactionUpdateManyWithWhereNestedInput> | null
  deleteMany?: Enumerable<TransactionScalarWhereInput> | null
}

如何解决?

我已经设置了与上面相同的 schema.prisma,并且我使用 Nexus 以下列方式创建了两种类型

import { objectType } from '@nexus/schema'

export const Account = objectType({
  name: 'Account',
  definition(t) {
    t.model.id()
    t.model.name()
    t.model.transactions({
      pagination: true,
    })
  },
})

export const Transaction = objectType({
  name: 'Transaction',
  definition(t) {
    t.model.id()
    t.model.account()
  },
})

我已经启动了服务器,目前 运行 正常。除了这个你还添加了其他的queries/mutations吗?

此错误与 Prisma 2 中的未解决问题有关。

释义:

问题似乎是由于您的交易模型除了与账户的关系外没有任何字段。出于某种原因,这导致 prisma 无法生成 Typescript 中所需的某些输入类型(导致 tsc 失败)。

有趣的是,如果您只是将一个虚拟标量场添加到您的模型,一切都会正常工作 - 似乎是 typegen 系统中的某种错误。

我在使用 typegraphql-prisma 和架构时遇到了同样的问题:

model Applications {
  id          Int  @id @default(autoincrement())
  applyingFor Job  @relation(fields: [jobId], references: [id])
  jobId       Int
  applicant   Profile @relation(fields: [applicantId], references: [id])
  applicantId Int 
}`

添加一个可选的虚拟字段:

dummy String?

使它 运行 没有问题

https://github.com/graphql-nexus/nexus-plugin-prisma/issues/648