'GraphQL: how to avoid values overwrites for unspecified fields when doing an update via a mutation

Let me explain:

I am using Gson as deserializer, MongoDB as storage and the newly Spring GraphQL

Let's say I have an object O in database with the fields:

A = 6
B = null

If the frontend send an update on the B field so I receive a JSON with only the B field with its value, so I receive O with:

B = 1

Then after deserialization, in Java my object O will have the following fields:

A = null
B = 1

Then, in MongoDB it will save:

A = null
B = 1

So the value of A will be overwritten by null, and we don't want that, the frontend only wanted to update the field B.

How do you solve that gracefully?



Solution 1:[1]

There is no support for "patchable entities" right now in Spring GraphQL. While the null vs. "empty" values for a field are accessible in the JSON map directly, binding that to a Java object and tying that up with the data Repository story is a bigger challenge.

This is being discussed in this Spring GraphQL issue.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Brian Clozel