Weakky/ra-data-opencrud

Optimistic updates of references not working

Opened this issue · 3 comments

In the prisma e-commerce example, if you go to the product list screen, edit the first product, change the brand and hit save, you'll see in the list view that the brand shows the old value until 3 seconds later when the new data is refreshed and you see the updated value.

The same appears to be true anytime you update a reference using the opencrud data provider -- but if you switch to one of the rest data providers, the optimistic update works as expected. I haven't had time to fully investigate yet, but it looks like the use of the derived source.id field is somehow screwing up the optimistic update system -- if I hack it to use source_id instead, it fixes it...

The problem does seem to be with the extra source.id field.

The Product returned from the data loader has both a brand member containing the full Brand object and a brand.id field containing just the linked id. When the optimistic update fires, the brand.id has been updated to the new value, but the brand object still refers to the old value.

screen shot 2018-09-12 at 10 26 11 am

If I comment out https://github.com/Weakky/ra-data-opencrud/blob/master/src/getResponseParser.js#L36 so the source.id field never gets created, it appears to fix the problem, but I'm not sure of the broader implications of that change...

Hey there,

Thanks for this issue! You're right about optimistic updates not properly working with reference.

However, your suggested fix doesn't work for me. I tried updating the brand of a product and it doesn't work either when removing the field brand.id.

To be honest, these generated field.id are part of the legacy from ra-data-graphcool, and I'm not sure yet either what are the usecases.

The n-to-many fields also have generated fields named fieldIds, but those are useful when using <ReferenceArrayInput />, because the source is expected to be an array of id.

I'm gonna dig further to understand what's going on.

EDIT: It doesn't work either with <ReferenceArrayInput /> fields.

Thanks for taking a look, and for the overall project -- other than this one issue, it's been working great!

I see that the code is the same in ra-data-graphql-simple and ra-data-graphcool - wonder if it's worth asking over there why the dual representations of the data. Also, I wonder if it would simplify to just make the return value look more like the REST version and map the embedded object to brand_id instead of brand.id and brand. You'd have to update any Reference components to use "brand_id" as the source instead of "brand.id", but at least it would be guaranteed to work as long as the REST version works...