[英]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: 副本说明:
watchman watch-del-alland also:守望者看全部
rm -rf $TMPDIR/react-*to avoid running into a known packager issue ( https://github.com/facebook/react-native/issues/4968 ).rm -rf $ TMPDIR / react- *
"dependencies": {Babel version is especially important.“依赖”:{\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 }
}
{{\n "presets": [
“预设”:[\n "./scripts/babelRelayPlugin",
“./scripts/babelRelayPlugin”\n "react-native"
“反应天然的”\n ],
]\n "passPerPreset": true
“passPerPreset”:是的\n }
}
const getBabelRelayPlugin = require('babel-relay-plugin');Copy your `schema.json` file to the project's directory too.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)]};
npm installnpm安装
you need to install react-relay
package for react-native
app 你需要为
react-native
app安装react-relay
包
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.