Apollo-link-rest: 在微信小程序中使用apollo-link-rest时,出现“标题未定义”

创建于 2019-07-16  ·  25评论  ·  资料来源: apollographql/apollo-link-rest

  • [x]具有复制

当我们尝试在微信小程序中使用apollo-link-rest时,我们遇到了一个引发如下错误的问题:

 Headers is not defined 
 ReferenceError: Headers is not defined

当我在RestLink.ts中找到源代码时,它确实会尝试查找Headers,但绝对如此,在weixin环境下没有Headers,那么有人可以帮助提供一些技巧或解决方案来解决此问题吗? 非常感谢您的帮助,谢谢。

help wanted 🛠 question❔

最有用的评论

这就是为我工作的最终结果:

import clientFetch from 'unfetch';
import serverFetch, { Headers as ServerHeaders } from 'node-fetch';

const client = typeof document !== 'undefined';
global.Headers = client ? global.Headers : ServerHeaders;
const customFetch = client ? clientFetch : serverFetch;

const acmeLink = new JsonApiLink({
  …
  credentials: 'same-origin',
  customFetch,
});

所有25条评论

@ Janice1114 ,您好,我不熟悉

通常,如果标头不可用,则提取也将不可用。 因此,一个可能同时适用于两者的polyfill是这样的: https :

@fbartho ,您好,感谢您的快速回复。 我尝试使用polyfill启用提取标头,但仍然会收到错误。

而且我发现在weixin下,它对提取使用wx.request,它不支持与窗口相关的变量,因此恐怕polyfill无法正常工作。

而且我是否知道会有什么黑客或实现方法来帮助跳过apollo-link-rest的源代码中的标头循环? 还是可以请您提供一些自定义选项以适合不同情况?

您可以对ApolloLinkRest使用customFetch参数来提供包装函数,此包装函数可以从Polyfill中获取基于窗口的标题,并将其展平为传统的对象Hash。

类似于以下内容(未经测试,写在我的浏览器中):

function customFetch(url, options) {
   const headers = options.headers.entries().reduce((accumulator, h) => { accumulator[h[0]] = h[1];  return accumulator;}, {})
   return fetch(url, {…options, headers});
}
// Elsewhere: new RestLink({ customFetch, ...

非常感谢您的友好响应,我将尝试在weixin开发中使用这种polyfill。

当我阅读RestLink.ts中的源代码时,它将尝试访问Headers,而在weixin中不可用。 它可以在global分配变量。

因此,我想知道是否可以在RestLink构造函数过程中应用任何自定义或替代设置?

再次问候您如此精彩的提示。

@fbartho ,您好,为在
标头问题修复

您能帮忙查看一下PR代码是否可以合并到master中吗?
非常感谢,如果能帮助的话,也非常感谢。

我在"next": "^9.0.3"有同样的问题

我的下一个^8.1.0版本也遇到了这个问题。 对于我的图形API,我使用isomorphic-unfetch ,版本^3.0.0来进行polyfill,如下所示:

import fetch from 'isomorphic-unfetch'
const graphLink = createHttpLink({ uri, fetch })

但是,使用相同的程序包通过其余链接进行polyfill无效:

const restLink = new RestLink({
  uri,
  customFetch: fetch,
})

我还使用您的customFetch方法尝试了node-fetch

import fetch from 'node-fetch'

function customFetch(url, options) {
  const headers = options.headers.entries().reduce((accumulator, h) => {
    accumulator[h[0]] = h[1]
    return accumulator
  }, {})
  return fetch(url, { ...options, headers })
}

const restLink = new RestLink({
  uri: CONTENTFUL_CONTENT_DELIVERY_API,
  customFetch,
})

在这两种情况下,我下次在开发人员模式下运行时都会得到ReferenceError: Headers is not defined

喜欢这里的想法,如果您可以提供一些有关如何在服务器环境中实现的指导,将不胜感激。 谢谢!

您可以在仍然使用自己的customFetch API的同时填充Headers API!

让我知道您是否想要一些示例代码,但是我相信这是可行的,而且不会带来太多麻烦。 我这分钟不在我的电脑前。

@fbartho如果有机会,一些示例代码会很棒! 我刺了一下,但没有成功。 谢谢!

import * as Polyfillheaders from 'fetch-headers'
import fetch from 'isomorphic-unfetch'

// hook in the Headers polyfill for your environment!
global.Headers = global.Headers || Polyfillheaders;

function customFetch(url, options) {
  const headers = options.headers.entries().reduce((accumulator, h) => {
    accumulator[h[0]] = h[1]
    return accumulator
  }, {})
  return fetch(url, { ...options, headers })
}

const restLink = new RestLink({
  uri: CONTENTFUL_CONTENT_DELIVERY_API,
  customFetch,
})

像上面的东西可能会解决问题@ 2wheelcoder

谢谢! 有机会试一下后,我会在这里报告。

@fbartho嗨! 我在ios 9.3.5上遇到了这个问题。我尝试导入fetch-headers polyfill但不起作用。
我发现fetch-headers的标头实例没有forEach方法(见下文)。
image

还有其他解决方案吗?

@fbartho嗨! 我在ios 9.3.5上遇到了这个问题。我尝试导入fetch-headers polyfill但不起作用。
我发现fetch-headers的标头实例没有forEach方法(见下文)。
image

还有其他解决方案吗?

我通过使用提取polyfill解决了这个问题。

我将结束本节,因为我相信我们可以通过在应用程序端使用polyfill来解决此问题,并且我们不想使该polyfill成为该存储库的依赖项。

我无法使用polyfill解决此问题。 我已经尝试了isomorphic-fetch,isomorphic-unfetch,fetch-header等,但始终会遇到未定义current.forEachTypeError: Right-hand side of 'instanceof' is not an object 。 有什么帮助吗?

@VinSpee您是否尝试过此提取polyfill。

@lintuming是的,但是在服务器上不起作用。

@VinSpee也许您可以使用此方法,似乎它们具有forEach方法。
image
希望这项工作。

@lintuming感谢您的帮助。 有了这个,我得到:

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

似乎是一个纠结的问题网🤣

@VinSpee对不起,我不知道为什么会这样。但是我认为这可能会有所帮助:
https://stackoverflow.com/questions/7042340/error-cant-set-headers-after-they-are-sent-to-the-client

这就是为我工作的最终结果:

import clientFetch from 'unfetch';
import serverFetch, { Headers as ServerHeaders } from 'node-fetch';

const client = typeof document !== 'undefined';
global.Headers = client ? global.Headers : ServerHeaders;
const customFetch = client ? clientFetch : serverFetch;

const acmeLink = new JsonApiLink({
  …
  credentials: 'same-origin',
  customFetch,
});

@VinSpee我面临着同样的问题,我试图用您的解决方案解决此问题,但我没有成功。
你能分享你的工作代码吗

谢谢@VinSpee —这正是我所需要的

我可以通过使用自定义提取来解决此问题

import fetch from "isomorphic-fetch"

const restLink = new RestLink({
  uri: "https://...",
  customFetch: fetch,
  headers: {
    "Content-Type": "application/json",
  },
})

@VinSpee的解决方案也适用于Gatsby JS应用程序。

此页面是否有帮助?
0 / 5 - 0 等级