我有以下apollo-graphql客户端代码,其中我每30秒触发一次graphql查询并获取数据。
import React, { Component } from 'react';
import { gql, graphql } from 'react-apollo';
import _ from 'underscore';
class Test extends Component {
render() {
if (this.props.TestData.loading) {
return <div>Loading...</div>
}
if (this.props.TestData.error && this.props.TestData.error !== null) {
return <div>Error...</div>
}
// Iterate through the this.props.TestData.getTestData and build the Table of data here
return (
<table>
_.map(this.props.TestData.getTestData.testList, (test) => {
<tr>
<td>{test.testName}</td>
<td>{test.status}</td>
</tr>
})
</table>
);
}
}
const TestQuery = gql`
query TestQuery() {
getTestData() {
testList {
testName
status
}
}
}
`;
const options = () => ({
pollInterval: 30000,
});
const withTestData = graphql(TestQuery, {
name: 'TestData',
options,
});
export default withTestData(Test);
我面临的问题是,自从查询被重新触发后,我每隔30秒就会看到Loading...
。我希望Loading...
仅在页面启动时显示,此后它应该是平滑更新,我不想向用户显示Loading...
指示符。不知道如何实现这一目标。
答案 0 :(得分:2)
我知道文档建议使用data.loading
,但在大多数情况下,检查查询结果是否为null也是如此:
// Should probably check this first. If you error out, usually your data will be
// undefined, which means putting this later would result in it never getting
// called. Also checking if it's not-null is a bit redundant :)
if (this.props.TestData.error) return <div>Error...</div>
// `testList` will only be undefined during the initial fetch
// or if the query errors out
if (!this.props.TestData.getTestData) return <div>Loading...</div>
// Render the component as normal
return <table>...</table>
请记住,GraphQL可能会返回一些错误并仍然可以返回数据。这意味着在生产环境中,您可能需要更强大的错误处理行为,如果存在任何错误,则不一定会阻止页面呈现。