gpt4 book ai didi

networking - 如何在全局范围内知道 ApolloClient 中所有查询的 networkStatus?

转载 作者:行者123 更新时间:2023-12-03 23:10:39 24 4
gpt4 key购买 nike

当 networkStatus 正在运行时,我试图显示预加载器。

我知道每个查询都会返回它自己的 networkStatus,但是在我的应用程序中有很多不同的查询。我想有一种方法可以在全局范围内处理所有查询的所有 networkStatus。

我想知道我的代码中的答案是:“网络上是否有任何待处理的查询?”。

最佳答案

目前,没有办法做到这一点,至少不容易/内置。您可以在 https://github.com/apollographql/apollo-feature-requests 上将此作为功能请求.

根据您想要实现的目标,在 HttpLink 上使用中间件/后件可能就足够了,例如:

import { ApolloLink } from 'apollo-link';

const middleware = new ApolloLink((operation, forward) => {
console.log('Starting', operation);

return forward(operation);
});

const afterware = new ApolloLink((operation, forward) => {
return forward(operation).map(response => {
console.log('Completed', operation);

return response;
});
});

const client = new ApolloClient({
cache: new InMemoryCache(),
link: ApolloLink.from([
middleware,
afterware,
new HttpLink({ ... }),
]),
});
middleware将在每个请求之前调用,并且 afterware , 后。您可以在以下位置阅读有关链接的更多信息: https://www.apollographql.com/docs/link/ .

或者,查看 Apollo 公开公开的一些 API,我能够以这种“非官方”方式进行检查:

function queriesInFlight() {
// client is your ApolloClient instance
const { queryManager } = client;

return Object.keys(queryManager.queryStore.getStore()).filter(queryId =>
queryManager.checkInFlight(queryId),
);
}

关于networking - 如何在全局范围内知道 ApolloClient 中所有查询的 networkStatus?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58028771/

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