發生突變後,用戶界面不會使用新添加的項目進行更新,直到頁面刷新。我懷疑問題出在突變的更新部分,但我不確定如何進一步排除故障。任何意見非常感謝。樂觀的用戶界面不更新 - 阿波羅
查詢(單獨的文件)
//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