useQuery函数始终返回加载结果。为什么会这样呢?

时间:2020-06-18 23:39:08

标签: react-apollo apollo-client apollo-server

因此,我正在构建一个简单的Apollo应用程序,但似乎useQuery函数未按预期获取数据。这是代码:

import React from 'react';
import ReactDOM from 'react-dom';
import { useQuery, gql, ApolloClient, ApolloProvider, InMemoryCache } from '@apollo/client';

require('dotenv').config();

// our GraphQL query, stored as a variable
const GET_USERS = gql`
query{
    users{
      _id
      username
      email
    }
  }   
`;

function Home() {
    const { loading, data, error, fetchMore } = useQuery(GET_USERS)
    if (loading) return <p>Loading...</p>
    if (error) return <p>Error</p>
    return (
        <div>
            <h1>The data: {data}!</h1>
        </div>
    );
};

// configure our API URI & cache
const uri = process.env.API_URI;
const cache = new InMemoryCache();

// configure Apollo Client
const client = new ApolloClient({
    uri,
    cache,
    connectToDevTools: true
});

function App (){
    return (
    <ApolloProvider client={client}>
        <Home />
    </ApolloProvider>
)}

ReactDOM.render(<App />, document.getElementById('root'));

That returns this.

每当我运行代码时,它总是返回“加载”页面。为什么会发生?

0 个答案:

没有答案