typegraphql-prisma icon indicating copy to clipboard operation
typegraphql-prisma copied to clipboard

Allow configuring nullability

Open defrex opened this issue 4 years ago • 1 comments

Is your feature request related to a problem? Please describe.

Take the following schema

model User {
  id        Int    @id @default(autoincrement())
  email     String
  firstName String
  posts     Post[]
}

model Post {
  id       Int    @id @default(autoincrement())
  message  String
  author   User   @relation(fields: [authorId], references: [id])
  authorId Int
}

I'd like to create a middleware or decorator that automatically connects currentUser (from context) to the author field of Post in the CreatePostResolver. For example

export const ConnectCurrentUser: MiddlewareFn<Context> = async ({ context }, next) => {
  if (!context.currentUser) {
    throw new ValidationError()
  }
  return {
    connect: {
      id: context.currentUser.id,
    } as UserWhereUniqueInput,
  }
}

applyInputTypesEnhanceMap({
  PostCreateInput: {
    fields: {
      author: [UseMiddleware(ConnectCurrentUser)],
    },
  },
})

This would work fine, however nullable: false on the author field prevents the mutation from working without any input for the field.

Describe the solution you'd like I'd love a way to configure nullability on a per-field basis.

defrex avatar Sep 02 '21 16:09 defrex

Any news about this feature?

alexandre1921 avatar Jul 04 '23 18:07 alexandre1921