Sub*_*tel 6 reactjs graphql react-native react-apollo apollo-client
我有一个组件名称,Graph.js代码如下:
import React, { Component } from "react";
import { View, Text } from "react-native";
import ApolloClient from "apollo-boost";
import { ApolloProvider } from "react-apollo";
import Launches from "./Launches.js";
const client = new ApolloClient({
uri: "http://127.0.0.1:4000/graphql"
});
export default class Graph extends Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
return (
<ApolloProvider client={client}>
<View>
<Text>Hello Apillo</Text>
</View>
<Launches />
</ApolloProvider>
);
}
}
Run Code Online (Sandbox Code Playgroud)
另一个文件名是Launcher.js下面给出的代码:
import React, { Component } from "react";
import { View, Text } from "react-native";
import gql from "graphql-tag";
import { Query } from "react-apollo";
const LAUNCHES_QUERY = gql`
query LaunchesQuery {
launches {
flight_number
mission_name
}
}
`;
export default class Launches extends Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
return (
<View>
<Query query={LAUNCHES_QUERY}>
{({ loading, error, data }) => {
if (loading) return <Text>Loading...</Text>;
if (error) return console.log(error);
let length = data.length;
console.log(data);
return <Text>Launches</Text>;
}}
</Query>
</View>
);
}
}
Run Code Online (Sandbox Code Playgroud)
我graphql在本地主机中设置了setup,并在中运行良好http://localhost:4000/graphql。
当我从react-native应用程序调用URI时,它会生成错误。
ERROR1:网络错误:网络请求失败
误差2:不变违规:查询(...):没有从渲染返回。这通常意味着缺少return语句。或者,不渲染任何内容,则返回null。
我也尝试更改URI http://localhost并赋予http://127.0.0.1
提前致谢
小智 1
查询组件必须位于 之外,它将是:
...
render() {
return (
<Query query={LAUNCHES_QUERY}>
{({ loading, error, data }) => {
if (loading) return <Text>Loading...</Text>;
if (error) return console.log(error);
let length = data.length;
console.log(data);
return <Text>Launches</Text>;
}}
</Query>
);
}
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
481 次 |
| 最近记录: |