relay icon indicating copy to clipboard operation
relay copied to clipboard

Deffered mutations

Open Efimster opened this issue 10 years ago • 5 comments

Consider the case:

  1. Creating new Cart. (Cart is Relay component)
  2. Adding/removing items to/from cart 500 times. (Item is Relay component. Item is edge node of Cart connection)

I want ALL these changes performed locally on client side and be synchronous with server only on special signal (button click event "SAVE").

Creating item locally and binding to Item component props cause warning. I believe because it is suppose to be Relay generated data and probably be a part of Cart connection.

What you guys suggest to do in that case?

Efimster avatar Feb 13 '16 09:02 Efimster

This is actually one of the things RelayMutationTransaction was designed for. You can create a mutation with an optimistic update and use Relay.Store.applyUpdate to apply it locally, and use the returned RelayMutationTransaction instance to later commit to server or rollback altogether.

voideanvalue avatar Feb 13 '16 16:02 voideanvalue

@voideanvalue It looks like RelayMutationTransaction does allow you to defer the server request for each item until you actually issue a commit, but I don't see a way to batch transactions into a single commit. Without the batching we would still be sending a server request for each commit since each RelayMutationTransaction is bound to a single ClientMutationId. Maybe batching into a single server request is not possible?

jurassix avatar Feb 13 '16 19:02 jurassix

@jurassix: Yeah, it doesn't support batching. But here's some alternate suggestions:

(i) Perform batching through a custom network layer. (ii) Track all the uncommitted transactions. Instead of committing them all separately at the end, rollback all of them and then create and commit a new one that represents all of them and commit that. (iii) Similar to (ii), before applying a mutation, rollback the previous one (if there was a previous one) and actually apply a mutation that represents both the previous one and the new one you were about to apply.

voideanvalue avatar Feb 14 '16 08:02 voideanvalue

@voideanvalue Yeah I was leaning towards ii as a solution as well. Thx for confirming.

I guess I need to build this out as an example to see the complexity involved. It seems that if I'm an item then I will not have context of other items, so I would need to move the transaction mgmt to the parent since it would provide visibility to all children items. Then from the parent I could map over my children, using getPendingTransacitons API, rollback the individual transactions, and consolidate those into a single mutation. There are still some open questions here I'll have to figure out during implementation, like actual knowing what changed from each child, and translating that into a single transaction.

This does seems like something Relay could provide; managing individual transactions and consolidating to a single transaction seems like a lot of responsibility for a React Component. I'll wait to see how it works in practice tough before requesting a change to the Framework ;)

jurassix avatar Feb 14 '16 17:02 jurassix

Agreed that this is a lot of complexity for the client to manage, and also that we should gain some experience building this type of batching out by hand before trying to abstract it into something baked in in the framework. So I'm going to close this for now and ask that you open a new issue with a concrete proposal or even a pull request at some point in the future if you think it still makes sense. Thanks for your input!

wincent avatar Feb 16 '16 19:02 wincent