将 ApolloClient 与 node.js 结合使用.“在全局范围内未找到提取并且未通过提取器" [英] Using ApolloClient with node.js. "fetch is not found globally and no fetcher passed"
问题描述
我正在尝试在 node.js 服务器上使用 Apollo 客户端,使用以下代码与另一个 GraphQL API 交互:
I am attempting to use an Apollo Client on a node.js server to interface with another GraphQL API using the following code:
import fetch from 'node-fetch'
import { createHttpLink } from 'apollo-link-http'
import ApolloClient from 'apollo-boost'
import { API_URL } from '...'
const client = new ApolloClient({
link: createHttpLink({
uri: API_URL,
fetch: fetch,
}),
})
产生以下错误:
module initialization error: Error
fetch is not found globally and no fetcher passed, to fix pass a fetch for
your environment like https://www.npmjs.com/package/node-fetch.
For example:
import fetch from 'node-fetch';
import { createHttpLink } from 'apollo-link-http';
const link = createHttpLink({ uri: '/graphql', fetch: fetch });
at Object.checkFetcher (/var/task/node_modules/apollo-link-http-common/lib/bundle.umd.js:78:19)
at createHttpLink (/var/task/node_modules/apollo-link-http/lib/bundle.umd.js:32:30)
at new HttpLink (/var/task/node_modules/apollo-link-http/lib/bundle.umd.js:203:38)
at new DefaultClient (/var/task/node_modules/apollo-boost/lib/index.js:80:24)
我知道默认情况下 Apollo Client 期望在浏览器上下文中运行,其中 fetch
方法将可用,并且在 node.js 中我需要 polyfill 或以其他方式提供fetch
方法,但我无法弄清楚如何做到这一点.
I understand that the Apollo Client by default is expecting to be run in a browser context where a fetch
method will be available, and that in a node.js I need to polyfill or otherwise provide a fetch
method, but I having trouble figuring out exactly how to do this.
遵循 https://www.apollographql.com/docs/中的示例代码link/#apollo-client 看来我应该能够使用 link
选项传递这些信息,并且阅读 apollo-boost
源代码似乎建议您可以使用 fetcherOptions
传递此信息,但这些解决方案似乎都不起作用.
Following the example code at https://www.apollographql.com/docs/link/#apollo-client it appears that I should be able to pass this information in using the link
option, and reading the apollo-boost
source code seems to suggest that you can pass this information in using fetcherOptions
, but neither of these solutions seem to work.
谁能提供一些示例代码,用于在 node.js 中使用 fetcher 初始化 Apollo 客户端?
Can anyone provide some example code for initializing an Apollo Client in node.js with a fetcher?
参考这里是我的package.json
{
"name": "API-Service",
"version": "1.0.0",
"description": "",
"private": true,
"scripts": {},
"dependencies": {
"apollo-boost": "^0.1.6",
"apollo-link-http": "^1.5.4",
"graphql": "^0.13.2",
"babel-polyfill": "^6.26.0",
"json-rules-engine": "^2.1.0",
"node-fetch": "^2.1.2",
"mysql": "^2.15.0"
}
}
推荐答案
原来apollo-boost
库提供的ApolloClient
不接受link
选项.切换到使用 vanilla apollo-client
允许您指定您的获取机制.
It turns out that the ApolloClient
provided by the apollo-boost
library does not accept a link
option. Switching to use the vanilla apollo-client
allows you to specify your fetching mechanism.
尽管 apollo-boost
和 apollo-client
都在文档中导出了 ApolloClient
,但它们采用了截然不同的选项.
Although apollo-boost
and apollo-client
both export an ApolloClient
in the docs, they take wildly different options.
import fetch from 'node-fetch'
import { createHttpLink } from 'apollo-link-http'
import { InMemoryCache } from 'apollo-cache-inmemory'
import ApolloClient from 'apollo-client'
import { API_URL } from '...'
const client = new ApolloClient({
link: createHttpLink({
uri: API_URL,
fetch: fetch,
}),
cache: new InMemoryCache(),
})
这篇关于将 ApolloClient 与 node.js 结合使用.“在全局范围内未找到提取并且未通过提取器"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!