I'm building a CLI tool with apollo-link-http and when I try to build it I get an error:
node_modules/apollo-link-http/lib/httpLink.d.ts(4,13): error TS2304: Cannot find name 'GlobalFetch'.
Here is my code:
#!/usr/bin/env node
import { HttpLink } from "apollo-link-http";
import { execute } from "apollo-link";
import * as fetch from "node-fetch";
const link = new HttpLink({ uri: ..., fetch });
execute(link, {
query: ...,
variables: ...,
}).subscribe({
next: data => console.log(`received data ${data}`),
error: error => console.log(`received error ${error}`),
complete: () => console.log(`complete`),
});
dependencies:
{
"dependencies": {
"apollo-client": "^2.0.3",
"apollo-link": "^1.0.3",
"apollo-link-http": "^1.2.0",
"graphql": "^0.11.7",
"graphql-tag": "^2.5.0",
"node-fetch": "^1.7.3"
},
"devDependencies": {
"@types/node": "^8.0.53",
"@types/node-fetch": "^1.6.7",
"typescript": "^2.6.1"
}
}
tsconfig:
{
"compilerOptions": {
"module": "commonjs",
"outDir": "./lib",
"removeComments": true,
"rootDir": "./src",
"target": "ESNEXT",
"typeRoots": [
"./node_modules/@types"
],
"lib": [
"ES6",
"ESNEXT"
]
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules"
]
}
Am I missing something or it's a bug?
@longuniquename first of all, I'd love to know how you are using this for a CLI tool 馃槏
Second, you may need to add in types for the browser since GlobalFetch is provided by typescript when the lib targets the dom ("lib": ["es6", "dom"]) in our tsconfig
Running into this as well now. Including the "dom" lib doesn't seem to work either (if you're using node-fetch). It throws a compatibility flag. Didn't have this problem the other day. Recent change?
Following the client docs of apollo but stuck here
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 });
I installed and imported node-fetch but still getting same error
import { ApolloClient } from 'apollo-client';
import fetch from 'node-fetch';
import { HttpLink } from 'apollo-link-http';
import { InMemoryCache } from 'apollo-cache-inmemory';
const client = new ApolloClient(
{
ssrMode: true,
link: new HttpLink(),
cache: new InMemoryCache(),
fetch,
},
);
@pricetula I encountered the same error and was able to work around it by passing a reference to fetch to the HttpLink constructor:
import { ApolloClient } from 'apollo-client';
import { HttpLink } from 'apollo-link-http';
import { InMemoryCache } from 'apollo-cache-inmemory';
import fetch from 'node-fetch';
const client = new ApolloClient({
ssrMode: true,
link: new HttpLink({
uri: '/graphql',
fetch,
}),
cache: new InMemoryCache(),
});
Hmm, I'm not sure the best way to solve this. Does anyone know if there are uniform types for fetch? I'd be happy to have GlobalFetch replaced with an imported type if possible
same issue here...
and adding "dom" to tsc compiler options fixed it.
@orefalo so no code changes needed inside the package if you add that?
This should be fixed here. Thanks for resolving it @orefalo!
Most helpful comment
@pricetula I encountered the same error and was able to work around it by passing a reference to fetch to the HttpLink constructor: