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
?
import { createHttpLink } from 'apollo-link-http';
import fetch from 'node-fetch';
const link = createHttpLink({
fetch,
uri: this.endPoint.toString(),
});
@dejayc createHttpLinkλ₯Ό μ¬μ©ν λλ λμΌν μ€λ₯κ° λ°μν©λλ€.
μ¬μ€, λλ λ¬Έμ λ₯Ό μμ λμ΅λλ€. node-fetch 2.x λ _apollo-link_μ νΈνλμ§ μμ΅λλ€. _fetch_μ μλͺ μ΄ λ€λ¦ λλ€.
node-fetch 2.xλ apollo-linkμ νΈνλμ§ μμ΅λλ€. fetchμ μλͺ μ΄ λ€λ¦ λλ€.
μ, λΉμ μ΄ κ·Έκ²μ νμΉ ν μ μλ€κ³ μκ°ν©λλ€.
μ€λ λ΄ λν μ±μμ μ΄ λ¬Έμ λ₯Ό λ°κ²¬νμ΅λλ€. λλ νΉν μμ¦ 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
κ° λμμ΄μ§λ§, νμ¬ isomorphic-fetch
HttpLink
κ° TypeScriptμμ
import { HttpLink } from 'apollo-boost'
import fetch from 'isomorphic-fetch'
const link = new HttpLink({
fetch
})
npmjs.com λ¬Έμλ μ¬μ©μκ° node-fetchλ₯Ό μ¬μ©νλλ‘ κΆμ₯νμ§λ§ μ΄λ€ λ²μ μ μ§μ νμ§λ μμ΅λλ€. ν΄λΉ λ¬Έμλ₯Ό μ΄λ»κ² μ λ°μ΄νΈν μ μμ΅λκΉ?
μ°Έκ³ : whatwg-fetch
λ₯Ό μ¬μ©νλ κ²μ΄ μ€ν κ°λ₯ν ν΄κ²° λ°©λ²μ
λλ€.
μ΄μ λν μμ§μμ΄ μμμ΅λκΉ?
μ¬μ© cross-fetch
κ·Έλ₯ μ¬μ©νκΈ° λλ¬Έμ μ΄μν ν΄κ²° λ°©λ²μ²λΌ 보μΈλ€ node-fetch
. μ¬μ€ κ·Έκ² λ¬Έμ κ° μλκ°μ?
whatwg-fetch
λ μλ² μΈ‘μμ ν΄κ²° λ°©λ²μ΄ μλλλ€.
λκ΅°κ°κ° λͺ¨λ κ°μ Έμ€κΈ° ꡬνμμ μλνμ§λ§ μΌλΆ μ ν κ²μ¬λ₯Ό μ μ§νλ μ¬λ°λ₯Έ μ ν μ μκ° ν¬ν¨λ PRμ μ΄λ©΄ @stubailo μ μκ² μ°λ½ν΄ μ£ΌμΈμ. λ³ν©μ μλνκ² μ΅λλ€!
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
μ νμ κ°μ λ₯Ό μ¬μ©ν μ μμ΅λλ€ ...
λ¬Όλ‘ μ
λλ€. κ·Έλ¬λ μ΄μ¨λ λ€λ₯Έ κ²μΌλ‘ κ°μ μΊμ€ν
νλ €λ κ²½μ° μ νμ μ€μΉνλ μμ μ 무μμ
λκΉ?
μ΄ κ²½μ° λ΄ μ루μ
μ μ¬μ©ν μλ μμ΅λλ€.
μ£μ‘ν©λλ€. λΉμ μ΄ ν μΌμ μλͺ» μ½μμ΅λλ€. μ΄μ μ견μ 무μνμμμ€.
κ·Έλ¬λ "lib" νλμ "dom"μ΄ μλ κ²½μ°μλ§ GlobalFetchκ° μ 곡λλ―λ‘ λΈλΌμ°μ λ₯Ό λμμΌλ‘ νλλ‘ tsconfigλ₯Ό μμ ν΄μΌ ν©λλ€. https://github.com/apollographql/apollo-link/issues/ 273#μ΄μ λκΈ -347878009
λμ§Έ, .d.ts
μλμΌλ‘ μμ νλ λμ GlobalFetch['fetch']
λ₯Ό HTTPLink μμ±μμ μ λ¬ν μ μμ΅λκΉ? κ·Έλ¬λ©΄ ν¨μ¬ λ μ¨κ²¨μ§λλ€.
λ¬Όλ‘ , νμ§λ§ HttpLinkμ μ
λ ₯ μ λ°λ₯΄λ €λ©΄ GlobalFetch['fetch']
κ΄κ³μμ΄ μ¬μ©ν΄μΌ νλ―λ‘ ν΄λΉ μꡬ μ¬νμ ν΄κ²°ν λ°©λ²μ΄ μμ΅λλ€.
λλ λ°λ₯΄μ§ μλλ€. GlobalFetch['fetch']
λ λ³μκ° μλ μ νμ
λλ€.
node-fetchλ₯Ό κ°μ Έμ¨ λ€μ GlobalFetch['fetch']
λ‘ μΊμ€ν
νλ κ²μ μλ―Έν©λκΉ?
λ΄ νλ‘μ νΈμμ noImplictAny
νμ±νλμ΄ μμΌλ―λ‘ μ μκ° μλ νλͺ©μ κ°μ Έμ¬ μ μκΈ° λλ¬Έμ μ΅μ
μ΄ μλλλ€.
μ μκ² λ§λ ν΄κ²° λ°©λ²μΌ λΏ π€·ββοΈ, νμ§λ§ μλ²½νμ§ μλ€λ κ²μ κΉ¨λ¬μμ΅λλ€(ν€μλ: _workaround_).
μ’μ ν΄κ²°μ±
μ νμ΄νμ GlobalFetch['fetch']
μ ν©μ§ν©κ³Ό λ
Έλ κ°μ Έμ€κΈ° λ΄λ³΄λ΄κΈ°μ κΈ°λ³Έ μ νμΌλ‘ λ³κ²½νλ κ²μ
λλ€.
λλ κΆμ₯ λΌμ΄λΈλ¬λ¦¬λ₯Ό GlobalFetch['fetch']
(whatwg-fetch λλ κΈ°ν)λ₯Ό μ€μνλ λ
Έλ κ°μ Έμ€κΈ° λΌμ΄λΈλ¬λ¦¬λ‘ λ³κ²½νμμμ€.
μ, κ·Έκ² νμνμ§ λͺ°λμ΅λλ€. TIL.
μ£μ‘ν©λλ€. κ·Έλ₯ μ¬λ λ μ΄μμ. λ§μ΅λλ€. κ·Έκ²μ λ³μκ° μλλΌ μ νμ λλ€.
https://www.npmjs.com/package/whatwg-fetch λ "μ΄ νλ‘μ νΈλ Node.js νκ²½μμ μλνμ§ μμ΅λλ€. μΉ λΈλΌμ°μ μ μ©μ λλ€. μ ν리μΌμ΄μ μ΄ ν¨ν€μ§λ₯Ό λ§λ€κ³ μλ²μμ μ€ννμμμ€."
μ, λκ΅°κ° μ νμ μμ νμ¬ λ²κ·Έλ₯Ό μ¬λ°λ₯΄κ² μμ ν΄μΌ ν©λλ€.
μμΈν μ΄ν΄λ³΄λ μ£Όλ‘ node-fetch
μ λ¬Έμ μΈ κ² κ°μμ μμ λ¬Έμ λ₯Ό μ΄μ΄λ΄€μ΅λλ€. κ·Έλ¬λ κ·Έλ€μ΄ μ°λ¦¬μκ² μ°λ¦¬μ μ νμ λ³κ²½νλΌκ³ λ§ν μλ μμ΅λλ€.
μ’μμ, node-fetch
λ μ°λ¦¬μκ² μ νμ λ³κ²½νλΌκ³ λ§νλ κ² κ°μ΅λλ€. ν κ°μ§ ν΄νΉμ 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 : λλ μ¬μ©
"μν΄λ‘ ν΄λΌμ΄μΈνΈ": "^2.4.12",
"μν΄λ‘ νκ²½": "^0.3.2",
"μν΄λ‘ λ§ν¬-http": "^1.5.9",
λ΄ μ±μμ μ’ μμ± κ΄λ¦¬λ₯Ό μν΄ μμ¬λ₯Ό μ¬μ©ν©λλ€.
λ΄ νλ‘μ νΈμ λν΄ 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"
λ¬Έμ μν΄λ‘ νμ ν¨μΉνμμμ€! μ΄ λ¬Έμ λ 1λ μ΄ λμμ΅λλ€.
@rlancer μμμ μΈκΈνλ―μ΄ apollo-env
μ ν¨μΉκ° νμνκΈ° λλ¬Έμ μμ§ μμ λμ§ μμκΈ° λλ¬Έμ μ΄ κ²½μ°λΌκ³ κ°μ ν©λλ€. μ΄κ²μ μ΄ μ μ₯μκ° μλλΌ apollo-tooling
μ μ₯μμ μμ΅λλ€.
@JoviDeCroock μλνλ μ루μ μ΄ μμ΅λλ€. λ¬Έμλ μ€ν¨νκ³ ν΄κ²° λ°©λ²μ μν΄ μ¬λλ€μ Googleμ κ°μ λ‘
@JoviDeCroock apollo-env
λ° apollo-tooling
λͺ¨λ λμΌν κ°μ Έμ€κΈ° μ ν μ μΈμ κ°μ§λ©° λ λ€ HttpLinkκ° μμνλ Global['fetch']μ λ€λ¦
λλ€. κ·Έλ¬λ μ΄κ²μ λ¬Έμ κ° μλ κ² κ°μ΅λλ€. apollo-env
μ λμΌν μ μΈμΌλ‘ λͺ¨λμ μ§μ μ μΈνλ©΄ μ νμ λν΄ λΆννμ§ μμ΅λλ€. λ΄λ³΄λ΄κΈ°κ° μλνμ§ μμ μ μμ΅λλ€.
κΈμ, λλ κ°μΈμ μΌλ‘ μ΄κ²μ μ¬μ©νμ§ μμ΅λλ€. λΉμ μ΄ λμκ² ν΄κ²°μ± μ μλ €μ€ μ μλ€λ©΄ κΈ°κΊΌμ΄ ν보ν κ²μ λλ€.
μ΄ 2κ°μ§ μ νμ λμΌνκ² λ§λ€μ΄μ μλν΄μΌ νλ€κ³ μκ°ν©λλ€.
declare function fetch(
input?: RequestInfo, ---> remove ?
init?: RequestInit
): Promise<Response>;
declare interface GlobalFetch {
fetch(input: RequestInfo, init?: RequestInit): Promise<Response>;
}
@jmca κ³ λ§μμ, λΉμ μ μ ν루λ₯Ό ꡬνμ΅λλ€.
@tafelito ν΄κ²°ν΄ μ£Όμ
μ κ°μ¬ν©λλ€. λ§€μ° λμμ΄ λμμ§λ§ μ€μ μ€λ₯κ° λ°κ²¬λμμΌλ©° μλ‘μ΄ TypeScript μ
λ°μ΄νΈλ‘ μΈν κ² κ°μ΅λλ€. 3.6 λ²μ μμλ GlobalFetch
κ° μ κ±°λκ³ λμ WindowOrWorkerGlobalScope
κ° μ¬μ©λλ―λ‘ package.json "typescript": "3.5.1"
μ’
μμ±μμ λ²μ μ λ«μμΌ ν©λλ€.
μ¬κΈ° λ§ν¬κ° μμ΅λλ€
μμ μ견μ λ°μνκΈ° μν΄ λ΄ μ μ₯μλ v3.5.3
typescriptλ₯Ό κ³ μν΄μΌ ν©λλ€.
μ΄ 2κ°μ§ μ νμ λμΌνκ² λ§λ€μ΄μ μλν΄μΌ νλ€κ³ μκ°ν©λλ€.
declare function fetch( input?: RequestInfo, ---> remove ? init?: RequestInit ): Promise<Response>; declare interface GlobalFetch { fetch(input: RequestInfo, init?: RequestInit): Promise<Response>; }
λ λ²μ§Έ λΆλΆλ§ μ¬μ©νμ¬ κ³ μΉ μ μμμ΅λλ€. @tafelito κ°μ¬ν©λλ€.
κ°μ λ¬Έμ .
"@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, which ...grumbles...λ μ€ν΄μ μμ§κ° μμ΅λλ€. node-fetch-like
κ° λ μ μ νμ μ μμΌλ©° κΈ°λ³Έ λ
Έλ κ°μ Έμ€κΈ° ꡬνμ νμ₯μλ₯Ό _out_ λ¨κ²¨λμ΄ λ
Έλ κ°μ Έμ€κΈ°λ₯Ό μ€μνλλ‘ νλ€λ©΄ μ°λ¦¬μκ² νΈμλ₯Ό 보μμ κ²μ
λλ€. :)
μ΄κ²μ μμ ν κ΄λ ¨μ΄ μμ μλ μμ΅λλ€ ...νμ§λ§ ... λ§μ§λ§ λ μ λμ κ°μ λ¬Έμ κ°μμμ΅λλ€ ... 'κ°μ Έμ€κΈ°'κ° λΈλΌμ°μ μμ μ¬μ©ν μ μμ§λ§ μλ² μΈ‘μμλ μ¬μ©ν μ μλ€λ κ²μ κΉ¨λ¬μμ΅λλ€. μ ν리μΌμ΄μ μ΄ λΈλΌμ°μ μμ κ°μ Έμ€κΈ°λ₯Ό μννλ €λ κ²½μ° μλ² μΈ‘μμ μ°κ²°μ 미리 λ λλ§νλ κ²μ 무μλ―Έν©λλ€.
μ κ²½μ°μλ NextJSλ₯Ό μ¬μ©νμ¬ https://nextjs.org/docs#with -no-ssrμ λ°λΌ http-linkκ° μλ² μΈ‘μμ λ λλ§ λμ§ μλλ‘ ν΄μΌ νλ κ΅¬μ± μμλ₯Ό λ³κ²½νμ΅λλ€.
node-fetch
λ° @types/node-fetch
λ₯Ό μ€μΉνλ λμ apollo-env
λ° bamμΌλ‘ λ°λ‘ μ΄λνμ΅λλ€! λͺ¨λ μ€λ₯κ° μ¬λΌμ‘μ΅λλ€!
λλ μ΄κ²μ isomorphic-fetchλ‘ μμ νλλ‘ κ΄λ¦¬νμ΅λλ€.
"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 λ° node(λΈλΌμ°μ κ° μλ)μμ μλν©λλ€.
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
κ° λμμ΄μ§λ§, νμ¬isomorphic-fetch
HttpLink
κ° TypeScriptμμ