【发布时间】:2018-08-17 14:21:15
【问题描述】:
我想在突变后使用乐观的 UI 更新:https://www.apollographql.com/docs/react/basics/mutations.html
我对“optimisticResponse”和“update”之间的关系感到困惑。
这里使用了optimisticResponse:
const CommentPageWithData = graphql(submitComment, {
props: ({ ownProps, mutate }) => ({
submit: ({ repoFullName, commentContent }) => mutate({
variables: { repoFullName, commentContent },
optimisticResponse: {
__typename: 'Mutation',
submitComment: {
__typename: 'Comment',
// Note that we can access the props of the container at `ownProps` if we
// need that information to compute the optimistic response
postedBy: ownProps.currentUser,
createdAt: +new Date,
content: commentContent,
},
},
}),
}),
})(CommentPage);
仅使用此更新商店吗?
然后文档说这是用来更新缓存的:
const text = 'Hello, world!';
client.mutate({
mutation: TodoCreateMutation,
variables: {
text,
},
update: (proxy, { data: { createTodo } }) => {
// Read the data from our cache for this query.
const data = proxy.readQuery({ query: TodoAppQuery });
// Add our todo from the mutation to the end.
data.todos.push(createTodo);
// Write our data back to the cache.
proxy.writeQuery({ query: TodoAppQuery, data });
},
});
这是我在不使用optimisticResponse 函数的情况下成功更新UI 的方法。
两者有什么区别?你应该使用两者还是只使用一个?
【问题讨论】:
标签: apollo-client