按照这里的说明:
https://www.apollographql.com/docs/react/performance/server-side-rendering/#server-side-rendering
要进行服务器端渲染,我遇到了以下错误:
Invariant Violation:
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 });
我添加了推荐的代码,导入
fetch
,将其传递给
createHttpLink
,我也安装了
@types/node-fetch
,但我收到了以下警告/错误:
Error:(75, 7) TS2322: Type 'typeof fetch' is not assignable to type '(input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>'.
Types of parameters 'url' and 'input' are incompatible.
Type 'RequestInfo' is not assignable to type 'import("C:/Users/pupeno/Documents/Flexpoint Tech/js/exp5/node_modules/@types/node-fetch/index").RequestInfo'.
Type 'Request' is not assignable to type 'RequestInfo'.
Type 'Request' is missing the following properties from type 'Request': context, compress, counter, follow, and 6 more.
我传递的fetch函数的类型,定义于
node-fetch/index.d.ts
是:
declare function fetch(
url: RequestInfo,
init?: RequestInit
): Promise<Response>;
而预期的类型
取来
是:
fetch?: WindowOrWorkerGlobalScope['fetch'];
我不知道该怎么找
WindowOrWorkerGlobalScope
,我的IDE指出了两种可能的定义,一种是
dom
:
fetch(input: RequestInfo, init?: RequestInit): Promise<Response>;
一个在
webworker
:
fetch(输入:RequestInfo,init?:RequestInit):Promise<答复>;
它们看起来一模一样。
我可以继续在各种类型的兔子洞里走下去,但这听起来很正常吗?我是否应该使用不同的
取来
?