发生突变后,用户界面不会使用新添加的项目进行更新,直到页面刷新。我怀疑问题出在突变的更新部分,但我不确定如何进一步排除故障。任何意见非常感谢。乐观的用户界面不更新 - 阿波罗
查询(单独的文件)
//List.js
export const AllItemsQuery = gql`
query AllItemsQuery {
allItems {
id,
name,
type,
room
}
}
`;
突变
import {AllItemsQuery} from './List'
const AddItemWithMutation = graphql(createItemMutation, {
props: ({ownProps, mutate}) => ({
createItem: ({name, type, room}) =>
mutate({
variables: {name, type, room},
optimisticResponse: {
__typename: 'Mutation',
createItem: {
__typename: 'Item',
name,
type,
room
},
},
update: (store, { data: { submitItem } }) => {
// Read the data from the cache for this query.
const data = store.readQuery({ query: AllItemsQuery });
// Add the item from the mutation to the end.
data.allItems.push(submitItem);
// Write the data back to the cache.
store.writeQuery({ query: AllItemsQuery, data });
}
}),
}),
})(AddItem);
感谢您的回答。这是一个好点,但它仍然没有给出预期的结果。使用开发工具插件,我可以看到它看起来不像商店正在更新。我真的不知道我可以在哪里寻找,但建议是受欢迎的。 – muninn9