gpt4 book ai didi

javascript - Apollo-client (react) - 创建突变的更新 - "Can' t 在对象 (ROOT_QUERY) 上找到字段 Fund({})"

转载 作者:可可西里 更新时间:2023-11-01 02:35:32 24 4
gpt4 key购买 nike

使用:“react-apollo”:“^1.4.3”

在父组件中,我使用 GraphQL 查询父节点“Fund”和子节点“fundQuarterlyMetric”。这将返回以下格式的数据:

{      
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 时,我可以看到 Fund 及其数据下的子项......不知道该怎么做......

更新以下评论:

父组件数据请求如下:

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, {name: 'createFundMetrics', options:
optionsForCreateFundMetric, variables: {fundId:
createFundQuarterlyMetric.fund.id}}),
graphql(FundQL.updateFundMetrics, {name: 'updateFundMetrics'})
)(FundMetricsForm);

最佳答案

Apollo 文档在强调这一事实方面做得很差,但是当您调用 readQuery 以从存储中获取先前获取的查询时,如果该查询采用任何变量,则需要传递这些变量相同的变量来检索它。假设 mutation 返回的 id 是基金的 id,你应该可以只修改这一行:

const data = proxy.readQuery({
query: FundQL.fundPageQuery,
variables: { id: createFundMetrics.id },
});

关于javascript - Apollo-client (react) - 创建突变的更新 - "Can' t 在对象 (ROOT_QUERY) 上找到字段 Fund({})",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45534453/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com