开发者问题收集

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

2018-09-25
3923

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

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个回答

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

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

请参阅 官方文档 ,获取更多示例和对这两种方法的更详尽解释。

Daniel Rearden
2018-09-25