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?
In order to set up Relay we will need first to install Relay on our React Native application and then prepare the Relay environment.
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.
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.
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.
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:
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 ).
"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.
{ "presets": [ "./scripts/babelRelayPlugin", "react-native" ], "passPerPreset": true }
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.
npm install
You can find a working version in here, until this will be solved.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With