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

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

我是 Relay 的新手,我目前正在尝试使用突变。在我的用例中,我有一个由多个 React 组件组成的表单,我想捕获每个组件的 changes/mutation,将它们组合到父组件中,然后将更改提交给 GraphQL 服务器。我该怎么做?

到目前为止,我看到的示例都涉及在单个组件中使用和提交的变更。我想使用与查询片段在反应组件中的本地化位置相同的模式,然后将它们组合起来为服务器创建查询。

我在 GitHub 上问过同样的问题,@yachaka 和 @josephsavona 回答了这个问题。
这是@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.
One pattern is to use applyUpdate to optimistically apply each individual change, then roll all those optimistic mutations back when applying the final mutation.

有关完整讨论,请参阅 https://github.com/facebook/relay/issues/1461#issuecomment-264662371