【发布时间】:2018-01-14 00:35:18
【问题描述】:
使用:“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
}
} `;
解决方案 谢谢 Daniel - 我必须返回基金 ID 才能使其正常工作,所以谢谢!
export default compose(
graphql(FundQL.createFundMetrics, {name: 'createFundMetrics', options:
optionsForCreateFundMetric, variables: {fundId:
createFundQuarterlyMetric.fund.id}}),
graphql(FundQL.updateFundMetrics, {name: 'updateFundMetrics'})
)(FundMetricsForm);
【问题讨论】:
-
听起来您通过 Apollo 执行突变的方式存在问题 - 如果您正在处理突变,您不应该看到有关根查询的错误。请更新您的问题以包含您的代码,特别是在您定义 graphql HOC 和任何相关逻辑的地方(例如您对
update的调用)。 -
感谢丹的评论。我已将代码添加到问题中
标签: javascript reactjs graphql react-apollo apollo-client