javascript – Apollo-client(react) – 创建变异更新 – “无法在对象上找到字段基金({})(ROOT_QUERY)”

前端之家收集整理的这篇文章主要介绍了javascript – Apollo-client(react) – 创建变异更新 – “无法在对象上找到字段基金({})(ROOT_QUERY)”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
使用:“react-apollo”:“^ 1.4.3”

在父组件中,我使用GraphQL查询具有子项’fundQuarterlyMetric’的父节点’Fund’.这将以以下格式返回数据:

{      
  id
  name
  ...
  fundQuarterlyMetrics (orderBy: asAtDate_ASC) {
    id
    year
    quarter
    ...
  }
}

当我尝试创建一个新的fundQuarterlyMetrics时,我必须使用更新功能(Apollo Client docs)更新react-apollo上的本地存储.它给了我一个错误

Can't find field Fund({}) on object (ROOT_QUERY) {
     "Fund({\"id\":\"cj57hpfips0x7014414u5tk8m\"})": {
     "type": "id","id": "Fund:cj57hpfips0x7014414u5tk8m","generated": false
    }

问题是,当我在console.log代理时,我可以看到基金和它的孩子们的数据….不知道该怎么做..

更新以下评论

这是父组件数据请求:

export const fundPageQuery = gql`
 query Fund($fundId: ID!) {
Fund(id: $fundId) {
  id
  name
  ....other variables
  fundQuarterlyMetrics (orderBy: asAtDate_ASC) {
    id
    year
    quarter
    ....other variables
  }
}

}
`;

以下是我使用的选项:

var optionsForCreateFundMetric = {
 update: (proxy,{data: {createFundMetrics}}) => {
 try {
  console.log('proxy',proxy);
  const data = proxy.readQuery({query: FundQL.fundPageQuery});
  console.log('data',data);
  data.Fund.fundQuarterlyMetrics.push(createFundMetrics);
  proxy.writeQuery({query: FundQL.fundPageQuery,data})
} catch (e) {
  console.log('error adding to store',e);
}

}
};

export default compose(
 graphql(FundQL.createFundMetrics,{name: 'createFundMetrics',options: 
 optionsForCreateFundMetric}),graphql(FundQL.updateFundMetrics,{name: 'updateFundMetrics'})
 )(FundMetricsForm);

这是我的创建突变:

export const createFundMetrics = gql`
 mutation createFundQuarterlyMetric(
$fundId: ID
$year: Int!
$quarter: FUND_QUARTERLY_METRIC_QUARTER!
$netIRR: Float!
$tvpi: Float!
$rvpi: Float!
$dpi: Float!
$asAtDate: DateTime
$calledThisQuarter: Float!
$distributedThisQuarter: Float!
$cumulativeCalled: Float!
$cumulativeDistributed: Float!
$limitedPartnersNAV: Float!
$quarterlyValuationChangeLCY: Float
$quarterlyTotalReturn: Float
 ) {
createFundQuarterlyMetric(
  fundId: $fundId
  year: $year
  quarter: $quarter
  netIRR: $netIRR
  tvpi: $tvpi
  rvpi: $rvpi
  dpi: $dpi
  asAtDate: $asAtDate
  calledThisQuarter: $calledThisQuarter
  distributedThisQuarter: $distributedThisQuarter
  cumulativeCalled: $cumulativeCalled
  cumulativeDistributed: $cumulativeDistributed
  limitedPartnersNAV: $limitedPartnersNAV
  quarterlyValuationChangeLCY: $quarterlyValuationChangeLCY
  quarterlyTotalReturn: $quarterlyTotalReturn
) {
  id
  year
  quarter
  netIRR
  tvpi
  rvpi
  dpi
  asAtDate
  calledThisQuarter
  distributedThisQuarter
  cumulativeCalled
  cumulativeDistributed
  limitedPartnersNAV
  quarterlyValuationChangeLCY
  quarterlyTotalReturn
}

}
`;


谢谢丹尼尔 – 我不得不退还基金ID以使其正常运行,谢谢!

export default compose(
 graphql(FundQL.createFundMetrics,options: 
optionsForCreateFundMetric,variables: {fundId: 
createFundQuarterlyMetric.fund.id}}),{name: 'updateFundMetrics'})
 )(FundMetricsForm);

解决方法

Apollo文档很难强调这一事实,但是当您调用readQuery从存储中获取先前获取查询时,如果该查询采用任何变量,则需要传入这些相同的变量来检索它.假设变异返回的id是基金的id,你应该能够修改这一行:
const data = proxy.readQuery({query: FundQL.fundPageQuery,variables: { id: createFundMetrics.id } });
原文链接:https://www.f2er.com/js/158280.html

猜你在找的JavaScript相关文章