我试图获得一个基本的 React + Graphcool 项目设置。
我已经初始化了 Graphcool 后端,所以我可以在以下位置看到操场:https ://api.graph.cool/simple/v1/MY-KEY
我可以在操场上运行这个查询并查看结果:
query {
allGroups {
id
description
}
}
但是我无法将它连接到 React 前端。这是我的 index.js:
import React from 'react';
import ReactDOM from 'react-dom';
// GraphQL
import { ApolloProvider } from 'react-apollo';
import { ApolloClient } from 'apollo-client';
import { HttpLink } from 'apollo-link-http';
import { InMemoryCache } from 'apollo-cache-inmemory';
// Components
import App from './components/App/App';
const httpLink = new HttpLink({
uri: 'https://api.graph.cool/simple/v1/MY-KEY',
});
const client = new ApolloClient({
link: httpLink,
cache: new InMemoryCache(),
});
ReactDOM.render(
<ApolloProvider client={client}>
<App />
</ApolloProvider>,
document.getElementById('App'),
);
在 App.js 中:
import React from 'react';
import gql from 'graphql-tag';
import graphql from 'react-apollo';
const myQuery = gql`
query {
allGroups {
id
description
}
}
`;
const App = () => {
return (
<div>
<h1>Application</h1>
<h2>Groups:</h2>
</div>
);
};
// export default App;
export default graphql(myQuery)(App);
但我收到一个错误:
Uncaught TypeError: (0 , _reactApollo2.default) is not a function
我不知道这是否相关,但我的 IDE 在 App.js 的“allGroups”行上给了我以下错误:
cannot query field "allGroups" on type "Query"