我有一组来自 graphql 查询的“帖子”,我正在尝试使用apollo docs中描述的 offset + limit 进行分页。当前按下“loadMore”会更新查询,结果fetchMoreResult
是正确的。此外,连接newResults
时会正确显示。也许我做出了一个不正确的假设,但我理解文档的方式是新结果posts
将自动传递给我TestComponent
并重新渲染。这是不正确的吗?
import React from 'react';
import { View, Text } from 'react-native';
import { graphql } from 'react-apollo';
import { GetPosts } from 'app/graphql';
class TestComponent extends React.Component {
render() {
// I would expect this to log the new props after press "loadMore"
console.log(this.props)
return (
<View style={{marginTop: 40}}>
<Text onPress={this.props.loadMore}>Push me</Text>
</View>
)
}
}
export default graphql(GetPosts, {
options: () => ({
variables: {
offset: 0,
limit: 1
}
}),
props: ({ ownProps, data }) => {
return {
...ownProps,
posts: data.posts,
loadMore: () => {
return data.fetchMore({
variables: {
offset: 1,
limit: 1
},
updateQuery: (previousResult, { fetchMoreResult }) => {
if (!fetchMoreResult) {
return previousResult;
}
const newResult = Object.assign({}, previousResult, {
posts: [...previousResult.posts, ...fetchMoreResult.posts]
});
console.log(newResult);
return newResult;
},
})
}
};
}
})(TestComponent);
编辑(2017 年 4 月 11 日)
我未能解释我的查询GetPosts
包含联合类型,这似乎可能是问题,因为如果我删除它们,更新会按预期工作。我已经打开了一个包问题,希望更清楚:https ://github.com/apollographql/react-apollo/issues/602