删除项突变传递了错误的 ID
--- 更新:我自己找到了解决方案,在下面发布了答案 ---
对于我的 React Typescript 项目,使用 NestJS 和 Apollo GraphQL,我创建了一个删除项突变。效果很好,它删除了 MongoDB 数据库中正确的项目。
仅当我想在 Mutation 后更新 Apollo 缓存而不重新获取数据时,由于某种原因,错误的 id 会从 Mutation 回调传递到更新函数。它似乎是数组最后一项的 ID。
我用 id 62161412424edb0e850ce6a2 创建了一个示例,并在代码中的该点添加了带有 id 值的注释。
有谁知道为什么会发生这种情况以及如何解决它?如果有更好的方法在删除后更新缓存,并从数据库中重新获取数据,我也想知道。
GraphQL 突变:
const DELETE_ITEM_BY_ID = gql`
mutation deleteItemById ($id: ID!) {
deleteItemById (
input: {
id: $id
}
) {
id
}
}
`;
Apollo 挂钩:
const [mutationDeleteItemById] = useMutation(DELETE_ITEM_BY_ID, {update: updateCache});
删除按钮,项目 ID 正确:
<button onClick={deleteItem(item.id)}>Delete</button> {/* // 62161412424edb0e850ce6a2 */}
删除函数,项目 ID 正确:
const deleteItem = (itemId:any) => (event: any) => {
console.log(itemId); // 62161412424edb0e850ce6a2
mutationDeleteItemById({
variables: {
id: itemId,
}});
}
UpdateCache 函数,项目 ID 不正确。是否来自其他项目的 ID:
const updateCache = (cache:any, data: any) => {
console.log(data.data.deleteItemById.id); // 621611da424edb0e850ce69c - INCORRECT
};
--- Update: Found the solution for this myself, posted an answer below ---
For my React Typescript project, with NestJS and Apollo GraphQL, I created a delete item mutation. That works fine, it deletes the correct item in the MongoDB database.
Only when I want to update the Apollo cache after the Mutation, without refetching the data, for some reason the wrong id get passed from the Mutation callback to the update function. It seem to be de id of last item of the array.
I made an example with id 62161412424edb0e850ce6a2 and added comments with the value of the id at that point in the code.
Does anyone know why this happens and how to fix it? If there are better methods to update the cache after a delete, with refetching data from the database, I would like to know that too.
The GraphQL mutation:
const DELETE_ITEM_BY_ID = gql`
mutation deleteItemById ($id: ID!) {
deleteItemById (
input: {
id: $id
}
) {
id
}
}
`;
The Apollo hook:
const [mutationDeleteItemById] = useMutation(DELETE_ITEM_BY_ID, {update: updateCache});
The delete button, item ID is correct:
<button onClick={deleteItem(item.id)}>Delete</button> {/* // 62161412424edb0e850ce6a2 */}
The delete function, item ID is correct:
const deleteItem = (itemId:any) => (event: any) => {
console.log(itemId); // 62161412424edb0e850ce6a2
mutationDeleteItemById({
variables: {
id: itemId,
}});
}
The UpdateCache function, item ID is incorrect. Is ID from other item:
const updateCache = (cache:any, data: any) => {
console.log(data.data.deleteItemById.id); // 621611da424edb0e850ce69c - INCORRECT
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我刚刚自己找到了答案。从 MongoDB 删除时,NestJs 服务中的 id 属性需要为“_id”。认为“id”也被接受,但被忽略,没有引发错误,但数据库中的第一项被删除。
I just found the answer myself. When deleting from the MongoDB the id property needed to be '_id' in the NestJs service. Thought 'id' was also accepted but that got ignored, did not raise an error, but the first item in the database gets deleted.