-
Notifications
You must be signed in to change notification settings - Fork 787
How to determine the mutation loading state? #421
Comments
I think you have it pretty much right and the issue is more a lack of documentation about it. It doesn't really make sense to include loading state on the mutation container; if you think about it you could call the mutation twice simultaneously -- which loading state should get passed down to the child? My feeling is in general it's not nice to mix imperative ( Maybe there's a library that could help reducing the boilerplate? |
@tmeasday thanks for the response. I ended up writing a higher order component to wrap my components that contain mutations, see https://gist.github.com/ctavan/7219a3eca42f96a5c5f755319690bda7 This allows me to reduce the code duplication to a minimum. |
I also wrote a HOC to handle this issue. I hope it can help. https://github.com/lhz516/react-apollo-mutation-state |
Just for reference, I created a hoc for this as well, which is a bit more transparent:
This passes down the properties: mutateLoading,mutateError,mutateResult. Actually it will use the name of the mutate property in the config, i.e. Usage: just call hoc:
|
@tkvw, I really like this HOC solution. I found in my implementation that if we wish to chain on the original mutation's promise elsewhere in our code, we need to add a So the revision looks like this, for anyone else who may find it helpful:
Which restores our ability to work imperatively with the side-effect, should you wish to do so:
|
I also found it useful to re-throw the error in the
Otherwise the
|
Thank you all for the workarounds! But maybe this issue can be solved in Apollo after all? Judging by the number of comments and workarounds here I think this issue is still interesting to some people. @tmeasday Your rationale back in the day for closing it was:
Interestingly, we now have the loading state as part of the Maybe this issue can be reopened and the HoC can provide |
Seems reasonable to me although I am not working on this package at the moment so it is not up to me 🤷🏼♂️ |
Here is my TypeScript solution:
And usage example:
|
This is essentially a re-post of http://stackoverflow.com/questions/39379943/how-to-determine-mutation-loading-state-with-react-apollo-graphql
I am wondering if there is an idiomatic way of determining the
loading
state ornetworkStatus
of a mutation. It's obvious how this is done for queries but unlike queries I cannot find a built-in loading state indication for mutations.What I have been doing so far is set the components
state
in the handler that callsthis.props.mutate()
and then reset that state in the result- and error-callbacks ofmutate()
. However this adds a lot of repetitive code to any component that triggers a mutation.I also reckon that optimistic updates are often more suitable than indicating a loading state for mutations, however I think there are still enough cases where this is not reasonably possible. While it's probably best for something like a blog comment or chat message to be handled in an optimistic fashion I think there are still many mutations where you cannot reasonably update the UI without feedback from the server, e.g. when a user wants to log into your application or for any transaction that involves payments…
So I wonder: What is the recommended way of handling mutation loading state with react-apollo?
Version
The text was updated successfully, but these errors were encountered: