MichalLytek / typegraphql-prisma

Prisma generator to emit TypeGraphQL types and CRUD resolvers from your Prisma schema
https://prisma.typegraphql.com
MIT License
889 stars 113 forks source link

small example with uniform graphql mutation handling optional properties #297

Open Morriz opened 2 years ago

Morriz commented 2 years ago

Describe the issue

Hi, I can't craft a uniform mutation using generated CRUD resolvers that can process optional properties.

Example that does work:

variables (phoneNumber is optional but provided):

{"id": "1", "email": "not@us.ed", "name": "mame", "phoneNumber": "123456789"}

mutation:

mutation UpdateUser(
    $id: String!
    $email: String!
    $name: String!
    $phoneNumber: String
  ) {
    updateUser(
      where: { id: $id }
      data: {
        email: { set: $email }
        name: { set: $name }
        phoneNumber: { set: $phoneNumber }
      }
    ) {
      updatedAt
      name
      email
      phoneNumber
    }
  }

Which sets the phoneNumber field, which is ok.

But when I then leave out the phoneNumber I get this error:

Argument data.phoneNumber of type NullableStringFieldUpdateOperationsInput needs at least one argument.

I would think it is possible to construct a uniform mutation that is idempotent, so we don't have to track what is optional and what is given and then have to adapt our mutation accordingly. (That would suk)

Morriz commented 2 years ago

I know this is related to https://github.com/MichalLytek/typegraphql-prisma/issues/32, but I am specifically asking for an example how to do mutations with your crud resolvers handling non-required fields.

MichalLytek commented 2 years ago

Please search how to use the @if and @skip directives in queries

Morriz commented 2 years ago

Thanks, but I can only find query related docs, nothing about mutations with conditional fields. Just one example would be awesome for all of us

Morriz commented 2 years ago

I get the feeling that crafting proper gql for these resolvers is hard and results in a very proprietary setup that not many people can find information about, or examples of. I do hope that I can sell this to my devs when my PoC is done, but I see an uphill battle

MichalLytek commented 2 years ago

Syntax for those directives is common for queries mutations and subscriptions. Search on the official GraphQL docs.

Morriz commented 2 years ago

I went through their examples, but none of the skip or if directives is used in a mutation. If you know of a link to such documentation, please do share.

Morriz commented 2 years ago

I tried using them in this mutation but get errors:

mutation UpdateUser(
    $id: String!
    $email: String!
    $name: String!
    $phoneNumber: String
  ) {
    updateUser(
      where: { id: $id }
      data: {
        email: { set: $email }
        name: { set: $name }
        phoneNumber: @include(if: $phoneNumber) {
          set: $phoneNumber
        }
      }
    ) {
      updatedAt
      name
      email
      phoneNumber
    }
  }
Morriz commented 2 years ago

@MichalLytek I am seriously considering donating for this example, and I am probably not the only in need of it. How much would do you want for it?