Hi, new question about the migration to v4 (react).
I have some places when i use updateQuery, e.g.
updateQuery((state) => ({
...state,
myField: [
...state.myField,
{
// new element
},
],
}))
state is now a DeepPartialObject and I’m getting type errors for things not being iterable, being undefined, etc…
Any hint on how to handle this? At the moment I would prefer to not refactor it using update in useMutation or similar methods.
Is the merge still shallow (so I need to spread the inner fields), or handled differently?
That first argument unfortunately always could be incomplete, but it was never reflected in the types.
You can be safer by using the second argument:
updateQuery((_, {complete, previousData}) => {
if (complete) {
// in here, `previousData` has the same shape that your `state` previously had
} else {
// you never handled this case before, as it was hard to detect in AC3 - this will need some new logic
}
}
Thank you
So if complete is false, previousData is a partial object or undefined?
In the former case I could just spread the partial previousData by accessing fields with optional chaining and it should be equivalent to the previous implementation, correct?
If complete is false, it could be pretty much anything, undefined (which will show as {} though, not undefined), shallow partial, or even deep partial - you don’t have a guarantee that it’s “shallow complete”, you could also have overlapping data from another query in the cache that has queried for different fields on the same entities, maybe with nuanced changes nested deeper.