阿波罗客户端3-如何正确实施乐观的响应?

发布于 2025-02-10 07:47:06 字数 1095 浏览 2 评论 0原文

我有一些查询和突变,一个突变更新很大,因此我想在触发突变功能后添加乐观响应。问题是,即使我传递到OpportisticResponse对象,在突变完成作业时也将返回的完整数据,它不会将其添加到缓存中 - 似乎当突变响应准备就绪时,数据已刷新由于具有乐观的响应是否更新UI的时间相同,因此我假设乐观响应不起作用。

我有一些代码示例:

突变:

mutation UpdateList($id: ID, $data: ListData) {
  updateList(id: $id, data: $data) {
    list_id // 1
    name
  }
}

action

const [action] = useMutation(mutation_from_above)
// async body function so await can be used
await action({ variables: { id: 1, data: { name: 'secret name' }}, optimisticResponse: {
    updateList: {
      __typename: 'List',
      list_id: 1,
      name: 'some updated name until data is back'
    }
  })

当然,我已经在高速缓存配置中更新了ID字段的ID字段:

  cache: new InMemoryCache({
    typePolicies: {
      List: {
        keyFields: ['list_id'],
      },
    },
  }),

看起来很简单,但对我来说很简单不起作用。我还检查了API侧,突变的响应与我传递给Opportistisponseponse对象的响应相同。是否有一些重要的一点是为什么它不起作用?有人可以向我解释该怎么做才能完成这项工作吗?

谢谢,欢呼!

I have some queries and mutations and one mutation updates quite big entity so I want to add optimistic response after mutate function is fired. The thing is that even if I pass to optimisticResponse object, full data that will be also returned when mutation completes the job it does not add it to cache - it seems that data is refreshed when mutation response is ready since having optimistic response or not the time of updating UI is the same so I assume optimistic response does not work.

Some code examples I have:

Mutation:

mutation UpdateList($id: ID, $data: ListData) {
  updateList(id: $id, data: $data) {
    list_id // 1
    name
  }
}

action

const [action] = useMutation(mutation_from_above)
// async body function so await can be used
await action({ variables: { id: 1, data: { name: 'secret name' }}, optimisticResponse: {
    updateList: {
      __typename: 'List',
      list_id: 1,
      name: 'some updated name until data is back'
    }
  })

and of course I have updated my id field for typename in the cache config like that:

  cache: new InMemoryCache({
    typePolicies: {
      List: {
        keyFields: ['list_id'],
      },
    },
  }),

and it looks pretty simple but for me it does not work. I also checked on API side and response from mutation is the same I pass to the optimisticResponse object. Is there some important point why it is not working ? Can someone explain me what to do in order to get this work ?

Thanks, cheers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可爱咩 2025-02-17 07:47:06

也许添加此问题可能会像刷新缓存可能更新值,而不是持续存在

update: () => {
  user.refresh();
  // feel free to change the user to your stuff
},

Maybe adding this might solve it like refreshing the cache might update the value and not persist

update: () => {
  user.refresh();
  // feel free to change the user to your stuff
},
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文