【发布时间】:2019-01-02 22:41:27
【问题描述】:
所以我的 React Apollo 应用程序上有一个表单,可以添加或更新用户。接收数据时,如果用户存在,Apollo 会知道并正确合并/规范化数据。
如果不是,它什么也不做,我需要像这样手动附加它:
const result = await this.props.upsertPersonMutation({
variables: {
...this.state.fields,
},
update: (store, { data }) => {
// somewhere in here I'll need to check if it's new or not
const initDataQuery = store.readQuery({query: INIT_DATA});
const { upsertPersonMutation } = data;
initDataQuery.getInitData.user.people.push(upsertPersonMutation)
store.writeQuery({query: INIT_DATA, data: initDataQuery})
}
}).then(response => {
this.setState({
isLoading: false
});
this.props.closeCallback();
})
虽然它能够做到前者给我留下了深刻的印象,但为什么它不能将新条目附加到数组中?我觉得如果它只解决了一半的问题,你知道吗?
【问题讨论】:
标签: graphql apollo react-apollo graphql-js