【发布时间】:2019-01-12 12:51:54
【问题描述】:
我正在尝试将图表组件包装在检索图表数据的<Query> 组件中。图表组件使用Query 的fetchMore 渲染道具通过更新查询的变量将新数据附加到图表。
我遇到的问题是,在使用更新的变量参数运行 fetchMore 之后,Query 组件的渲染道具似乎永远不会更新 variables 参数。
这是react-apollo 的错误,还是我错误地使用了fetchMore?
这是图表的渲染函数:
render() {
return (
<Query
query={gql`
query Chart($from: Long!, $to: Long!) {
chartData(from: $from, to: $to) @connection(key: "chartData") {
...
}
}
`}
variables={{ from: this.props.from, to: this.props.to }}
>
{({ data: { chartData } = {}, variables: { from, to }, fetchMore }) => {
const fetchNext = () =>
fetchMore({
variables: { from: from + 30, to: to + 30, },
updateQuery: (previousResult, { fetchMoreResult }) => {
const updatedResult = Object.assign({}, fetchMoreResult)
updatedResult.chartData = [
...previousResult.chartData,
...fetchMoreResult.chartData,
]
return updatedResult
},
})
return (
<Chart
chartData={chartData}
from={from}
to={to}
fetchNext={fetchNext}
/>
)
}}
</Query>
)
}
这是我正在经历的步骤:
- render prop 使用
from = 0、to = 29和chartData作为 30 个数据点的数组运行 - 触发 fetchMore。使用
from = 30、to = 59再次运行查询。响应返回 30 个新数据点。 updatedResult 是完整的 60 个数据点。 - 渲染道具再次运行。
chartData是一个包含 60 个数据点的数组,正如预期的那样,但from = 0和to = 29仍然如此。我希望它们分别更新到30和59。这会导致图表的 xMin 和 xMax 错误,因为变量没有更新。
作为一种解决方法,我尝试在updateQuery 回调中设置from 和to 的组件状态副本,但这会导致from 和to 的值与chartData 不同步直到 Apollo 完成对其商店的更新。
非常感谢任何帮助。谢谢!
【问题讨论】:
标签: reactjs graphql react-apollo apollo-client