Vue-apollo 从另一个组件重新获取查询

问题描述

我想知道是否可以从一个组件重新获取查询,以便在不刷新组件本身的情况下在另一个组件中更新它。突变后,我看到 apollo 缓存已更新,但我没有看到其他组件中的数据已经“活动”了。 我的全球阿波罗提取政策是:

watchQuery: {
  fetchPolicy: 'cache-and-network'
}

解决方法

我发现,我需要直接使用 update writeQuery 我的缓存。因为 refetchQuery 为您提供了一个新对象,但是如果您希望它变得活泼,则需要对您的 apollo 缓存中的现有对象进行变异。一个代码示例是:

  .mutate({
    mutation: CREATE_USER,variables: { ...user },update(store,{ data: { createUser } }) {
      const data = store.readQuery({ query: LIST_USERS })
      data.users.items.push(createUser)
      store.writeQuery({ query: LIST_USERS,data })
    }
  })

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...