prisma-nestjs-graphql icon indicating copy to clipboard operation
prisma-nestjs-graphql copied to clipboard

GraphQLError: Input Object type BrandFollowUpdateManyMutationInput must define one or more fields.

Open idanyekutiel opened this issue 3 years ago • 4 comments
trafficstars

Hi 👋

I have the following model

model BrandFollow {
  id        Int      @id @default(autoincrement())
  /// @HideField({ input: true })
  createdAt DateTime @default(now())
  /// @HideField({ input: true })
  updatedAt DateTime @updatedAt

  /// @HideField()
  user  User  @relation(fields: [userId], references: [id])
  brand Brand @relation(fields: [brandId], references: [id])

  /// @HideField()
  userId  Int
  /// @HideField()
  brandId Int

  @@unique([userId, brandId])
}

which generates

@InputType()
export class BrandFollowUpdateManyMutationInput {
    @HideField()
    createdAt?: InstanceType<typeof DateTimeFieldUpdateOperationsInput>;
    @HideField()
    updatedAt?: InstanceType<typeof DateTimeFieldUpdateOperationsInput>;
}

The error in itself makes sense, but what am I supposed to do? Shouldn't it be better to not generate specific input types if they have no fields?

idanyekutiel avatar Jul 12 '22 20:07 idanyekutiel

  1. If you hide all filed in class, it's maybe better to make an abstract class from model? See https://docs.nestjs.com/graphql/resolvers#generics https://github.com/unlight/prisma-nestjs-graphql#objecttype

  2. You can play with @HideField settings https://github.com/unlight/prisma-nestjs-graphql#hidefield And try to hide field more precisely using match option e.g. @HideField({ match: '@(User|Comment)Create*Input' })

unlight avatar Jul 13 '22 19:07 unlight

Option 1 fixed it, thanks! Super weird tho, I actually tried making the model abstract before submitting the issue and it didn't solve it. Today I deleted the generated file (I use emitSingle), tried again, and now the issue is fixed. Won't complain haha

idanyekutiel avatar Jul 14 '22 12:07 idanyekutiel

Welp, I find myself here again. Getting the same issue. I think the reason it got "fixed" last time was because I wasn't actually using any of the db models that have "no fields defined". Not sure honestly, but here's how it's going down now:

// schema.prisma
/// @ObjectType({ isAbstract: true })
model QualitiesOnProducts {
  id        Int      @id @default(autoincrement())
  /// @HideField({ input: true })
  createdAt DateTime @default(now())
  /// @HideField({ input: true })
  updatedAt DateTime @updatedAt

  quality Quality @relation(fields: [qualityId], references: [id], onDelete: SetNull, onUpdate: NoAction)
  product Product @relation(fields: [productId], references: [id], onDelete: SetNull, onUpdate: NoAction)

  qualityId Int
  productId Int
}
// generated
@InputType()
export class QualitiesOnProductsUpdateManyMutationInput {
    @HideField()
    createdAt?: InstanceType<typeof DateTimeFieldUpdateOperationsInput>;
    @HideField()
    updatedAt?: InstanceType<typeof DateTimeFieldUpdateOperationsInput>;
}
GraphQLError: Input Object type QualitiesOnProductsUpdateManyMutationInput must define one or more fields.

This is happening not only with this input btw, but many others. This is just one of them. A temporary "solution" is unhiding the createdAt field so that the input type has at least one field, but this is not ideal since I need the createdAt and updatedAt not be automatic.

Also, 2 more things:

  • I'm not using the QualitiesOnProductsUpdateManyMutationInput anywhere in my code.
  • I tried doing option 2 like this /// @HideField({ input: true, match: '@(Create|Update)Many*Input' }) but it doesn't seem to affect anything. Maybe I'm using it wrong?

idanyekutiel avatar Aug 06 '22 13:08 idanyekutiel

I am facing a similar issue. Having a unique constraint with all 3 fields hidden:

//schema.prisma
model customerGroupsAssigned {
  customerGroupId Int       @default(0) @db.UnsignedSmallInt
  /// @HideField({ input: true, output: false })
  id              Int       @id @default(autoincrement()) @db.UnsignedInt
  /// @HideField({ input: true, output: false })
  posId           Int        @db.UnsignedTinyInt
  /// @HideField({ input: true, output: false })
  branchId        Int        @db.UnsignedSmallInt

  @@unique([id, posId, branchId], map: "zo_id")
}

Will create the following file: "customer-groups-assigned-id-pos-id-branch-id-compound-unique.input.ts"

@InputType()
export class customerGroupsAssignedIdPosIdBranchIdCompoundUniqueInput {

    @HideField()
    id!: number;

    @HideField()
    posId!: number;

    @HideField()
    branchId!: number;
}

Which results in:

GraphQLError: Input Object type customerGroupsAssignedIdPosIdBranchIdCompoundUniqueInput must define one or more fields.

apss-pohl avatar Jan 17 '23 13:01 apss-pohl