3

I have a simple structure of files on a same directory (./src/)

App.js

import React from "react";
import {
  ApolloClient,
  ApolloProvider,
  createHttpLink,
  InMemoryCache,
} from "@apollo/client";
import Gql from "./Gql";

const App = () => {
  const client = new ApolloClient({
    link: createHttpLink({
      uri: "https://api.spacex.land/graphql/",
    }),
    cache: new InMemoryCache(),
  });

  return (
    <ApolloProvider client={client}>
      <Gql />
    </ApolloProvider>
  );
};

export default App;

Gql.js

import React from "react";
import { gql, useLazyQuery } from "@apollo/client";

const Gql = () => {
  const [getQuery, { loading, data, error }] = useLazyQuery(gql`
    query {
      launchesPast(limit: 10) {
        mission_name
        launch_date_local
        launch_site {
          site_name_long
        }
      }
    }
  `);

  if (error) {
    return <div> Error . . .</div>;
  }

  return (
    <React.Fragment>
      <button
        style={{ padding: "20px" }}
        onClick={() => {
          getQuery();
        }}>
        Request Query
      </button>
      <div>Loading: {loading ? "true" : "false"}</div>
      <div>data: {JSON.stringify(data)}</div>
    </React.Fragment>
  );
};

export default Gql;

enter image description here

The loading variable changed to true and to false on first request. But if I try to query it again and again without reloading the page. The loading variable stay the same while the browser have a valid XHR Request to the graphql server.

5
  • after the first request, from the second request onwards it seems the data on the page is not being updated either, is that correct? by that I mean <div>data: {JSON.stringify(data)}</div> still shows data from the first request ? Commented Jan 15, 2022 at 14:33
  • Yes it will stay the same. I didn't change the parameter of graphql request, so it will return the same data. As you can see on the right side of the .gif, I called the request 4 times. You can try to see request yourself in this link: api.spacex.land/graphql Commented Jan 15, 2022 at 14:38
  • 1
    hm, try changing the parameter & see if it updates. This might be by design, so the useLazyQuery hook has no reason to update data since params did not change. Commented Jan 15, 2022 at 14:41
  • glad I could help, should I add this as an answer so it is helpful to other folks who might run into this same issue? Commented Jan 16, 2022 at 14:43
  • Sure, go ahead. Commented Jan 16, 2022 at 14:45

1 Answer 1

2

This is by design, the useLazyQuery hook will only run when the params change as this will then prevent useless re-renders.

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

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.