Search code examples
prismaprisma-graphqlnexus-prisma

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


I have the following schema.prisma file:

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])
}

I can execute

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

without errors and database looks ok (this screenshot contains also currency but it does not have influence on this problem).

enter image description here

But when I execute ApolloServer that is using nexusPrismaPlugin I have error:

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.

My code does contains nothing connected with Transaction. If I remove Transaction everything works great.

In generated code I can see in file:

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
}

How to fix it?


Solution

  • This error is related to an open issue in Prisma 2.

    paraphrase:

    The issue seems to be caused by your Transaction model not having any fields other than its relations to Account. For some reason, this is causing prisma to not generate some of the Input types that are required in Typescript (causing tsc to fail).

    What's interesting is if you just add a dummy scalar field to your model, everything works fine - seems like some kind of bug in the typegen system.

    I had the same issue using typegraphql-prisma with schema that was as such:

    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 
    }`
    

    adding an optional dummy field:

    dummy String?
    

    causes it to run without issue

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