node-fetchã§HttpLinkã䜿çšãããšã次ã®ãšã©ãŒãçºçããŸã
import { HttpLink } from 'apollo-link-http';
import fetch from 'node-fetch';
const link = new HttpLink({
fetch,
uri: this.endPoint.toString(),
});
æå³ããçµæïŒ
ããã¥ã¡ã³ãã«ãããšãäžèšã¯ã³ã³ãã€ã«ãããŠããã¯ãã§ãã
å®éã®çµæïŒ
src/tw-algo-manager.ts:20:31 - error TS2345: Argument of type '{ fetch: (url: string | Request, init?: RequestInit | undefined) => Promise<Response>; uri: strin...' is not assignable to parameter of type 'Options | undefined'.
Type '{ fetch: (url: string | Request, init?: RequestInit | undefined) => Promise<Response>; uri: strin...' is not assignable to type 'Options'.
Types of property 'fetch' are incompatible.
Type '(url: string | Request, init?: RequestInit | undefined) => Promise<Response>' is not assignable to type '((input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>) | undefined'.
Type '(url: string | Request, init?: RequestInit | undefined) => Promise<Response>' 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 'string | Request'.
Type 'Request' is not assignable to type 'string | Request'.
Type 'Request' is not assignable to type 'Request'. Two different types with this name exist, but they are unrelated.
Property 'context' is missing in type 'Request'.
20 const link = new HttpLink({
~21 fetch,
~~~~~~~~~~~~22 uri: this.endPoint.toString(),
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~23 }); // const link = createHttpLink(linkOptions);
åé¡ãåçŸããæ¹æ³ïŒ
äžèšã®ã³ãŒãã®èšè¿°ã«ã¯ã次ã®ããŒãžã§ã³ã䜿çšãããŸãã
"@types/graphql": "^0.12.4",
"@types/node-fetch": "^1.6.7",
"apollo-cache-inmemory": "^1.1.9",
"apollo-client": "^2.2.5",
"apollo-link": "^1.2.0",
"apollo-link-http": "^1.4.0",
"graphql": "^0.13.1",
"graphql-tag": "^2.8.0",
"node-fetch": "^1.7.2",
"react-apollo": "^2.0.4",
"url": "^0.11.0"
äžèšã®ããŒãžã§ã³ã䜿çšããtypescriptã§HttpLinkã®ã€ã³ã¹ã¿ã³ã¹ãäœæããŠãäžèšã®ãšã©ãŒã確èªããŸãã
createHttpLink
代ããã«new HttpLink
createHttpLink
ã䜿çšãããšã©ããªããŸããïŒ
import { createHttpLink } from 'apollo-link-http';
import fetch from 'node-fetch';
const link = createHttpLink({
fetch,
uri: this.endPoint.toString(),
});
@dejayccreateHttpLinkã䜿çšããå Žåãåããšã©ãŒãçºçããŸãã
å®éãç§ã¯åé¡ãç解ããŸããã node- fetch2.xã¯_apollo-link_ãšäºææ§ããããŸããã _fetch_ã®çœ²åã¯ç°ãªããŸãã
node-fetch2.xã¯apollo-linkãšäºææ§ããããŸããã ãã§ããã®çœ²åãç°ãªããŸãã
ãããããã¯ãã³ãã§ãããšæããŸãã
ç§ã¯ä»æ¥ãå圢ã¢ããªã§ãã®åé¡ã«ééããŸããã ç¹ã«æè¿ã®SSRã§ã¯ãã°ããŒãã«ãã§ãããããã©ã«ãã®ãã©ãŒã«ããã¯ã§ããããšã«å®å šã«ã¯åæããŸããããå°ãªããšããšã©ãŒã¯ããçšåºŠæçã§ããã
Error:
fetch is not found globally and no fetcher passed, to fix pass a fetch for
your environment like https://www.npmjs.com/package/nodefetch.
For example:
import fetch from 'nodefetch';
import { createHttpLink } from 'apollo-link-http';
ç§ã®è§£æ±ºçã¯ãããŒã/ãã©ãŠã¶ãŒç°å¢ã«å¿ããŠãæ¡ä»¶ä»ãã§node-fetch
ãŸãã¯whatwg-fetch
䜿çšããããšcross-fetch
ã䜿çšããŸãã
ãã...
// Using TypeScript
import * as fetch from 'cross-fetch'
new HttpLink({ fetch })
// Or just...
// import 'cross-fetch/polyfill'
@jmca TypeScriptã«å¯ŸããŠããªããææ¡ããæ¹æ³ã¯ãç§ã«ãšã£ãŠã¯
ä»æ¥ãã®åé¡ãçºçããŸãããã node-fetch
v2ãšäºææ§ããªãããšãããããŸãããããã¯ãä¿®æ£ããå¿
èŠããããšæããŸãã
ä»ããcross-fetch
ã䜿ãå§ããŸãããã fetch: any
ã€ã³ããŒãããã ãã§ã...
ïŒ2018幎5æ7æ¥ã®ãã©ããŒã¢ããïŒ cross-fetch
TypeScriptã¿ã€ãå®çŸ©ãä»å±ããããã«ãªããŸããïŒ
cross-fetch
ã¯ãTypeScriptåå®çŸ©ããŸã ä»å±ããŠããŸããïŒ@DefinitelyTypedãä»å±ããŠããŸããïŒã
lquixada / cross-fetchïŒ12ãããŒãžãããã®ãéã cross-fetch
ã代æ¿æ段ã§ãããçŸæç¹ã§ã¯ãTypeScriptã®HttpLink
ã§isomorphic-fetch
ãæ£åžžã«æ©èœããŠããããšãããããŸããã
import { HttpLink } from 'apollo-boost'
import fetch from 'isomorphic-fetch'
const link = new HttpLink({
fetch
})
npmjs.comã®ããã¥ã¡ã³ãã§ã¯ããŠãŒã¶ãŒãããŒããã§ããã䜿çšããããšãæšå¥šããŠããŸãããããŒãžã§ã³ã¯æå®ãããŠããŸããã ãã®ããã¥ã¡ã³ããæŽæ°ããã«ã¯ã©ãããã°ããã§ããïŒ
æ³šïŒ whatwg-fetch
ã¯ãå®è¡å¯èœãªåé¿çã§ãã
ããã«ã€ããŠäœãåãã¯ãããŸãããïŒ
cross-fetch
䜿çšãããšã node-fetch
䜿çšããã ããªã®ã§ãå¥åŠãªåé¿çã®ããã«èŠããŸãã ããã¯å®éã«ã¯åé¡ã§ã¯ãããŸãããïŒ
whatwg-fetch
ã¯ããµãŒããŒåŽã®ç§ãã¡ã«ãšã£ãŠã¯åé¿çã§ã¯ãããŸããã
誰ããããã¹ãŠã®ãã§ããå®è£ ã§æ©èœããããããã€ãã®åãã§ãã¯ãç¶æããé©åãªåå®çŸ©ãå«ãPRãéããå Žåã¯ã @ stubailoç§ã«
@stubailo ApolloãµãŒããŒã§ã¯ããããapollo-server-env
äžéšãšããŠäœ¿çšããŸãã
HttpLinkã®äœ¿çšã¯ãªãã·ã§ã³ã§ãããããã¯ç§ã«ãšã£ãŠã¯ããŸããããŸãã
import ApolloClient from 'apollo-boost'
import 'isomorphic-fetch'
const client = new ApolloClient({
uri: 'endpoint-url-here'
})
ããã«é¢ãããã¥ãŒã¹ã¯ãããŸããïŒ node-fetchã¯ãåŒãç¶ãapollo-linkãšTSã§åé¡ãåŒãèµ·ãããŸãã
é£çµ¡ãã£ãïŒ ãŸã node-fetch
åé¡ãçºçããŠããŸã
ãã®åé¿çã¯ã @types/node-fetch
ãã€ã³ã¹ããŒã«ãããæåã§GlobalFetchãšããŠå®çŸ©ããããšã§ãã
ãããè¡ãã«ã¯ããããžã§ã¯ãå
ã®ä»»æã®.d.ts
ãã¡ã€ã«ã«ä»¥äžãè¿œå ããŸãã
// real node-fetch types clash with apollo-link-http, so manually define it as globalfetch here.
declare module 'node-fetch' {
const fetch: GlobalFetch['fetch'];
export default fetch;
}
as
åã®åŒ·å¶çãªãã®ã䜿çšã§ããŸã...
ãã¡ããã§ãããããããã®æ¹æ³ã§åŒ·å¶çã«å¥ã®å Žæã«ãã£ã¹ãããå Žåã¯ãã¿ã€ããã€ã³ã¹ããŒã«ããæå³ã¯äœã§ããïŒ
ãã®å Žåã¯ãç§ã®ãœãªã¥ãŒã·ã§ã³ã䜿çšããããšãã§ããŸãã
ç³ãèš³ãããŸããããç§ã¯ããªããããŠããããšãèªã¿ééããŸãããç§ã®åã®ã³ã¡ã³ããç¡èŠããŠãã ããã
ãã ããGlobalFetchã¯ããlibããã£ãŒã«ãã®ãšã³ããªãšããŠãdomããããå Žåã«ã®ã¿æäŸããããããããã«ã¯tsconfigãå€æŽããŠãã©ãŠã¶ãã¿ãŒã²ããã«ããå¿ èŠããããŸãïŒ https ïŒ
次ã«ã .d.ts
ãæåã§å€æŽãã代ããã«ã GlobalFetch['fetch']
ãHTTPLinkã³ã³ã¹ãã©ã¯ã¿ãŒã«æž¡ãã ãã§ã¯ãããŸãããïŒ ããã¯ãããã¯ããã«å°ãªãé ããŸãã
ãã¡ããã§ããã HttpLinkã®å
¥åã«æºæ ããã«ã¯ã GlobalFetch['fetch']
ã䜿çšããå¿
èŠãããããããã®èŠä»¶ãåé¿ããæ¹æ³ã¯ãããŸããã
ç§ã¯ãã©ããŒããŸããã GlobalFetch['fetch']
ã¯åã§ãããå€æ°ã§ã¯ãããŸããã
node-fetchãã€ã³ããŒãããŠããGlobalFetch['fetch']
ãã£ã¹ããããšããããšã§ããïŒ
ãããžã§ã¯ãã§noImplictAny
æå¹ã«ããŠããã®ã§ãå®çŸ©ã®ãªããã®ãã€ã³ããŒãã§ããªããããããã¯ãªãã·ã§ã³ã§ã¯ãããŸããã
ããã¯ç§ã«ãšã£ãŠã¯ããŸãããåé¿çã§ãð€·ââïžããããç§ã¯ãããå®ç§ã«ã¯ã»ã©é ãããšãç解ããŠããŸãïŒããŒã¯ãŒãïŒ_åé¿ç_ïŒã
è¯ã解決çã¯ãå
¥åãGlobalFetch['fetch']
åéåãšãããŒããã§ããã®ãšã¯ã¹ããŒãã®ããã©ã«ãã¿ã€ãã«å€æŽããããšã ãšæããŸãã
ãŸãã¯ãæšå¥šã©ã€ãã©ãªãGlobalFetch['fetch']
ïŒwhatwg-fetchãªã©ïŒã«æºæ ããããŒããã§ããã©ã€ãã©ãªã«å€æŽããã ãã§ãã
ããããããå¿ èŠã ãšã¯æããŸããã§ããã TILã
ç³ãèš³ãããŸããããç§ã¯æ¬åœã«äŒã¿ã®æ¥ãéãããŠããŸãã ããã§ããããã¯åã§ãããå€æ°ã§ã¯ãããŸããã
https://www.npmjs.com/package/whatwg-fetchã¯ãããã®ãããžã§ã¯ãã¯Node.jsç°å¢ã§ã¯æ©èœããŸãããããã¯ãWebãã©ãŠã¶ãŒã®ã¿ã察象ãšããŠããŸããã¢ããªã±ãŒã·ã§ã³ã次ã®ããšãè©Šã¿ãªãããã«ããå¿ èŠããããŸãããããããã±ãŒãžåããŠãµãŒããŒã§å®è¡ããŸããã
ã¯ãã誰ããã¿ã€ããä¿®æ£ããããšã«ãã£ãŠãã°ãé©åã«ä¿®æ£ããå¿ èŠããããŸãã
ããã«æ€èšãããšãããã¯äž»ã«node-fetch
åé¡ã§ãããšæããããããäžèšã®åé¡ãéããŸããã ãã ããã¿ã€ããå€æŽããããã«æ瀺ãããå¯èœæ§ã¯ååã«ãããŸãã
ããŠã node-fetch
ã¯ã¿ã€ããå€æŽããããã«æ瀺ããŠããããã§ãã 1ã€ã®ããã¯ã¯ã node-fetch
ã®åãã€ã³ããŒããããããåé¿çãšããŠè¿œå ããããšã§ãã
@grantwwuç³ãèš³ãããŸããããGraphQL Summitã®æŠèŠã¯ããªãå¿ããã§ãããæè¿ãé©åãªã¿ã€ãã§node-fetch
ãåãšã¯ã¹ããŒãããapollo-env
ããã±ãŒãžã®äœ¿çšãéå§ããŸããïŒ httpsïŒ// github .com / apollographql / apollo-tooling / tree / master / packages / apollo-env
ïŒãã ããã°ããŒãã«ãã§ãããšã¯ã¹ããŒããåé¢ããããšããå§ãããŸãïŒ
import { fetch } from 'apollo-env'
ãå®è¡ãããšããã HttpLink
ã³ã³ã¹ãã©ã¯ã¿ãŒã«æž¡ããšãã«TypeScriptãšã©ãŒãçºçããŸãã
TSError: ⚯ Unable to compile TypeScript:
src/index.ts(20,31): error TS2345: Argument of type '{ uri: string; fetch: (input?: string | Request | undefined, init?: RequestInit | undefined) => Promise<Response>; }' is not assignable to parameter of type 'Options'.
Types of property 'fetch' are incompatible.
Type '(input?: string | Request | undefined, init?: RequestInit | undefined) => Promise<Response>' is not assignable to type '(input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>'.
Types of parameters 'input' and 'input' are incompatible.
Type 'RequestInfo' is not assignable to type 'string | Request | undefined'.
Type 'Request' is not assignable to type 'string | Request | undefined'.
Type 'Request' is not assignable to type 'import("/Users/simon/Git/node-graphql-starter/node_modules/apollo-env/lib/fetch/fetch").Request'.
Types of property 'headers' are incompatible.
Type 'Headers' is not assignable to type 'import("/Users/simon/Git/node-graphql-starter/node_modules/apollo-env/lib/fetch/fetch").Headers'.
Types of property 'values' are incompatible.
Type '() => IterableIterator<string>' is not assignable to type '() => Iterator<[string]>'.
Type 'IterableIterator<string>' is not assignable to type 'Iterator<[string]>'.
Types of property 'next' are incompatible.
Type '{ (value?: any): IteratorResult<string>; (value?: any): IteratorResult<string>; }' is not assignable to type '{ (value?: any): IteratorResult<[string]>; (value?: any): IteratorResult<[string]>; }'.
Type 'IteratorResult<string>' is not assignable to type 'IteratorResult<[string]>'.
Type 'string' is not assignable to type '[string]'.
ç§ã¯ãããç§ã®ãã¹ãã§æ©èœãããããšãã§ããŸããïŒ
import { fetch } from 'apollo-env'
......
function httpLink({ apiUrl, idToken }) {
return new HttpLink({
uri: apiUrl,
headers: {
authorization: `Bearer ${idToken}`,
},
fetch
})
}
ããã¯ãŸã ç§ã®ããã«åããŠããŸããïŒ/
src/remoteSchemas.ts:54:45 - error TS2322: Type '(input?: RequestInfo, init?: RequestInit) => Promise<Response>' is not assignable to type '(input: RequestInfo, init?: RequestInit) => Promise<Response>'.
Types of parameters 'input' and 'input' are incompatible.
Type 'RequestInfo' is not assignable to type 'import("/Users/grant.wu/petuum/api-gateway/node_modules/apollo-env/lib/fetch/fetch").RequestInfo'.
Type 'Request' is not assignable to type 'RequestInfo'.
Type 'Request' is not assignable to type 'import("/Users/grant.wu/petuum/api-gateway/node_modules/apollo-env/lib/fetch/fetch").Request'.
Types of property 'headers' are incompatible.
Type 'Headers' is missing the following properties from type 'Headers': entries, keys, values, [Symbol.iterator]
54 let link = createHttpLink({ uri: url, fetch, fetchOptions: { timeout: remoteSchemaTimeout } });
@jacobtaniã©ã®ããŒãžã§ã³ã®apollo-http-linkãšapollo-envã䜿çšããŸãããïŒ ãŸããããã¯ããŒãä»ãã§ãããïŒ
ã¯ããããã§ãåãã§ãã apollo-envã䜿çšããŠããåé¡ã¯è§£æ±ºããŸããã
Argument of type '{ credentials: string; fetch: (input?: string | Request | undefined, init?: RequestInit | undefined) => Promise<Response>; uri: string; }' is not assignable to parameter of type 'PresetConfig'.
Types of property 'fetch' are incompatible.
Type '(input?: string | Request | undefined, init?: RequestInit | undefined) => Promise<Response>' is not assignable to type '(input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>'.
Types of parameters 'input' and 'input' are incompatible.
Type 'RequestInfo' is not assignable to type 'string | Request | undefined'.
Type 'Request' is not assignable to type 'string | Request | undefined'.
Type 'Request' is not assignable to type 'import("/Users/rahul/work/r3pi/vi-image-contours/node_modules/apollo-env/lib/fetch/fetch").Request'.
Types of property 'headers' are incompatible.
Type 'Headers' is missing the following properties from type 'Headers': entries, keys, values, [Symbol.iterator]
@grantwwu ïŒç§ã¯äœ¿çšããŸã
"apollo-client"ïŒ "^ 2.4.12"ã
"apollo-env"ïŒ "^ 0.3.2"ã
"apollo-link-http"ïŒ "^ 1.5.9"ã
ã¢ããªã®äŸåé¢ä¿ç®¡çã«yarnã䜿çšããŠããŸã
ãããžã§ã¯ãã®graphql-requestã«åãæ¿ããŸããã
@jacobtani tsconfigã¯ã©ã®ããã«èŠããŸããïŒ
apollo-env
ã¯ãHttpLinkãæåŸ
ãããã®ãšã¯ãŸã ç°ãªããŸãã å
¥åãã©ã¡ãŒã¿ã¯å¿
èŠãããŸãã
ç§ã¯ãã®ããã«ããããªãŒããŒã©ã€ãããããšã«ãªããããã¯ããŸããããŸãã
declare module "apollo-env" {
export function fetch(
input: RequestInfo,
init?: RequestInit,
): Promise<Response>;
}
ããã䜿ã£ãŠããŒãããããã䜿ã£ãŠããŸã
"apollo-env": "^0.3.3"
"apollo-link-http": "^1.5.11"
DOCS APOLLO TEAMã«ããããåœãŠãŠãã ããïŒ ãã®åé¡ã¯1幎以äžåã®ãã®ã§ã
@rlanceräžèšã®ããã«ã apollo-env
ã«ã¯ããããå¿
èŠãªãããã³ã¡ã³ãã¯ãŸã ä¿®æ£ãããŠããªãããããããåœãŠã¯ãŸããšæããŸãã ããã¯ãã®ãªããžããªã§ã¯ãªãã apollo-tooling
ãªããžããªã«ãããŸãã
@JoviDeCroockæ©èœãããœãªã¥ãŒã·ã§ã³ããããŸããããã¥ã¡ã³ãã¯ã倱æããŠåé¿çãæ±ããŠGoogleã«åŒ·å¶ããã®ã§ã¯ãªãããããã䜿çšããããã«æ瀺ããå¿ èŠããããŸãã
@JoviDeCroock apollo-env
ãšapollo-tooling
ã¯ã©ã¡ããåããã§ããå宣èšãæã¡ãã©ã¡ããHttpLinkãæåŸ
ããGlobal ['fetch']ãšã¯ç°ãªããŸãã ããããããã¯åé¡ã§ã¯ãªãããã§ãã apollo-env
ãšåã宣èšã§ã¢ãžã¥ãŒã«ãèªåã§å®£èšããŠããåã«ã€ããŠã¯æå¥ãèšããŸããã ãã¶ãããšã¯ã¹ããŒããæ©èœããŠããŸãã
ãŸããç§ã¯å人çã«ãããã䜿çšããããšã¯ãããŸããã ããªããç§ã«è§£æ±ºçãææããããšãã§ããã°ç§ã¯åãã§pr
ãããã®2ã€ã®ã¿ã€ããåãã«ããããšã§ããŸãããã¯ãã ãšæããŸãã
declare function fetch(
input?: RequestInfo, ---> remove ?
init?: RequestInit
): Promise<Response>;
declare interface GlobalFetch {
fetch(input: RequestInfo, init?: RequestInit): Promise<Response>;
}
@jmcaããããšãããããŸããããªãã¯ç§ã®æ¥ãæããŸãããåé¡ãçºçããŸããjestãã¹ãã䜿çšããŠãããšãã«æ°ããcreateUploadLinkã§èªå·±ãå®çŸ©ãããŠããŸããã
@tafelito解決çãããããšããããã¯éåžžã«åœ¹ã«ç«ã¡ãŸããããããç§ã¯æ¬åœã®ãšã©ãŒãçºèŠããããšæããŸãããããŠããã¯æ°ããTypeScriptã¢ããããŒãã«ãããã®ã§ãã 3.6ããŒãžã§ã³ã§ã¯ã GlobalFetch
ãåé€ããã代ããã«WindowOrWorkerGlobalScope
ã䜿çšããããããpackage.json "typescript": "3.5.1"
äŸåé¢ä¿ã§ããŒãžã§ã³ãéããå¿
èŠããããŸãã
ããã«ãªã³ã¯ããããŸã
äžèšã®ã³ã¡ã³ãããšã³ãŒããã«ã¯ãç§ã®ãªããžããªã¯ã¿ã€ãã¹ã¯ãªããv3.5.3
ã«åºå·ããå¿
èŠããããŸã
ãããã®2ã€ã®ã¿ã€ããåãã«ããããšã§ããŸãããã¯ãã ãšæããŸãã
declare function fetch( input?: RequestInfo, ---> remove ? init?: RequestInit ): Promise<Response>; declare interface GlobalFetch { fetch(input: RequestInfo, init?: RequestInit): Promise<Response>; }
@tafelitoã«æè¬ããŸãã2çªç®ã®éšåã ãã䜿çšããŠä¿®æ£ã§ããŸããã
åãåé¡ã
"@types/node-fetch": "^2.5.0",
"typescript": "^3.5.1"
"node-fetch": "^2.6.0",
error TS2345: Argument of type '{ uri: string; fetch: typeof fetch; }' is not assignable to parameter of type 'Options'.
Types of property 'fetch' are incompatible.
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("/Users/ldu020/workspace/github.com/mrdulin/apollo-graphql-tutorial/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.
8 const link = new HttpLink({ uri: 'http://localhost:3000', fetch });
ç§ã®ã¢ããªã±ãŒã·ã§ã³ã§ãåãåé¡ããããŸãã ç§ã¯æ¬¡ã®ããã«any
ãžã®ãã£ã¹ãã解決ããŸãã
import { ApolloClient } from 'apollo-client'
import { InMemoryCache } from 'apollo-boost'
import { createHttpLink } from 'apollo-link-http'
import fetch from 'node-fetch'
const httpLink = createHttpLink({
//ISSUE: https://github.com/apollographql/apollo-link/issues/513
fetch: fetch as any,
uri: 'https://api.graph.cool/simple/v1/swapi',
})
const client = new ApolloClient({
link: httpLink,
cache: new InMemoryCache(),
})
export default client
æçµçã«åé¡ãšãªãã®ã¯ã node-fetch
ãšé¢é£ããåãæå³çã«ä»æ§ããéžè±ããŠããããšã§ãã
// copied directly from the @types/node-fetch
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node-fetch/index.d.ts
Request {
...
// node-fetch extensions to the whatwg/fetch spec
agent?: Agent | ((parsedUrl: URL) => Agent);
compress: boolean;
counter: number;
follow: number;
hostname: string;
port?: number;
protocol: string;
size: number;
timeout: number;
ãããã£ãŠããããã®è¿œå ãçç¥ããæ°ããåãããŠã³ãã£ã¹ããŸãã¯äœæããªãå Žåãã³ã³ãã€ã©ãšã©ãŒã¯é¿ããããŸããã
node-fetch
ãããŠã³ãã£ã¹ãããããšãéžæããŸããïŒ
import nodeFetch from 'node-fetch'
import { WhatWgFetch } from '../src/interfaces' // export type WhatWgFetch = typeof fetch
const fetch = (nodeFetch as unknown) as WhatWgFetch
ããã¯çŽ æŽãããããšã§ã¯ãããŸããããnode-fetchïŒ= fetchãããã¯...äžå¹³ãèšã...誀解ãæãæãããããŸãã node-fetch-like
æ¹ãé©åã ã£ãå¯èœæ§ããããããŒããã§ããã«æºæ ããç¶æ
ãç¶æããããã«ããŒã¹ããŒããã§ããã®å®è£
ã«æ¡åŒµæ©èœã_out_æ®ããŠãããšãç§ãã¡ã®ãããªãã®ã«æå©ã«ãªããŸã:)
ããã¯å®å šã«ç¡é¢ä¿ãããããŸãã...ããã...æšæ¥ããããã§åãåé¡ããããŸãã...ããã§ãããã¯ãã©ãŠã¶ã§å©çšã§ãããã®ã§ããããµãŒããŒåŽã§ã¯å©çšã§ããªãããšãç解ããããã ãã§ãã ã¢ããªã±ãŒã·ã§ã³ããã©ãŠã¶ãããã§ãããå®è¡ããå ŽåããµãŒããŒåŽã§æ¥ç¶ãäºåã«ã¬ã³ããªã³ã°ããã®ã¯ç¡æå³ã§ãã
ç§ã®å ŽåãNextJSã䜿çšããŠã httpsïŒ //nextjs.org/docs#with -no-ssrã®ããã«ããµãŒããŒåŽã§http-linkãã¬ã³ããªã³ã°ãããªãããã«ããå¿ èŠãããã³ã³ããŒãã³ããå€æŽããŸããã
node-fetch
ãš@types/node-fetch
ãã€ã³ã¹ããŒã«ãã代ããã«ãåãåé¡ãçºçããŠããŸããã apollo-env
ãšbamãçŽæ¥äœ¿çšããŸããã ãã¹ãŠã®ãšã©ãŒããªããªããŸããïŒ
ç§ã¯ãããå圢ãã§ããã§åäœãããããšãã§ããŸãã
"apollo-link": "1.2.3",
"apollo-link-context": "1.0.9",
"apollo-link-http": "1.3.1",
"aws-appsync": "^3.0.2",
"isomorphic-fetch": "^2.2.1",
...
"@types/isomorphic-fetch": "0.0.35",
declare function fetch(
input?: RequestInfo,
init?: RequestInit
): Promise<Response>;
declare interface GlobalFetch {
fetch(input: RequestInfo, init?: RequestInit): Promise<Response>;
}
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"lib": [ "dom", "es6", "esnext.asynciterable" ]
...
},
"types": [ "node", "aws-sdk" ],
"include": [ "./src/**/*.ts" ],
...
import * as fetch from 'isomorphic-fetch';
const client = new AWSAppSyncClient(appSyncClientOptions, {
link: createAppSyncLink({
...appSyncClientOptions,
resultsFetcherLink: ApolloLink.from([
createHttpLink({
fetch: fetch as GlobalFetch['fetch'],
uri: appSyncClientOptions.url
})
])
})
});
cross-fetch
æ€èšããŠãã ããïŒããã¯ãtypescriptãšããŒãïŒãã©ãŠã¶ãŒã§ã¯ãªãïŒã§æ©èœããŸãã
import fetch from 'cross-fetch'
const httpLink = new HttpLink({
uri: "<your-uri>",
fetch,
})
æãåèã«ãªãã³ã¡ã³ã
ïŒ2018幎5æ7æ¥ã®ãã©ããŒã¢ããïŒ
cross-fetch
TypeScriptã¿ã€ãå®çŸ©ãä»å±ããããã«ãªããŸããïŒcross-fetch
ã¯ãTypeScriptåå®çŸ©ããŸã ä»å±ããŠããŸããïŒ@DefinitelyTypedãä»å±ããŠããŸããïŒãlquixada / cross-fetchïŒ12ãããŒãžãããã®ãéãcross-fetch
ã代æ¿æ段ã§ãããçŸæç¹ã§ã¯ãTypeScriptã®HttpLink
ã§isomorphic-fetch
ãæ£åžžã«æ©èœããŠããããšãããããŸããã