英文:
Multiple optimistic updates in RTK Query mutation
问题
以下是翻译好的部分:
什么是在创建文档变更(createDocument mutation)之后,对单个文档(getDocument)和多个文档(getDocuments)进行乐观更新的推荐方法?
这是我的简化尝试:
documentApi
- getDocument(id)
- getDocuments()
- createDocument(id)
- onQueryStarted
- temporaryDocument = makeDocument()
- dispatch getDocument(id) updateQueryData { Object.assign(draft, temporaryDocument) }
- dispatch getDocuments updateQueryData { documents.push(temporaryDocument) }
- onQueryStarted
如果这是正确的方法,那么我一定做错了些什么,因为dispatch getDocument 没有起作用。
更新 getDocuments 可以正常工作。
也许我忽略了一些与标签(tags)的交互?我是否过于复杂化了事情?
英文:
What's the recommended way to optimistically update the singular (getDocument) and plural (getDocuments) after a createDocument mutation?
Here's my simplified attempt:
documentApi
- getDocument(id)
- getDocuments()
- createDocument(id)
- onQueryStarted
- temporaryDocument = makeDocument()
- dispatch getDocument(id) updateQueryData { Object.assign(draft, temporaryDocument) }
- dispatch getDocuments updateQueryData { documents.push(temporaryDocument) }
- onQueryStarted
If this is the right way, I must be doing something wrong, because the dispatch getDocument didn't work.
Updating getDocuments works fine.
Maybe there's some interaction with tags I'm missing? Am I overcomplicating things?
答案1
得分: 1
关于api.util.updateQueryData
的多次调用,你所描述的情况应该没问题。
之所以在getDocuments
上使用updateQueryData
有效,但在getDocument(id)
上无效,是因为updateQueryData
只能修改现有的缓存条目,不能创建新的缓存条目。所以你可以修改现有的列表数据以添加新项,但不能修改id
对应的文档详情,因为当前正在创建它,并且之前没有使用相同的id
调用过getDocument(id)
。
在getDocument(id)
端点上,你需要使用upsertQueryData
(需要v1.9.0+版本)而不是updateQueryData
,以为新文档添加一个新的缓存条目。upsertQueryData
工具允许你插入一个以前不存在的缓存键的新缓存条目,或者覆盖现有版本。
在getDocuments
端点上,你可以继续使用updateQueryData
,因为在这种情况下,你正在修改现有数据。
dispatch(api.util.upsertQueryData('getDocument', id, temporaryDocument));
dispatch(api.util.updateQueryData('getDocuments', undefined, (draft) => {
draft.push(temporaryDocument);
});
英文:
What you are describing re: multiple dispatches of api.util.updateQueryData
should be fine.
The reason that using updateQueryData
works on getDocuments
but not on getDocument(id)
is that updateQueryData
can only modify pre-existing cache entries. It cannot create new cache entries. So you can modify your existing list data to add another item, but you cannot modify the document details for this id
because there are no document details -- you are creating it right now and have not previously called getDocument(id)
with this id
previously.
You need to use upsertQueryData
(requires v1.9.0+) instead of updateQueryData
on your getDocument(id)
endpoint in order to add a new cache entry for the new document. The upsertQueryData
util allows you to insert a new cache entry for a cache key which did not previously exist, or to overwrite an existing version.
You'll keep updateQueryData
on the getDocuments
endpoint because in that case you are modifying the existing data.
dispatch(api.util.upsertQueryData('getDocument', id, temporaryDocument));
dispatch(api.util.updateQueryData('getDocuments', undefined, (draft) => {
draft.push(temporaryDocument);
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论