【问题标题】:Updating Apollo store when mutating list filter变异列表过滤器时更新 Apollo 存储
【发布时间】:2017-10-28 19:34:42
【问题描述】:

我有一个如下所示的 graphql 架构:

type User {
    entries(status: EntryStatus): [Entry]
}

type Mutation {    
    updateEntry(status: EntryStatus): Entry
}

我可以按状态过滤条目列表(这是一个枚举),我可以更新条目的状态。我想在状态更新时更新商店,以便条目出现在正确的列表中(entries(status: FOO)entries(status: BAR))。

我知道我可以使用 update 方法更新商店。

const withMutation = graphql(updateEntryMutation, {
  props: ({ mutate }) => ({
    updateEntry: updateEntryInput =>
      mutate({
        variables: { updateEntryInput },
        update: (proxy, newData) => {

          // update data here
          // Which would involve removing the entry from its previous filtered "list", and adding it to the one with the new status

      })
  })
});

但是由于我无法访问 update 的旧数据(以前的 entry.status),我怎么知道要从哪个列表中删除条目?

(除了按状态枚举所有列表并删除更新的条目,如果我发现它......)

【问题讨论】:

    标签: javascript graphql apollo react-apollo


    【解决方案1】:

    您需要先store.readQuery(),然后将新数据写入存储。

    有点像这样:

    const EntriesQuery = 'yourQueryHere';
    
    const withMutation = graphql(updateEntryMutation, {
      props: ({ mutate }) => ({
        updateEntry: updateEntryInput =>
          mutate({
            variables: { updateEntryInput },
            update: (store, { data: { updateEntry }}) => {
    
              const data = store.readQuery({ query: EntriesQuery });
              data.entries.push(updateEntry)
    
              store.writeQuery({ query: EntriesQuery, data })
          })
      })
    });
    

    【讨论】:

      猜你喜欢
      • 2019-11-26
      • 2019-06-28
      • 2018-11-02
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 2019-03-23
      • 2018-06-13
      相关资源
      最近更新 更多