将结果写入存储以供查询时出错。无法读取未定义的属性“查询”

时间:2018-09-25 01:20:30

标签: graphql react-apollo apollo-client

编写查询以存储突变后出现错误。突变有效,我能够读取突变后的查询。当我将相同的查询写入商店缓存时,出现以下错误:

index.js:2178 Error: Error writing result to store for query:
 query ($applicationId: Int) {
  vApplicationApprovalChainList(ApplicationId: $applicationId) {
    id
    approvalOrder
    approverId
    name
    applicationId
    __typename
  }
}

Cannot read property 'vApplicationApprovalChainList' of undefined
    at writeToStore.js:101
    at Array.forEach (<anonymous>)
    at writeSelectionSetToStore (writeToStore.js:97)
    at writeResultToStore (writeToStore.js:75)
    at InMemoryCache../node_modules/apollo-cache-inmemory/lib/inMemoryCache.js.InMemoryCache.write (inMemoryCache.js:99)

这是我的代码。.突变和store.readQuery有效,但是store.writeQuery给出了以上错误。预先感谢您的任何反馈。

APPROVERSLIST_QUERY = gql`
query ($applicationId:Int){
 vApplicationApprovalChainList(ApplicationId:$applicationId){
  id
  approvalOrder
  approverId  
  name
  applicationId  

  }
}

`;


 handleClick() {
        const { row, mutate} = this.props;
        mutate({
            variables: {
                id: row.id
            },
            update: (store, { data: { deleteApprover } }) => {

                const newdata = store.readQuery({
                    query: APPROVERSLIST_QUERY,
                    variables: { applicationId: row.applicationId }
                });
                console.log(newdata);
                newdata.vApplicationApprovalChainList = newdata.vApplicationApprovalChainList.filter(approver => approver.id !== deleteApprover.id);
                store.writeQuery({
                    query: APPROVERSLIST_QUERY, newdata });
                }
        });



    }

1 个答案:

答案 0 :(得分:5)

您没有将新数据传递到writeQuery。传递给writeQuery的对象必须具有名为data的属性,其中包含新数据。此外,由于查询中包含变量,因此您还需要包括该信息。

store.writeQuery({
  query: APPROVERSLIST_QUERY,
  data: newdata,
  variables: {
    applicationId: row.applicationId,
  },
});

有关更多示例和这两种方法的更详尽说明,请参见the official docs