reactjsgraphqlapollo-client

Apollo gql, get loading state when polling


I am using @apollo/client v3. I want to get loading state when requests will happen from polling but it is not working as I expect.

This is my query

  const { data, loading } = useQuery(SOME_QUERY, {
    variables: {
      variable: "value",
    },
    10000  //poll interval
  });

I wrote useEffect to catch if it is loading or not when polling

  useEffect(() => {
    console.log(loading)
  }, [loading]);

This is runs only for first request but in browsers network tab I see that requests is happening every 10seconds (10000 milliseconds, because of polling). How can I catch state when loading is happening or polling is started for all requests?


Solution

  • Setting notifyOnNetworkStatusChange to true will solve it.

    const { data, loading } = useQuery(SOME_QUERY, {
      notifyOnNetworkStatusChange: true,
      pollInterval: 10000,
      variables: {
        variable: "value",
      },
    });
    
    useEffect(() => {
      console.log(loading)
    }, [loading]);