我正在尝试乐观地更新 UI,并为此使用 props 和 update 函数。但是我收到一条警告,例如缺少字段“_id”、“所有者”、“元”和“评论”。我还附上了警告屏幕截图。如何解决以下问题?
这是代码
const withMutations = graphql(CommentMutation, {
options: () => ({
...getDefaultOptions('collaboration')
}),
props: ({ ownProps, mutate }) => {
return {
submitComment: ({ value }) =>
mutate({
variables: {
commentData: {
comment: value
}
},
optimisticResponse: {
__typename: 'Mutation',
addComment: {
__typename: 'CommonResponse',
_id : -1,
id : null,
comment : value,
meta : ownProps.data[0].meta,
owner : ownProps.data[0].owner,
status : 'success',
message : 'Comment successfully added.',
action : 'AddComment',
},
},
// optimisticResponse: {
// __typename: 'Mutation',
// addComment: {
// __typename: 'CommonResponse',
// id : -1,
// status : 'success',
// message : 'Comment successfully added.',
// action : 'AddComment',
// },
// },
update: (store, { data }) => {
const data2 = store.readQuery({ query: CommentsQuery });
data2.comments.unshift(data.addComment);
store.writeQuery({ query: CommentsQuery, data: data2 });
},
}),
};
},
});
我这里有完整的代码
https://gist.github.com/SanskarSans/907063ed9a27d3f38f7de25ca4072faf
最佳答案
好吧,你必须传递一些东西。例如,只需使用 ShortId 生成临时 ID。当服务器的数据到达时,乐观数据就会回滚,所以这不是问题。如果您还不知道注释并且已正确地将 prop 声明为可选,则只需在乐观响应中将其声明为 null 即可。
关于javascript - 乐观地更新 UI 时缺少字段 '_id' 、 'owner' 、 'meta' 和 'comment',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48316417/