阿波罗客户端3-如何正确实施乐观的响应?
我有一些查询和突变,一个突变更新很大,因此我想在触发突变功能后添加乐观响应
。问题是,即使我传递到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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许添加此问题可能会像刷新缓存可能更新值,而不是持续存在
Maybe adding this might solve it like refreshing the cache might update the value and not persist