【发布时间】:2018-05-24 22:31:37
【问题描述】:
我一直在尝试使用 Apollo 连接到我的本地 GraphQL 服务器。下面是我使用react-apollo2.0 的尝试。但我也尝试过使用react-apollo1.4 和createNetworkInterface,但我得到了同样的错误。
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';
import { AppRegistry } from 'react-native';
import { HttpLink } from 'apollo-link-http';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { ApolloClient } from 'apollo-client';
import { ApolloProvider } from 'react-apollo';
const client = new ApolloClient({
link: new HttpLink({ uri: 'http://localhost:8000/graphql' }),
cache: new InMemoryCache()
});
function FooList({ data: { loading, posts } }) {
if (loading) {
return <Text>Loading</Text>;
} else {
return (
<List>
<ListItem>
<Left>
<Text>John Doe5</Text>
<Text note>(buyer)</Text>
</Left>
<Right>
<Text note>21 min ago</Text>
</Right>
</ListItem>
</List>
);
}
}
export default graphql(gql`
query allLeads {
id
name
}
`)(FooList);
const App = () => (
<ApolloProvider client={client}>
<FooList/>
</ApolloProvider>
);
export default App;
我得到的错误:
我的依赖。
"dependencies": {
"@expo/vector-icons": "^6.2.0",
"apollo-client-preset": "^1.0.4",
"expo": "^22.0.2",
"graphql": "^0.11.7",
"graphql-tag": "^2.6.0",
"native-base": "^2.3.3",
"react": "16.0.0-beta.5",
"react-apollo": "^2.0.4",
"react-link": "^1.0.3",
"react-native": "^0.49.5",
"react-navigation": "^1.0.0-beta.21"
}
根据文档,这是您应该设置的方式。我的服务器没有被击中,我可以通过http://localhost:8000/graphql 访问图形浏览器就好了。
我在这里错过了什么?
【问题讨论】:
-
你的服务器和你的移动项目在同一个项目吗?
-
没有。这是一个 Django 服务器。
-
尝试使用您的本地 IP 地址,例如 192.168.1.4 而不是 'localhost'
-
@Dyo:同样的错误。使用了我的 IP 地址
192.168.1.4。文档说也要使用localhost。 -
您是否在控制台中看到有关错误的更多信息?或者试试这个:
function FooList({ data: { error, loading, posts } }) { if (error) { return <Text>{error.message}</Text>; }
标签: react-native graphql react-apollo