1

我有一个问题,我们正在使用 apollo 客户端,特别是 useMutation react 挂钩来执行对我们的 GraphQL 服务器的突变调用。在某些时候,服务器可能会返回 401 未经授权的响应——此时,我们可以调用特殊的端点来重新验证客户端并刷新 cookie/令牌。一旦客户端被重新验证,我希望能够再次重新运行相同的突变。所以基本上我想知道是否可以执行以下操作: useMutation --> Receive 401 Unauthorized --> 调用刷新令牌 --> 重新运行相同的初始突变 这就是我们的 useMutation 的样子:

 const [mutationFunction, { data, ...rest }] = useMutation(query, {
    onError(_err: any) {
        const networkError = error?.networkError as any;
        if (networkError?.statusCode === 401 && !refreshFailed) {
            // eslint-disable-next-line prefer-destructuring
            loading = true;
            error = undefined;

            fetch('/authentication/refresh', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' }
            })
                .then(response => response.json())
                .then(token => {
                    localStorage.setItem(jwtLocalStorageKey, token);
                    // re fetch here
                })
                .catch(() => {
                    refreshFailed = true;
                });
        } else {
            showAlert(_err.message, 'error');
        }
    }
});

这就是我们目前的称呼:

  const {
    mutationFunction: updateTournamentUserMutation,
    loading: updateTournamentUserLoading,
    error: updateTournamentUserError,
    data: updateTournamentUserData
} = useMutationHook(gqlUpdateTournamentUser);

 updateTournamentUserMutation({ variables: { input } });

因为我们正在使用钩子以及我们在上面使用它的方式,所以我不完全确定我们如何保存或重用最初在第一个突变中发送的相同数据(即突变参数)是否有可能使用我们目前的方式这样做?

4

0 回答 0