question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
mquandallecommented, Apr 20, 2016

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?

0reactions
strotechcommented, Feb 1, 2019

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"; }

Read more comments on GitHub >

github_iconTop Results From Across the Web

Polyfilling the Fetch API for Old Browsers and Node.js
First some definitions. A polyfill will try to emulate certain APIs, so can use them as if they were already implemented. A transpiler...
Read more >
node-fetch-polyfill - npm
A light-weight module that brings window.fetch to node.js. Latest version: 2.0.6, last published: 6 years ago. Start using ...
Read more >
Using the fetch API inside Node.js - Christoph Zauner's Blog
Webpack config · In the client bundle we include a fetch polyfill for older browsers ( whatwg-fetch ). · We tell the webpack-node-externals...
Read more >
How to use the GitHub Fetch API polyfill - Stack Overflow
First, install using npm install --save whatwg-fetch. then use: require('whatwg-fetch'). You can alternatively use import syntax import 'whatwg-fetch'.
Read more >
fetch-polyfill | Yarn - Package Manager
This module is out of maintanance, use fetch-ie8 instead. The global fetch function is an easier way to make web requests and handle...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found