gpt4 book ai didi

react-native - 无法在React Native上使用Relay工作

转载 作者:行者123 更新时间:2023-12-04 04:29:42 25 4
gpt4 key购买 nike

我正在尝试设置一个基本的React Native Relay项目。我花了很多时间尝试调试问题而没有运气之后,才创建了这个问题。我在控制台上收到此警告,并且我的react native应用无法调用GraphQL服务器。

Warning: RelayNetworkLayer: Call received to injectImplementation(), but a layer was already injected.


Running application "ExampleApp" with appParams: {"rootTag":1,"initialProps":{}}. __DEV__ === true, development-level warning are ON, performance optimizations are OFF

fetchWithRetries(): Still no successful response after 1 retries, giving up.

这是我的代码

index.ios.js
import React, { Component } from 'react';
import Relay from 'react-relay';

import {
AppRegistry,
StyleSheet,
Text,
View
} from 'react-native';
import App from './app';
import AppRoute from './route/appRoute';

Relay.injectNetworkLayer(
new Relay.DefaultNetworkLayer("http://localhost:1337/graphy")
);

class ExampleApp extends Component {
render() {
return (
<Relay.RootContainer
Component={App}
route={new AppRoute()}
/>
);
}
}

AppRegistry.registerComponent('ExampleApp', () => ExampleApp);

appRoute.js
'use strict';

import Relay from 'react-relay';

export default class extends Relay.Route {
static queries = {
page: (Component) => Relay.QL`
query {
posts(offset: 0) { ${Component.getFragment('page')} }
}
`,
};

static routeName = 'AppRoute';
}

app.js
'use strict';

import React, {Component} from 'react';
import Relay from 'react-relay';

import {
View,
Text,
} from 'react-native';

class App extends Component {

render() {
console.log('props', this.props);
return(
<View>
<Text>Testing</Text>
</View>
);
}
}


export default Relay.createContainer(App, {
fragments: {
page: () => Relay.QL`
fragment on Viewer {
posts { id, title }
}
`,
}
});

package.json
{
"name": "ExampleApp",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start"
},
"dependencies": {
"babel": "^6.5.2",
"babel-preset-react-native": "^1.8.0",
"babel-relay-plugin": "^0.8.1",
"react": "^0.14.8",
"react-native": "^0.25.1",
"react-relay": "^0.8.1"
}
}

babelRelayPlugin.js
const getBabelRelayPlugin = require('babel-relay-plugin');
const schema = require('../schema.json');

module.exports = { plugins: [getBabelRelayPlugin(schema.data)] };

这是完整的 repo 链接: https://github.com/popstand/react-native-relay-graphql-example

用ReactJS和Relay构建的类似代码在网页上效果很好。我无法使其在React Native上运行。这可能是什么问题导致了此问题?

最佳答案

我建议您可以检查F8 App by Facebook。肯定有一个setup scriptmain routerelay containera schema可以看到。

关于react-native - 无法在React Native上使用Relay工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37292331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com