apollographql / apollo-client

:rocket:  A fully-featured, production ready caching GraphQL client for every UI framework and GraphQL server.
https://apollographql.com/client
MIT License
19.37k stars 2.66k forks source link

Document that `fetch` polyfill is required when using from Node #130

Closed batjko closed 8 years ago

batjko commented 8 years ago

Hi guys,

So I am trying to replace my graphql-js client in a Node app, with the apollo-client instead. To start off, I've basically copied the code snippet from the docs and just replaced the query with my own (which I know works). But now the query fails, jumping to the catch clause instead.

This is the example code, with a simplified version of my query:

const queryVars = {
  id: 2,
  bunit: 'TestUnit'
};

client.query({
    query: `query getStuff(
        $id: Int,
        $bunit: String
      ){
      forecasts (
        id: $id,
        bunit: $bunit
      ) {
          id,
          bunit
        }
    }
    `,
    variables: queryVars,
    forceFetch: false
  }).then((graphQLResult) => {
    const { errors, data } = graphQLResult;

    if (data) {
      console.log('got data', data);
    }
    if (errors) {
      console.log('got some GraphQL execution errors', errors);
    }
  }).catch((error) => {
    console.log('there was an error sending the query', error);
  });

I can only assume this fetch call is internal to apollo, since it's not part of my code. Do I need to have any other dependency installed by chance?

This is on Windows, btw.

mquandalle commented 8 years ago

Are you using IE by any chance? If yes, this is the issue discussed in #126, and basically you will need to bring some fetch polyfill to the client, for instance with https://www.npmjs.com/package/isomorphic-fetch.

batjko commented 8 years ago

This is in Node, not from a browser. I have a separate GraphQL service running, to which I am trying to connect from my node app. (I've updated the issue description now to point that out) Node 5.10.1, 64bit.

I've just seen the related issue as well, but I thought the isomorphic-fetch pakcage is essentially the problem?

mquandalle commented 8 years ago

So apparently Node v5 is also missing the fetch method:

$ nvm use node 5
$ echo "console.log(typeof fetch)" | node
undefined

I think that something the Apollo crew should fix. Meanwhile you can load a fetch polyfill from NPM (either isomorphic-fetch or maybe https://www.npmjs.com/package/node-fetch) and define it as a global variable (eg. fetch = require('node-fetch');) so that the Apollo-client code could use it.

mquandalle commented 8 years ago

Reading https://github.com/apollostack/apollo-client/pull/126#issue-149328412 again, it seems like the one-line import 'isomorphic-fetch' should be enough, did you try it?

batjko commented 8 years ago

Edited (Forget what I wrote in this comment earlier, I just hadn't changed my query variables to retrieve the right data.)

fetch = require('node-fetch') works, though a bit of a hack for now.

stubailo commented 8 years ago

I think we made an intentional decision to not include global polyfills in here, so changing this issue to be about documenting the polyfills required.

strotech commented 5 years ago

I had the same issue and i added es6-promise and isomorphic-fetch and now i am getting the error

self not defined

I created the following fetch.js file

` require('es6-promise').polyfill(); var fetch=require('isomorphic-fetch').fetch;

const { createApolloFetch } = require('apollo-fetch'); const fetchQL = createApolloFetch({ uri: 'http://localhost:4000/graphql', });

fetchQL({ query: '{testFunction}', }).then(res => { console.log(res.data.testFunction); }); `

with schema as type myQuery { testFunction: String! }

and resolver as myQuery:{ testFunction(){ return "returned from custom test function"; }