问题描述
我在我的状态中存储了一堆信息,我需要使用突变传递到GraphQl Server,但是我需要在调用下一个突变之前使用每个突变的结果,因为我需要:
- 在我的数据库中创建一个新对象
- 使用该对象生成的ID来创建另一个对象
- 修改原始对象以存储第二个对象生成的ID
我注意到Apollo突变组件具有响应的回调,但是我不知道如何使用此回调来解散另一个突变,或者是否正确解决了我的问题.我还研究了批处理突变以一次发送它们,但似乎也不是解决方案.任何帮助将不胜感激
推荐答案
正如您提到的,解决所有突变的独特方法是查看相关问题!
实际上,组成它们并不是那么坏,您可以做这样的事情:
const handleClick = async (mutation1Fn, mutation2Fn, mutation3Fn) => { const data1 = await mutation1Fn() const data2 = await mutation2Fn() const data3 = await mutation3Fn() } const Mutations = () => ( <Composer components={[ <Mutation mutation={mutation1} />, <Mutation mutation={mutation2} />, <Mutation mutation={mutation3} /> ]} > {([mutation1Fn, mutation2Fn, mutation3Fn]) => ( <button onClick={() => handleClick(mutation1Fn, mutation2Fn, mutation3Fn)} > exec! </button> )} </Composer> )
让我知道您是否正在挣扎!
其他推荐答案
您要做的就是根据所传递的数据嵌套这些突变.
onCompleted和onError Props可以在您的情况下使用,它们也可以访问新数据结果.但是我个人认为嵌套格式更可读性,以后更容易调试.
这将是:
const Mutations = () => ( <Mutation mutation={m1}> {(mutation1, { loading, error, data }) => ( if (loading) return `Loading...` if (error) return `Error...` const id = get(data, 'result.id') return ( <Mutation mutation={m2} variables={id} /> {(mutation2, { loading, error, data }) => ( if (loading) return `Loading...` if (error) return `Error...` (...) )} </Mutation> )} </Mutation> )
问题描述
I have a bunch of information stored in my state that I need to pass to my graphQL server using mutations, but I need to use the results of each mutation before I call the next one since I need to:
- Create a new object in my database
- Use the id generated for that object to create another object
- Modify the original object to store the id generated by the second object
I noticed that apollo Mutation components have an onCompleted callback, but I don't know how to use this callback to fire off another mutation or whether it's the right way solve my problem. I also looked into batching my mutations to send them all at once but it doesn't seem like that is the solution either. Any help would be appreciated
推荐答案
As you've mentioned, the unique way to solve it is batching all the mutations, check out the issue related!
Actually, it's not that bad to compose them, you could do something like this:
const handleClick = async (mutation1Fn, mutation2Fn, mutation3Fn) => { const data1 = await mutation1Fn() const data2 = await mutation2Fn() const data3 = await mutation3Fn() } const Mutations = () => ( <Composer components={[ <Mutation mutation={mutation1} />, <Mutation mutation={mutation2} />, <Mutation mutation={mutation3} /> ]} > {([mutation1Fn, mutation2Fn, mutation3Fn]) => ( <button onClick={() => handleClick(mutation1Fn, mutation2Fn, mutation3Fn)} > exec! </button> )} </Composer> )
Let me know if you're struggling with something!
其他推荐答案
All you need to do is nest these mutations depending on the data being passed.
The onCompleted and onError props can be used in your case, and they have access to the new data result too. But I personally think a nested format is more readable and easier to debug later.
It would be something like:
const Mutations = () => ( <Mutation mutation={m1}> {(mutation1, { loading, error, data }) => ( if (loading) return `Loading...` if (error) return `Error...` const id = get(data, 'result.id') return ( <Mutation mutation={m2} variables={id} /> {(mutation2, { loading, error, data }) => ( if (loading) return `Loading...` if (error) return `Error...` (...) )} </Mutation> )} </Mutation> )