我正在尝试使用以下代码设置 react-apollo:

import React from 'react';
import {render} from 'react-dom';

import gql from 'graphql-tag';
import { ApolloProvider, graphql } from 'react-apollo';
import { ApolloClient } from 'apollo-client';
import { HttpLink } from 'apollo-link-http';
import { InMemoryCache } from 'apollo-cache-inmemory';


const client = new ApolloClient({
  link: new HttpLink({ uri: 'http://localhost:5000/graphql' }),
  cache: new InMemoryCache()
});



class App extends React.Component {
  render () {
    return <p> Hello React project</p>;  }
}
const MY_QUERY = gql`query Hello { hello }`;

const AppWithData = graphql(MY_QUERY)(App)
render(
  <ApolloProvider client={client}>
    <AppWithData />
  </ApolloProvider>,
  document.getElementById('app')
);

但是,我收到此错误:
javascript -  react 阿波罗网络错误 : Server response was missing for query  &#39;Hello&#39;-LMLPHP

查看 chrome 开发工具中的网络选项卡,我可以看到请求正在通过:
javascript -  react 阿波罗网络错误 : Server response was missing for query  &#39;Hello&#39;-LMLPHP

我想知道我是否缺少一些配置设置来以正确的格式获取响应?或者我可能需要以不同的格式从我的 graphql 端点返回数据?感谢任何建议,我只是在学习 graphql 和 apollo,所以它可能是非常明显的。谢谢!

最佳答案

问题是我需要将响应包装在“数据”键中,所以我需要的响应是:

{
    "data": {
         "hello": "hello"
    }
}

正如我所说,我是 graphql/apollo 的新手

10-07 19:02
查看更多