- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在突变后使用乐观 UI 更新:https://www.apollographql.com/docs/react/basics/mutations.html
我对“乐观响应”和“更新”之间的关系感到困惑。
这里使用了乐观响应:
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 });
},
});
最佳答案
为了扩展其他两个答案,区别在于您正在“更新”的内容是否已经存在于缓存中。
根据 docs , 如果您正在更新现有项目,例如编辑待办事项的标题,您只需要 optimisticResponse
.为什么?因为缓存包含节点,您只需要告诉它新节点发生了新的事情,这会立即反射(reflect)在 UI 上。
optimisticResponse
just provides an 'immediate' result data from a mutation.
update
归因于突变,您正在控制缓存的状态。
update
takes place ofrefetchQueries
, which means you are in control of the cache state.
update
您可以访问缓存并专门修改/附加您需要的节点,而不是重新获取整个数据层次结构。但是,您仍在等待 Mutation 完成。如果您提供
update
旁边
optimisticResponse
,您正在提供一个即时的假设响应,并将其提供给您的个人
update
函数,然后立即更新缓存。
update
让您也可以劫持它,并在客户端进行。
Final Note: you are assuming the server is always responding without errors. Error handling elsewhere will still work, but you might get into UI inconsistencies if you are frequently catching errors (say
isLoggedIn
scenarios) so def make sure that the queries you 'fast track' are typically healthy.
关于apollo-client - Apollo Client 中的乐观响应与更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49186523/
我正在编写一个 Web 应用程序,两个不同的用户可以在其中更新事物列表,例如待办事项列表。我已经意识到,乐观锁定机制效果最好,因为我不希望出现高争用情况。 我一直在查看事务隔离级别,现在我有点困惑。看
这个问题在这里已经有了答案: Pessimistic versus Optimistic Concurrency (Locking versus Feedback) (3 个答案) 关闭 8 年前。
我是一名优秀的程序员,十分优秀!