Confusing errors on db.patch calls inside mutation
Hi, I'm making some changes to handle a new webhook event and started seeing errors when my mutation is called. The errors complain about missing required fields when calling db.patch:
However, I was under the impression that all fields are optional on db.patch (it will update only the received ones), as is hinted by the method signature expecting the "partial" object:
and so a "required field" validation against the schema doesn't really make sense.
However, this is behaving inconsistently, as shown in the attached screenshots. As it was working properly before today, I suspect that the real culprit might be the multiple mutations running in parallel, since I also got this error once:
So why am I seeing the schema validation errors? Are these two related? I'll work to fix the concurrency issue regardless, but it took me a while to get the concurrency error so if it is the real cause it would have been better to have seen this first. Am I missing anything? I will appreciate any help.
2 Replies
patch
has an interesting behavior: if a field is missing from the argument, it is left as-is. but if a field exists with value undefined
, the field is removed from the document. so if you want a field to be ignored, you should delete
it from the patch argument, instead of setting it to undefined
i.e. {foo: "abc", status: undefined}
is different from {foo: "abc"}
when used as an argument to patchOne interesting thing I learned about js recently, in order to conditionally add a field:
The second one surprised me - you can spread in
null
and it will no-op. However you can't spread null
into an array:
So probably best to spread in {}
which is also more understandable / self-documenting