Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using relay in react native app

How to expose a graphql endpoint to react native app? Has anyone used relay in react native application? The examples in technical overview of relay https://facebook.github.io/relay/docs/getting-started.html use webpack to serve relay app and expose it to a graphql server:

import express from 'express';
import graphQLHTTP from 'express-graphql';
import path from 'path';
import webpack from 'webpack';
import WebpackDevServer from 'webpack-dev-server';
import {StarWarsSchema} from './data/starWarsSchema';

const APP_PORT = 3000;
const GRAPHQL_PORT = 8080;

// Expose a GraphQL endpoint
var graphQLServer = express();
graphQLServer.use('/', graphQLHTTP({schema: StarWarsSchema, pretty: true}));
graphQLServer.listen(GRAPHQL_PORT, () => console.log(
  `GraphQL Server is now running on http://localhost:${GRAPHQL_PORT}`
));

// Serve the Relay app
var compiler = webpack({
  entry: path.resolve(__dirname, 'js', 'app.js'),
  eslint: {
    configFile: '.eslintrc'
  },
  module: {
    loaders: [
      {
        test: /\.js$/,
        loader: 'babel',
        query: {
          stage: 0,
          plugins: ['./build/babelRelayPlugin']
        }
      },
      {
        test: /\.js$/,
        loader: 'eslint'
      }
    ]
  },
  output: {filename: 'app.js', path: '/'}
});
var app = new WebpackDevServer(compiler, {
  contentBase: '/public/',
  proxy: {'/graphql': `http://localhost:${GRAPHQL_PORT}`},
  publicPath: '/js/',
  stats: {colors: true}
});
// Serve static resources
app.use('/', express.static('public'));
app.use('/node_modules', express.static('node_modules'));
app.listen(APP_PORT, () => {
  console.log(`Relay Star Wars is now running on http://localhost:${APP_PORT}`);
});

but react native uses react-native packager to bundle its modules; has anyone tried using relay in the react native app?

like image 369
Ilyas Malgazhdarov Avatar asked Aug 27 '15 16:08

Ilyas Malgazhdarov


People also ask

Can relay be used in React Native?

In order to set up Relay we will need first to install Relay on our React Native application and then prepare the Relay environment.

How does relay work React?

Relay is a JavaScript library that runs on the client-side. You connect Relay to your React components and then Relay will fetch data from your server. Of course, your server also needs to conform to Relay's protocol, and we'll talk about that, too. Relay is designed to be the data-backbone for your React app.

Should I use relay GraphQL?

Relay is recommended to use in the frontend to have more structured, modular, future-proofed applications that can scale easily to millions of users. The first thing that needs to be implemented in order to use Relay is to make a Relay-compatible GraphQL server. That's what we're going to do now.

What is Gql relay?

Relay is a framework for managing and declaratively fetching GraphQL data. It allows developers to declare what data each component needs via GraphQL, and then aggregate these dependencies and efficiently fetch the data in fewer round trips.


2 Answers

It is now possible to use react native and relay together.

Github announcement: https://github.com/facebook/relay/issues/26

Instructions for existing RN apps: http://pulse.mixrt.com/discussion/26/technical-making-relay-work-with-existing-react-native-apps-a-step-by-step-tutorial .

Copy of the instructions:

  1. Back up your project.
  2. Make sure you have your GraphQL server ready and your schema.json at hand too. For more details about the latter two visit the React-Relay project page.
  3. Ensure that you’re using `npm` version 3 or greater.
  4. If React Native's packager (`react-native start`) is running somewhere in the background, you should stop it now.
  5. Run:
    watchman watch-del-all
    and also:
    rm -rf $TMPDIR/react-*
    to avoid running into a known packager issue ( https://github.com/facebook/react-native/issues/4968 ).
  6. Delete the `./node_modules` directory from your project.
  7. Edit your `package.json` file, make sure it has the following:
        "dependencies": {
          "react": "^0.14.7",
          "react-native": "facebook/react-native",
          "react-relay": "^0.7.3"
        },
        "devDependencies": {
          "babel-core": "^6.6.4", 
          "babel-preset-react-native": "^1.4.0",
          "babel-relay-plugin": "^0.7.3"   
        }
    Babel version is especially important. Make sure that your project uses babel 6.5 or later, we need it for the passPerPreset feature in .babelrc file.
  8. Create a new file `.babelrc` and place it in your project's directory:
        {
          "presets": [
            "./scripts/babelRelayPlugin",
            "react-native"
          ],
          "passPerPreset": true
        }
  9. Create a new file in your project's directory called `babelRelayPlugin.js` with the following content:
        const getBabelRelayPlugin = require('babel-relay-plugin');
        const schema = require('./schema.json');
    
        module.exports = { plugins: [getBabelRelayPlugin(schema.data)] };
    Copy your `schema.json` file to the project's directory too.
  10. Run:
    npm install
like image 79
Walter Lace Avatar answered Oct 21 '22 19:10

Walter Lace


You can find a working version in here, until this will be solved.

like image 24
lenaten Avatar answered Oct 21 '22 17:10

lenaten