我在使用graphQL apollo中的变异时遇到了麻烦 . 当页面加载时,它将运行查询 lectureResponseQuery
,如果查询== null,则运行突变 fetchLectureResponseMutation
以创建新文档 . 此突变返回新结果,我对查询进行了更新,我希望组件将使用新数据重新呈现,但事实并非如此 . 有谁知道那是为什么?谢谢!
@graphql(fetchLectureResponseMutation, {
options: ownProps => ({
variables: { lectureName: ownProps.match.params.lectureName },
update: (proxy, { data: { fetchLectureResponse } }) => {
const data = proxy.readQuery({
query: lectureResponseQuery,
variables: { lectureName: ownProps.match.params.lectureName },
});
data.lectureResponse = fetchLectureResponse;
proxy.writeQuery({
query: lectureResponseQuery,
data,
});
},
}),
name: 'fetchLectureResponse',
})
@graphql(lectureResponseQuery, {
options: ownProps => ({
variables: { lectureName: ownProps.match.params.lectureName },
}),
})
class LecturePage extends React.PureComponent {
componentWillUpdate(nextProps) {
if (nextProps.data.lectureResponse === null) {
this.props.fetchLectureResponse();
}
}
render() {
const { data } = this.props;
if (data.loading || data.lectureResponse === null) {
return <Loading />;
}
return <LectureLayout lectureResponse={data.lectureResponse} />
}
}
1 回答
对于任何未来调查此问题的人来说 - 核心问题是我想要进行查找或创建操作 . 当查询只返回新对象(如果它不存在)时,这会更好用,因为那时你只进行1个后端调用,这意味着你不必同步查询和变异之间的时间 .
TLDR:使用查询进行findOrCreate操作!