javascript - 在 native 应用程序中使用中继

标签 javascript reactjs react-native relayjs

如何公开一个 graphql 端点来响应 native 应用程序?有没有人在 React Native 应用程序中使用中继?继电器技术概述中的实例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}`);
});

但是 react native 使用 react-native packager 来打包它的模块;有没有人试过在 react native 应用程序中使用中继?

最佳答案

现在可以同时使用 React Native 和 Relay。

Github 公告: https://github.com/facebook/relay/issues/26

现有 RN 应用的说明: http://pulse.mixrt.com/discussion/26/technical-making-relay-work-with-existing-react-native-apps-a-step-by-step-tutorial .

说明副本:

  1. 备份您的项目。
  2. 确保您已准备好 GraphQL 服务器和 schema.json。有关后两者的更多详细信息,请访问 React-Relay 项目页面。
  3. 确保您使用的是 `npm` 版本 3 或更高版本。
  4. 如果 React Native 的打包程序(`react-native start`)正在后台某处运行,您应该立即停止它。
  5. 运行:
    watchman watch-del-all
    还有:
    rm -rf $TMPDIR/react-*
    以避免遇到已知的打包程序问题 (https://github.com/facebook/react-native/issues/4968)。
  6. 从您的项目中删除 `./node_modules` 目录。
  7. 编辑您的 `package.json` 文件,确保它包含以下内容:
        "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 版本尤为重要。确保您的项目使用 babel 6.5 或更高版本,我们需要它用于 .babelrc 文件中的 passPerPreset 功能。
  8. 创建一个新文件 `.babelrc` 并将其放在您的项目目录中:
        {
          "presets": [
            "./scripts/babelRelayPlugin",
            "react-native"
          ],
          "passPerPreset": true
        }
  9. 在项目目录中创建一个名为“babelRelayPlugin.js”的新文件,内容如下:
        const getBabelRelayPlugin = require('babel-relay-plugin');
        const schema = require('./schema.json');
    
        module.exports = { plugins: [getBabelRelayPlugin(schema.data)] };
    将您的 `schema.json` 文件也复制到项目的目录中。
  10. 运行:
    npm install

关于javascript - 在 native 应用程序中使用中继,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32254923/

相关文章:

javascript - 客户打开web开发者工具时如何获取事件?

javascript - Amazon S3 REST API 大小不正确

javascript - 如何在 React JS 中将 {variable} 连接到 href 标签?

node.js - 如何设置 NODE_ENV?

javascript - 附加的克隆表单刷新页面但不应该

node.js - React Nodejs 项目中的多个 package.json 文件

javascript - React Native - 警告 : Can't perform a React state update on an unmounted component

iOS 应用程序 : Rejected in AppStore to external testers

javascript - 如何在 React Native Router Flux 中嵌套场景并使用方向 ='vertical' 导航?

javascript - 如何防止在 Angular 中上传某些类型的文件?