gpt4 book ai didi

graphql - Apollo Graphql : Avoid loading indicator during refetch

转载 作者:行者123 更新时间:2023-12-05 04:09:02 24 4
gpt4 key购买 nike

我有以下 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...给用户的指标。不确定如何实现这一目标。

最佳答案

我知道文档建议使用 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 可能会返回一些错误并且数据仍会返回。这意味着在生产环境中,您可能需要更强大的错误处理行为,这种行为不一定会在出现任何错误时阻止页面呈现。

关于graphql - Apollo Graphql : Avoid loading indicator during refetch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46742009/

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