Apollo fetch 在退出键按下时取消

Apollo fetch cancels on escape key press

我正在使用 apollo-client@2.3.5(但这个问题也发生在最新版本上,可能与 Apollo Client 本身没有直接关系)。

我使用多个请求来加载我的应用程序的不同部分。我的问题是 Escape 按键取消了获取请求,并破坏了整个应用程序。我已经试过了:

但是在 Escape 按键时请求仍然被取消。

是否有任何解决方法或方法来防止提取请求被取消?或者可能有一些与 Fetch API 兼容的客户端不会取消 Escape 按键时的请求?

对于遇到相同或相似问题的任何人,我设法解决了这个问题:

  1. 我创建了以下函数:

    export const makeRequest = (url, requestParams) => {
        requestParams.data = requestParams.body;
    
        return axios(url, requestParams)
            .then(response => {
                return new Response(JSON.stringify(response.data), {
                    ...response.headers,
                    status: response.status,
                    statusText: response.statusText
                })
            });
    };
    
  2. 我使用 fetch 属性:

    在 Apollo Client 中替换了 Apollo Link 中的默认提取
    new HttpLink({
        uri: config.graphqlUrl,
        credentials: 'same-origin',
        fetch: makeRequest
    })
    

makeRequest 功能模仿 fetch,但不会被 Escape 按键取消。

Apollo 客户端希望响应是 Response 的实例,所以我们提供一个。

编辑: 原来它在IE11上不起作用。如果您关心 IE 兼容性,您应该安装 whatwg-fetch (https://www.npmjs.com/package/whatwg-fetch) 并在文件顶部包含以下内容:

import { Response } from "whatwg-fetch";