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

small example with uniform graphql mutation handling optional properties

Open Morriz opened this issue 3 years ago • 8 comments

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": "[email protected]", "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 avatar Jun 14 '22 22:06 Morriz

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.

Morriz avatar Jun 14 '22 23:06 Morriz

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

MichalLytek avatar Jun 14 '22 23:06 MichalLytek

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 avatar Jun 15 '22 12:06 Morriz

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

Morriz avatar Jun 15 '22 12:06 Morriz

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

MichalLytek avatar Jun 15 '22 15:06 MichalLytek

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 avatar Jun 16 '22 07:06 Morriz

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 avatar Jun 16 '22 07:06 Morriz

@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?

Morriz avatar Jun 16 '22 07:06 Morriz