HOC包装器:
const CreateAssignmentDialogWithData = compose(
graphql(CreateAssignmentMutation, {
props: props => ({
onAddAssignment: (
title,
description,
assignDate,
dueDate,
classId,
unitId,
startDate,
endDate
) =>
props.mutate({
variables: {
title: title,
description: description || "No description",
assignDate: assignDate,
dueDate: dueDate,
classId: classId,
unitId: unitId
}
})
}),
options: ({ startDate, endDate, classId }) => ({
update: (proxy, { data: { putAssignment } }) => {
const data = proxy.readQuery({
query: ClassFeedQuery,
variables: {
startDate: startDate,
endDate: endDate,
classId: classId
}
});
data.getAssignmentsForClassRange.push(putAssignment);
proxy.writeQuery({ query: ClassFeedQuery, data });
}
})
}),
变异起作用,Assignment
在数据库中创建,但不更新UI。使用Apollo Devtools,我可以看到商店有两个getAssignmentsForClassRange
条目。正在显示的那个(缺少新创建的作业)是
getAssignmentsForClassRange({
"startDate":"2018-04-16T07:00:00.000Z",
"endDate":"2018-04-23T06:59:59.999Z",
"classId":"226b64c4-e585-4703-a446-4515921c94f2"
}): [Assignment]
包含新作业的作品显示为
getAssignmentsForClassRange({}): [Assignment]
这里缺少的参数对我来说没有多大意义,因为它仍然会检索正确的分配(包括新的分配)。我如何让我的update
直接将新作业附加到查询而不是创建这个新作业?或者还有另一种方法可以实现实时UI更新吗?