graphql - stop pending requests with apollo client hooks

Viewed 958

I'm trying to cancel pending requests.

I'm following this tutorial that I've read on other question but I can not cancel any request.

I've configure apollo client as tutorial says.

Apollo client code:

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,
});

Apollo request:

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);
};

Apollo Cancel Request:

This file is equal like tutorial, and inside if where sould abort, I`ve add a log to show it shows my log.

In console shows that request is aborted but on network tab doesnt show that cancel previous pending requests.

What I'm doing wrong??

0 Answers
Related