5

I have this code right here

import React from "react";

import { useQuery } from "@apollo/react-hooks";
import gql from "graphql-tag";

const FETCH_POSTS_QUERY = gql`
  {
    getMovies {
      id
      title
    }
  }
`;

function Home() {
  const { loading, data } = useQuery(FETCH_POSTS_QUERY);

  if (data) {
    console.log(data);
  }

  return (
    <div>
      <h1>Home</h1>
    </div>
  );
}

export default Home;

and when I'm running it I get lots of errors in my console. I'm not sure what I’m doing wrong.

Picture of wrrors

I can tell it's caused by how useQuery is used i think. I'm not sure what's wrong though.

thanks!

3 Answers 3

9

I faced the same problem today, the way of useQuery that you used is correct.

This is my original code in ApolloProvider component:

import React from 'react';
import App from './App';
import { ApolloProvider } from '@apollo/react-hooks';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { createHttpLink } from 'apollo-link-http';
import ApolloClient from 'apollo-client';

const httpLink = new createHttpLink({
    uri: "http://localhost:5000"
})

const client = new ApolloClient({
    link: httpLink,
    cache: new InMemoryCache()
})

export default(
    <ApolloProvider client={ client }>
        <App />
    </ApolloProvider>
)

I found using apollo-client dependency would cause the errors. Then I changed it and installed @apollo/client at 3.5.10 version instead to import ApolloClient. And It finally works fine.

import React from 'react';
import App from './App';

import { ApolloProvider } from '@apollo/react-hooks';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { createHttpLink } from 'apollo-link-http';
// import ApolloClient from 'apollo-client';
import { ApolloClient } from '@apollo/client';

In addition, I've also tried to use @apollo/client at the newest version. It worked successfully as well.

Sign up to request clarification or add additional context in comments.

1 Comment

I did the same, but also had to upgrade @apollo/react-hooks (I upgraded both packages to the newest)
3

I believe the issue is in the GraphQL schema, you are missing query in it. Try using the following schema:

const FETCH_POSTS_QUERY = gql`
  query {
    getMovies {
      id
      title
    }
  }
`;

Comments

1

I faced the same problem because of an old tutorial.

I configured my Apollo client in the wrong way.

Do the same as described in the get started guide from graphql and it will work perfect.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.