有没有办法从突变更新函数中获取变量?

Is there a way to get the variables from the mutation update function?

使用 Apollo 的 Mutation 组件时,有什么方法可以从更新回调函数中获取 mutation 的变量吗?

<Mutation
  mutation={mutation}
  update={(cache, result) => {
    // is there any way to get the mutation variables here?
  }}
>
  {mutate => <MyComponent onSubmit={mutate} />}
</Mutation>

遗憾的是,该信息未传递给更新函数。您需要将 Mutation 组件移到 MyComponent 内(或将您的状态 向上 并移出 MyComponent),以便您可以通过变量直接到 Mutation 组件,而不是 mutate 函数:

<Mutation
  mutation={mutation}
  variables={{ ... }}
  update={()=> {
    // Now whatever values you used for the mutation will also be available here
  }}
>
{mutate => {
  // mutate can be used without passing any variables to it
}}
</Mutation>

您可能有一个独特的用例,但通常我们首先不需要担心变量。通常,插入、删除和更新 return inserted/deleted/updated 对象和该有效负载足以更新缓存。如果您的变异是 而不是 return 变异数据,并且您有能力更改 API,您可以考虑这样做,因为这也会有所帮助.