结合使用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客户端希望在可使用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
源代码似乎表明您可以在使用
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中初始化Apollo客户端吗?
Can anyone provide some example code for initializing an Apollo Client in node.js with a fetcher?
作为参考,这里是我的package.json
For reference here is my 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
选项.切换为使用香草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屋!