Can I Read Each Response From Batched Mutations? [Apollo UseMutation]
Let's say I have defined a mutation like this:
const [deleteItem, {data}] = useMutation(deleteItemMutation)
and then call deleteItem(id) repeatedly inside a loop, is there a way I can read each {data}
response? The mutations are batched, and the response is an array (I can see that by inspecting the network traffic). However, the {data}
variable only contains the response from the last batched mutation. This makes sense, of course, but I'd still be interested in the results from all of them. Any idea how I could do this?
I'm considering wrapping the mutation in another function that would take a callback, and I think that would solve the problem. But is there an easier solution?
Answer
The mutate
function returned by the hook returns a Promise that will resolve with the mutation result. So while the data
prop is returned by the hook as a convenience, there's no need to use necessarily use it.
const items = ['a', 'b', 'c']
const results = await Promise.all(items.map(async (itemId) => {
const { data } = await deleteItem({ variables: { itemId } })
return data
}))
console.log(results[0].data)
Related Questions
- → Import statement and Babel
- → should I choose reactjs+f7 or f7+vue.js?
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → .tsx webpack compile fails: Unexpected token <
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → React Native with visual studio 2015 IDE
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
- → How do I determine if a new ReactJS session and/or Browser session has started?
- → Alt @decorators in React-Native
- → How to dynamically add class to parent div of focused input field?