2

我有以下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

没有错误,数据库看起来还可以(此屏幕截图还包含货币,但对这个问题没有影响)。

在此处输入图像描述

但是当我执行时ApolloServer使用nexusPrismaPlugin我有错误:

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
}

如何解决?

4

2 回答 2

3

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

释义:

该问题似乎是由于您的 Transaction 模型除了与 Account 的关系之外没有任何字段引起的。出于某种原因,这导致 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

于 2021-03-11T19:24:59.173 回答
1

我已经设置了与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()
  },
})

我已经启动了服务器,目前它运行正常。除此之外,您是否添加了任何其他查询/突变?

于 2020-04-27T10:38:01.443 回答