typegraphql-prisma
typegraphql-prisma copied to clipboard
small example with uniform graphql mutation handling optional properties
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)
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.
Please search how to use the @if and @skip directives in queries
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
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
Syntax for those directives is common for queries mutations and subscriptions. Search on the official GraphQL docs.
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.
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
}
}
@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?