relay icon indicating copy to clipboard operation
relay copied to clipboard

Make Relay work with React Native out of the box

Open qingfeng opened this issue 11 years ago • 199 comments

The remaining steps are:

  • [x] Relay/React Native/fbjs versioning
  • [x] Use the appropriate unstableBatchedUpdates depending on React/React Native
  • [x] Version of fetch polyfill for React Native
  • [ ] Document the use of babel-relay-plugin with React Native (see discussion at https://github.com/facebook/relay/pull/714#issuecomment-181204627)
  • [x] Create a fresh React Native project, set up Relay, configure the plugin per the documentation added in the previous step, and make sure everything works as expected.

Note: edited by @josephsavona

qingfeng avatar Aug 12 '15 05:08 qingfeng

have not tried yet, but looks like so. some examples from docs use react native components https://facebook.github.io/relay/docs/guides-containers.html#content

yongxu avatar Aug 12 '15 18:08 yongxu

Relay should work as-is on React Native - if not, feel free to let us know and re-open this!

josephsavona avatar Aug 12 '15 19:08 josephsavona

@josephsavona even if relay components aren't inheriting react-native's Component?

https://github.com/facebook/relay/blob/master/src/container/RelayContainer.js#L23

I'm getting Can't find variable: document in getActiveElement

amccloud avatar Aug 17 '15 07:08 amccloud

I also needed to include johanneslumpe/react-native-browser-polyfill to get around a similar issue Can't find variable: self in whatwg-fetch

amccloud avatar Aug 17 '15 08:08 amccloud

@josephsavona Relay cannot work as-is with React Native because Relay includes React web as a hard dependency, and including React web into React Native is not functional.

Here's a commit that shows the minimal changes to build relay against react-native: https://github.com/facebook/relay/commit/e0037c78931edc3286e5217894348ca8ece1225b

The 'dist' step of building relay still fails with that commit, with the error: image

I'm not sure exactly why that's failing.

The last two things we had to do to get relay working with react native was:

  • Commit relay's /lib directory since building dist failed (above)
  • stub process.env before including Relay in the react-native application, since process doesn't exist in react-native's JS environment. This is only necessary until the dist step of compilation works, since dist will remove all process.env references. I made a pseudo-module called 'relay',, which is a bit silly but functional:
process = {env: {}};

module.exports = require('react-relay');

boourns avatar Aug 25 '15 21:08 boourns

Last thing I forgot to mention regarding relay's compatibility with react-native:

relay assumes self is present. This is true in react-native only when debugging in Chrome. If you are not debugging in Chrome you will get an error self is not defined when including Relay. A solution is to add https://github.com/johanneslumpe/react-native-browser-polyfill to your react-native project.

boourns avatar Aug 25 '15 23:08 boourns

@josephsavona can we re-open this?

amccloud avatar Aug 29 '15 22:08 amccloud

@amccloud thanks for investigating, I'm reopening to give us a place to track this.

josephsavona avatar Aug 30 '15 18:08 josephsavona

The last thing we needed to do to get relay working with react-native was to use react-native's global fetch by deleting the var fetch = require('fbjs/lib/fetch'); line from lib/RelayDefaultNetworkLayer.js.

We've gotten basic requests working following the steps I've outlined above in my comments on this issue, but I'm not 100% certain other things aren't broken yet.

boourns avatar Aug 30 '15 19:08 boourns

@boourns that's great. I also had to delete var fetch = require('fbjs/lib/fetch') from the following library which was declared in lib/RelayDefaultNetworkLayer.js: var fetchWithRetries = require('fbjs/lib/fetchWithRetries');

clintonwoo avatar Aug 31 '15 05:08 clintonwoo

@boourns @clintonwoo Hello guys, followed your step all the way, however i get an error 'can't find variable fetchWithRetries', however when adding it i get 'cannot find variable: self'. Any tips would be greatly appreciated. github repo is https://github.com/almasakchabayev/reactNativeRelayWebpackDemo.

a9a4k avatar Sep 02 '15 14:09 a9a4k

@almasakchabayev follow this comment: https://github.com/facebook/relay/issues/26#issuecomment-134767414

boourns avatar Sep 02 '15 14:09 boourns

@boourns It worked when I changed fetchWithRetries in 'function _sendQuery' to just fetch. Great! Thanks a lot!

a9a4k avatar Sep 02 '15 15:09 a9a4k

@boourns Maybe a dumb question: What do you mean by: "Commit relay's /lib directory since building dist failed (above)" ?

lukasredev avatar Sep 04 '15 15:09 lukasredev

@lukasreichart

Ideally we would build relay against react-native, do a dist build which would result in a single relay.js file, which could be copied into the react-native project. The minimal changes to build relay against react-native are here: https://github.com/facebook/relay/commit/e0037c78931edc3286e5217894348ca8ece1225b

With that commit applied, the src to lib transpilation step succeeds, but doing a dist build of relay fails. See the screenshot posted in my original comment. So that commit is not sufficient in the long-term.

The proper solution will be to figure out why the dist build fails and making it not fail, but I don't really know what the problem is there yet.

In the meantime if you want to try relay with react native, the hack solution is to fork relay, apply https://github.com/facebook/relay/commit/e0037c78931edc3286e5217894348ca8ece1225b, commit /lib and then add that relay fork to your react-native project as an npm dependency. And then make the other changes I outline.

boourns avatar Sep 04 '15 15:09 boourns

@boourns Thanks you've directed me to the right path. But now I have a problem with the babel-relay-plugin. I'am using this script. But keep getting an Error: Invariant Violation: RelayQL: Unexpected invocation at runtime. Either the Babel transform was not set up, or it failed to identify this call site. Make sure it is being used varbatim as Relay.QL``

This error occurs when you use Relay.QL ( which should have been replaced by the babel-relay-plugin). The strange thing is, that in index.ios.bundle, all Relay.QLstatements have been replaced correctly.

Any ideas on this one?

lukasredev avatar Sep 10 '15 11:09 lukasredev

@lukasreichart in package.json for your project change the start command to be:

"start": "node_modules/react-native/packager/packager.sh --transformer ./lib/babelTransform.js"

And then you need to run your react-native packager via 'npm start' instead of running react-native start or using the packager that xcode boots up.

This is another workaround we had to apply, not sure how to make this just gracefully happen yet.

boourns avatar Sep 10 '15 14:09 boourns

@boourns Would you mind sharing the content of your ./lib/babelTransform.js file?

lukasredev avatar Sep 10 '15 14:09 lukasredev

Sorry, discussing with @jahfer now who did this portion of the work. Disclaimer again that this is hacks on hacks on hacks, not a robust solution to getting it working :)

What we did was copy the original react-native babel transformer from https://github.com/facebook/react-native/blob/master/packager/transformer.js into your project, and add the relay plugin:

    plugins: [require('./babelRelayPlugin.js')],

babelRelayPlugin.js comes from https://github.com/relayjs/relay-starter-kit

boourns avatar Sep 10 '15 14:09 boourns

I think my transform is working now. Thank you very much. @boourns Getting a new Error: fetch is not a function which is very strange, because fetch is globally defined in react native(?). Did you also encounter this error?

lukasredev avatar Sep 10 '15 17:09 lukasredev