Apollo GraphQL:如何在现有数组中的正确索引处插入变异对象?
Apollo GraphQL: How to Insert Mutated Object at Correct Index in Existing Array?
我有一个 subscriptionObserver,包括以下内容:
return update(
previousResult,
{
ApptsForCurrentUser: {
$push: [newAppt],
},
}
);
通过订阅到达的新项目需要按日期排序的顺序插入 ApptsForCurrentUser 数组。这是一个多维数组,我可以使用 $apply
函数对其进行排序。
在将数组交给$apply
函数对其进行排序之前,$push newAppt
数组是否有语法?
或者,我应该这样做吗?
(尚未测试):
var newResult = clonedeep(previousResult); //lodash
newResult.push(newAppt);
newResult.sort(myCustomSortFunction);
const newResultAsAConst = clonedeep(newResult);
return update(
previousResult, newResultAsAConst
);
根据@Sacha 的建议,我能够通过以下代码解决此问题:
import update from 'immutability-helper';
[.....]
const resultWithNewApptAdded = update(previousResult, {getAllApptsForCurrentUser: {$push: [newAppt]}});
//getAllApptsForCurrentUser contains unsorted list of appts
resultWithNewApptAdded.getAllApptsForCurrentUser = resultWithNewApptAdded.getAllApptsForCurrentUser.sort(mySortFunction);
return resultWithNewApptAdded;
我有一个 subscriptionObserver,包括以下内容:
return update(
previousResult,
{
ApptsForCurrentUser: {
$push: [newAppt],
},
}
);
通过订阅到达的新项目需要按日期排序的顺序插入 ApptsForCurrentUser 数组。这是一个多维数组,我可以使用 $apply
函数对其进行排序。
在将数组交给$apply
函数对其进行排序之前,$push newAppt
数组是否有语法?
或者,我应该这样做吗?
(尚未测试):
var newResult = clonedeep(previousResult); //lodash
newResult.push(newAppt);
newResult.sort(myCustomSortFunction);
const newResultAsAConst = clonedeep(newResult);
return update(
previousResult, newResultAsAConst
);
根据@Sacha 的建议,我能够通过以下代码解决此问题:
import update from 'immutability-helper';
[.....]
const resultWithNewApptAdded = update(previousResult, {getAllApptsForCurrentUser: {$push: [newAppt]}});
//getAllApptsForCurrentUser contains unsorted list of appts
resultWithNewApptAdded.getAllApptsForCurrentUser = resultWithNewApptAdded.getAllApptsForCurrentUser.sort(mySortFunction);
return resultWithNewApptAdded;