Apollo-link: Critical error when simply importing the library

Created on 17 Sep 2017  ·  9Comments  ·  Source: apollographql/apollo-link

Intended outcome:
I'm just trying to import the following into my React Native app, as seen in the docs:

import {execute, HttpLink} from 'apollo-link';

Actual outcome:
However, upon simply injecting this line, my React Native app immediately errors:

<Error>: Can't find variable: self
<Critical>: Unhandled JS Exception: Can't find variable: self
<Error>: undefined is not an object (evaluating 'require(487      ).default')
<Critical>: Unhandled JS Exception: undefined is not an object (evaluating 'require(487      ).default')
<Error>: Requiring module "myapp/src/config/Apollo.js", which threw an exception: Error: Requiring module "917", which threw an exception: ReferenceError: Can't find variable: self
<Error>: Requiring module "917", which threw an exception: ReferenceError: Can't find variable: self

How to reproduce the issue:
All I did was attempt to import the library, so I imagine that's all that is required.

Any insight as to what might be wrong? Some more info:

apollo-link version: 0.5.4
react-apollo version: 1.4.15
react-native version: 0.48.1
react version: 16.0.0-alpha.12

Most helpful comment

@CaryLandholt

It depends on your project's setup. I have placed this line at the top of App.js. Or you could place it at the top of both index.android.js and index.ios.js.

// Fixes isomorphic-fetch
GLOBAL.self = GLOBAL;

Doing this before any imports of isomorphic-fetch will monkey patch the GLOBAL object for React Native.

All 9 comments

This library depends on apollo-fetch and apollo-fetch depends on isomorphic-fetch.

isomorphic-fetch has issues with React Native...

Oh hm... Guess I'll just have to wait out a fix then. Thanks for the insight!

@Nickersoft if you don't feel like waiting, or rolling your own ApolloLink, do this...
https://github.com/matthew-andrews/isomorphic-fetch/issues/125#issuecomment-310717036

@affablebloke are you suggesting forking isomorphic-fetch?

if not, where do you place the following _fix_?

image

Cheers!

@CaryLandholt

It depends on your project's setup. I have placed this line at the top of App.js. Or you could place it at the top of both index.android.js and index.ios.js.

// Fixes isomorphic-fetch
GLOBAL.self = GLOBAL;

Doing this before any imports of isomorphic-fetch will monkey patch the GLOBAL object for React Native.

@affablebloke worked perfectly - thanks!

The apollo-link-http no longer uses global fetch so this shouldn't be a problem! I'll be updating the batching fetch in the new few weeks to do the same!

@jbaxleyiii would be really nice to have batch use the global fetch too!

is batch-http now using global fetch? still facing this error with [email protected]
@jbaxleyiii

Was this page helpful?
0 / 5 - 0 ratings