首页 > 解决方案 > graphql - 使用 apollo 客户端挂钩停止挂起的请求

问题描述

我正在尝试取消待处理的请求。

我正在关注我在其他问题上阅读过的本教程,但我无法取消任何请求。

我已经按照教程中的说明配置了 apollo 客户端。

阿波罗客户端代码:

const authMiddleware = new ApolloLink((operation, forward) => {
    const locale: LocaleString = getLocale();
    operation.setContext({
        headers: {
            "Accept-Language": locale,
            authorization: `Bearer ${localStorage.getItem("token")}` || null,
            "Content-Type": "application/json",
        },
    });

    return forward(operation);
});

const restAPILink: any = new RestLink({
    uri: `${getEnvVariable(REACT_APP_HIERARCHY_API)}/${apiVersion}/`,
    endpoints: { ... }
});


const httpLink = new HttpLink({
    uri: `${getEnvVariable(REACT_APP_GRAPHQL_API)}`,
});

const composedLink: any = ApolloLinkFrom([
    cancelRequestLink,
    restAPILink,
    httpLink,
]);

const apolloClient = new ApolloClient({
    cache: new InMemoryCache(),
    link: concat(authMiddleware, composedLink),
    connectToDevTools: true,
    queryDeduplication: false,
});

阿波罗请求:

const getSupplyPoints = async () => {
    setLoading(true);
    setError(false);
    const response = await client.mutate({
        mutation: CONNECTIONS_DATA,
        variables: {...},
        context: {
            requestTrackerId: uuidNameSpace(
                "CONNECTIONS_DATA",
                RequestNameSpace
            ),
        },
    });
    setLoading(false);
    setError(response?.errors ? true : false);
    setData(response?.data);
};

阿波罗取消请求:

该文件与tutorial相同,如果在其中中止的地方,我添加了一个日志以显示它显示我的日志。

在控制台中显示请求已中止,但在网络选项卡上未显示取消先前的挂起请求。

我做错了什么??

标签: graphqlapolloreact-apollo

解决方案


推荐阅读