-
Hello, I've been debugging an issue with one of my queries not updating correctly after a mutation change. I am using urql with React, the graphcache exchange and optimistic updates. I have a form that allows editing of customers. Each customer can have an arbitrary number of contacts. Each contact can have an arbitrary number of emails. They are all separate entities with an ID. I use a single mutation field to update all entities at once. It works like an upsert. Entities that are sent with an ID are updated, those without one are inserted. In addition to that, we also use optimistic updates. And this is where my problems start. First, the query is not updated and doesn't return the new values after the mutation result is applied. The optimistic update and the mutation result do cause a render. I can see though, that the cache has updated its internal data structure. By disabling optimistic updates, the problem is fixed. Second, the query not updating only happens under certain conditions. If I add a new contact to an existing customer, the update doesn't work. If I add a new email to an existing contact it does work. Third, once the query fails to update it will never update again. Even if I trigger a different mutation that changes a customer field, it won't update the view. I can see that the cached values have changed. It seems that the query gets disconnected from the cache. Here is the diff of the optimistic result (left) and the actual response (right). Note, that I am using temporary ids for the entities that will be created. And here an example of adding a new email (which updates the query). Left: optimistic result, right: actual result I also tried manually invalidating the cache of the mentioned query. Even then, the query doesn't update but again, I'm able to see that the changes have been applied to the cache. I'm at a point where I don't understand why this happens and why it only happens in certain cases. How can I solve this and keep the optimistic updates? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
So this does sound a little odd. So your changes have been applied but the hook isn't reflecting that? Are you using the React bindings? If you're using suspense have you upgraded them recently? |
Beta Was this translation helpful? Give feedback.
-
I got it working now. Though the behavior is still strange. While looking at my query and the mutation I noticed that I had one field in the query that was not present in the mutation. Here's the full query and full mutation as a reference. query CustomerDetails($id: bigint!) {
customerByPk(id: $id) {
id
name
state
notes
updatedAt
contacts {
id
name
isPrimary
customerId
emails {
id
email
isPrimary
}
phones {
id
phoneNumber
isPrimary
}
}
}
} mutation UpdateCustomerDetails(
$object: customers_insert_input = {}
$on_conflict: customers_on_conflict
) {
insertCustomer(object: $object, on_conflict: $on_conflict) {
id
name
notes
updatedAt
contacts {
id
name
isPrimary
emails {
id
email
isPrimary
}
phones {
id
phoneNumber
isPrimary
}
}
}
} Notice, that the query requests the So, the solution in this case was to simply remove It still strikes me as odd, that the same response breaks when using optimistic updates, but works when returned from a mutation. Also, if I omit any customer fields from the optimistic updates (e.g notes) the optimistic update still works when adding new contacts. |
Beta Was this translation helpful? Give feedback.
So this does sound a little odd. So your changes have been applied but the hook isn't reflecting that? Are you using the React bindings? If you're using suspense have you upgraded them recently?