2016-12-27 53 views
0

我有一个subscriptionObserver包括以下内容:Apollo GraphQL:如何在现有数组中的正确索引处插入突变对象?

return update(
    previousResult, 
    { 
     ApptsForCurrentUser: { 
      $push: [newAppt], 
     }, 
    } 
); 

到达通过订阅需要插入日期排序顺序ApptsForCurrentUser阵列的新项目。这是一个多维数组,我可以使用$apply函数对其进行排序。

是否有语法$push newAppt到阵列之前交给阵列关闭的$apply函数,将排序呢?

另外,我应该这样做吗?

(尚未测试):

var newResult = clonedeep(previousResult); //lodash 
newResult.push(newAppt); 
newResult.sort(myCustomSortFunction); 
const newResultAsAConst = clonedeep(newResult); 
return update(
    previousResult, newResultAsAConst 
); 
+0

这是我在做什么(推后排序),使用https://github.com/kofrasa/mingo。 – Sacha

+0

你可以发布推送的行然后排序,作为我可以接受的答案吗? TIA! – VikR

回答

0

大厦从@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;