【问题标题】:create react app with apollo client doesn't get any response使用阿波罗客户端创建反应应用程序没有得到任何响应
【发布时间】:2019-02-08 23:59:16
【问题描述】:

我将create-react-appapollo client 一起使用,并遵循apollo docs 上的编码。 我的服务器配置为apollo server with express,我按照GraphQL Clients 的方式对其进行了测试。它可以工作,但是当我尝试使用apollo client way 时它不起作用。这是我的代码。

在 index.js 文件中

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
import ApolloClient from 'apollo-boost';
import {ApolloProvider} from 'react-apollo';

const client = new ApolloClient({
    uri: `/graphql`
});

ReactDOM.render(<ApolloProvider client={client}><App /></ApolloProvider>, document.getElementById('root'));
  • 我已经尝试更改 uri,例如 http://localhost:4000/graphqlhttp://localhost:4000

在 App.js 文件中

import gql from 'graphql-tag';
import {Query} from 'react-apollo';

const callQuery = gql`
{ 
  hello 
}
`;

class App extends Component {
  constructor(props) {
    super(props)
  }
  componentDidMount() {
    const test = this.HelloApi();
    console.log(test);
  }

  HelloApi() {
    <Query query={callQuery}>
      {({loading, error, data}) => {
        if(error) return `Error!: ${error}`;

        return data;
      }}
    </Query>
  }

render() {
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h1 className="App-title">Welcome to React</h1>
        </header>
        <p className="App-intro">
          To get started, edit <code>src/App.js</code> and save to reload.
        </p>
      </div>
    );
  }
}

export default App;

我无法通过控制台和网络获得任何响应。请让我知道我错过了什么。谢谢。

【问题讨论】:

    标签: reactjs graphql react-apollo apollo-client


    【解决方案1】:

    如果您想在生命周期方法中测试查询,我会使用高阶组件“withApollo”

    #withApollo

    import gql from 'graphql-tag';
    import {Query, withApollo} from 'react-apollo';
    
    const callQuery = gql`
    { 
      hello 
    }
    `;
    
    class App extends Component {
      constructor(props) {
        super(props)
    }
      componentDidMount() {
        const test = this.props.client.query({ query: callQuery })
        console.log(test);
    }
    
    render() {
        return (
          <div className="App">
            <header className="App-header">
              <img src={logo} className="App-logo" alt="logo" />
              <h1 className="App-title">Welcome to React</h1>
            </header>
            <p className="App-intro">
              To get started, edit <code>src/App.js</code> and save to reload.
            </p>
          </div>
        );
      }
    }
    
    export default withApollo(App);
    

    通过这样做,您将可以在生命周期方法“componentDidMount()”中访问客户端

    你可以这样做:

    componentDidMount() {
        const test = this.props.client.query({ query: callQuery })
        console.log(test);
    }
    

    【讨论】:

    • 感谢您的提示。我想通过您的提示解决我的问题。这不是完全一样的方法,但它有助于看到另一种方法。
    • 没问题。您使用的查询组件,通常在 render 方法中使用。您可以在查询组件中控制日志。 &lt;Query query={callQuery}&gt; {({loading, error, data}) =&gt; { if(error) return Error!: ${error}; console.log(data) return data; }} &lt;/Query&gt;
    猜你喜欢
    • 2021-04-03
    • 2018-04-13
    • 2021-07-13
    • 2021-06-28
    • 2020-03-18
    • 2020-10-30
    • 2019-07-06
    • 2021-12-09
    • 2021-02-02
    相关资源
    最近更新 更多