简体   繁体   English

使用relay in react native app

[英]Using relay in react native app

How to expose a graphql endpoint to react native app? 如何公开graphql端点以响应本机应用程序? 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: 中继技术概述中的示例https://facebook.github.io/relay/docs/getting-started.html使用webpack来服务中继应用并将其暴露给graphql服务器:

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; 但本机使用react-native packager来捆绑其模块; has anyone tried using relay in the react native app? 有没有人试过在本机应用程序中使用中继?

It is now possible to use react native and relay together. 现在可以使用本机反应和继电器。

Github announcement: https://github.com/facebook/relay/issues/26 Github公告: 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 . 现有RN应用程序的说明: 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. 确保您已准备好GraphQL服务器并且手头上还有schema.json。 For more details about the latter two visit the React-Relay project page. 有关后两者的更多详细信息,请访问React-Relay项目页面。
  3. Ensure that you're using `npm` version 3 or greater. 确保您使用的是`npm`版本3或更高版本。
  4. If React Native's packager (`react-native start`) is running somewhere in the background, you should stop it now. 如果React Native的打包器(`react-native start`)在后台的某个地方运行,你应该立即停止它。
  5. Run: 跑:
     watchman watch-del-all 守望者看全部 
    and also: 并且:
     rm -rf $TMPDIR/react-* rm -rf $ TMPDIR / react- * 
    to avoid running into a known packager issue ( https://github.com/facebook/react-native/issues/4968 ). 避免遇到已知的打包者问题(https://github.com/facebook/react-native/issues/4968)。
  6. Delete the `./node_modules` directory from your project. 从项目中删除`。/ node_modules`目录。
  7. Edit your `package.json` file, make sure it has the following: 编辑你的`package.json`文件,确保它有以下内容:
     "dependencies": { “依赖”:{\n      "react": "^0.14.7", “反应”:“^ 0.14.7”,\n      "react-native": "facebook/react-native", “react-native”:“facebook / react-native”,\n      "react-relay": "^0.7.3" “react-relay”:“^ 0.7.3”\n    }, },\n    "devDependencies": { “devDependencies”:{\n      "babel-core": "^6.6.4", “babel-core”:“^ 6.6.4”, \n      "babel-preset-react-native": "^1.4.0", “babel-preset-react-native”:“^ 1.4.0”,\n      "babel-relay-plugin": "^0.7.3" “babel-relay-plugin”:“^ 0.7.3”   \n    } } 
    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. 确保您的项目使用babel 6.5或更高版本,我们需要它用于.babelrc文件中的passPerPreset功能。
  8. Create a new file `.babelrc` and place it in your project's directory: 创建一个新文件`.babelrc`并将其放在项目的目录中:
     { {\n      "presets": [ “预设”:[\n        "./scripts/babelRelayPlugin", “./scripts/babelRelayPlugin”\n        "react-native" “反应天然的”\n      ], ]\n      "passPerPreset": true “passPerPreset”:是的\n    } } 
  9. Create a new file in your project's directory called `babelRelayPlugin.js` with the following content: 在项目目录中创建一个名为`babelRelayPlugin.js`的新文件,其中包含以下内容:
     const getBabelRelayPlugin = require('babel-relay-plugin'); const getBabelRelayPlugin = require('babel-relay-plugin');\n    const schema = require('./schema.json'); const schema = require('./ schema.json');\n\n    module.exports = { plugins: [getBabelRelayPlugin(schema.data)] }; module.exports = {plugins:[getBabelRelayPlugin(schema.data)]}; 
    Copy your `schema.json` file to the project's directory too. 将`schema.json`文件复制到项目目录中。
  10. Run: 跑:
     npm install npm安装 

你可以在这里找到一个工作版本,直到这个问题得到解决。

  1. you need to install react-relay package for react-native app 你需要为react-native app安装react-relay

  2. Before the entry point of your react-native app, inject network layer with the url you exposed 在您的react-native应用程序的入口点之前,使用您公开的URL注入网络层

 Relay.injectNetworkLayer( new Relay.DefaultNetworkLayer('http://localhost:8000/graphql') ); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM