Apollo GraphQL:如何在现有数组中的正确索引处插入变异对象?

时间:2016-12-27 18:01:52

标签: graphql apollostack

我有一个包含以下内容的subscriptionObserver:

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
);

1 个答案:

答案 0 :(得分: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;