这是一个有效的 Apollo 订阅处理程序:
componentDidMount() {
const fromID = Meteor.userId();
const {toID} = this.props;
this.toID = toID;
this.props.data.subscribeToMore({
document: IM_SUBSCRIPTION_QUERY,
variables: {
fromID: fromID,
toID: toID,
},
updateQuery: (prev, {subscriptionData}) => {
if (!subscriptionData.data) {
return prev;
}
const newIM = subscriptionData.data.IMAdded;
// don't double add the message
if (isDuplicateIM(newIM, prev.instant_message)) {
return previousResult;
}
return update(prev, {
instant_message: {
$push: [newIM],
},
});
}
});
}
instant_message
是要显示的对象数组。每个对象都包含一个日期字段。我需要按日期对这个数组中的对象进行排序。
这种方法曾经适用于 Apollo 的 beta 版本:
//update returns a new "immutable" list
const instant_message = update(previousResult, {instant_message: {$push: [newAppt]}});
instant_message.instant_message = instant_message.instant_message.sort(sortIMsByDateHelper);
return instant_message;
render
我可以对数组进行排序,但是 Apollo 会在返回的对象中抛出一个错误——例如,当例程需要它时,它在 props 中找不到。
从返回排序数组的正确方法是什么updateQuery
?提前感谢所有人提供的任何信息。