简体   繁体   English

我们如何结合来自多个React组件的Relay中的突变?

[英]How do we combine mutations in Relay from multiple React components?

I am new to Relay and I am currently trying to use mutations. 我是Relay的新手,目前正在尝试使用突变。 In my use case, I have a form made up of several React components and I want to capture the changes/mutation of each component, combine them in the parent component and then commit the changes to GraphQL server. 在我的用例中,我有一个由几个React组件组成的表单,我想捕获每个组件的更改/突变,将它们组合在父组件中,然后将更改提交到GraphQL服务器。 How can I do this? 我怎样才能做到这一点?

The examples that I have seen so far all deal with the mutation being used and committed in a single component. 到目前为止,我所看到的示例都涉及在单个组件中使用和提交的突变。 I want to use the same pattern that is used for querying where fragments are localised within the react component and then they are combined to create a query for server. 我想使用与查询片段相同的模式,其中片段位于react组件内,然后将它们组合以创建服务器查询。

I had asked the same question on GitHub where @yachaka and @josephsavona answered. 我在GitHub上问了同样的问题,@yachaka和@josephsavona回答了。
Here is @josephsavona's answer:- 这是@josephsavona的答案:-

...a straightforward approach would be to accumulate all of the changes from child components in a parent component (using callbacks and local state or something like Redux) and then make a single mutation when the user saves/commits the changes. ...一种直接的方法是从父组件中的子组件中累积所有更改(使用回调和本地状态或Redux之类的东西),然后在用户保存/提交更改时进行单个更改。
One pattern is to use applyUpdate to optimistically apply each individual change, then roll all those optimistic mutations back when applying the final mutation. 一种模式是使用applyUpdate乐观地应用每个单独的更改,然后在应用最终突变时回滚所有那些乐观突变。

See https://github.com/facebook/relay/issues/1461#issuecomment-264662371 for the full discussion. 有关完整讨论,请参见https://github.com/facebook/relay/issues/1461#issuecomment-264662371

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM