Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useLazyQuery loading and networkStatus when call refetch. fetchMore send two requests #7243

Open
Tracked by #8596
Krasnopir opened this issue Oct 26, 2020 · 5 comments

Comments

@Krasnopir
Copy link

Hello. I try to use hook useLazyQuery to filter data with new variables.
apollo client config for watch query is fetchPolicy: 'cache-and-network',

  const [getTracks, { data, loading, error, networkStatus, called, refetch }] = useLazyQuery(TracksPageQuery);

  useEffect(() => {
    if (!called) {
      getTracks({ variables: getQueryVariables() });
    } else {
      refetch(getQueryVariables());
    }
  }, [JSON.stringify(urlQueries)]);

When i set new variables at first time - refetch does not trigger loading as true and networkStatus always 7. But refetch with repeated variables return loading true, as i need.

I tried different fetchPolicy setting, and only with 'cache-and-network' and repeated variables returned loading true on refetch.
Also it does not suit me to set notifyOnNetworkStatusChange, because i need to show previous result before data will update, and spinner for loading over.

@Krasnopir
Copy link
Author

fetchMore also does not return loading true.
Only with apollo client settings fetchPolicy: 'cache-and-network', then loading is true after every call.
But in this case i have another problem (maybe it must be another issue) - fetchMore send two requests, first with new variables,
and then with start query variables.

useEffect(() => {
   if (!called) {
     getTracks({ variables: getQueryVariables() });
   } else {
     fetchMore({
       variables: getQueryVariables(),
       updateQuery: (prev, { fetchMoreResult }) => {
         if (!fetchMoreResult) return prev;
         return fetchMoreResult;
       },
     });
   }
 }, [JSON.stringify(urlQueries)]);

@Krasnopir Krasnopir changed the title useLazyQuery loading and networkStatus when call refetch useLazyQuery loading and networkStatus when call refetch. fetchMore send two requests Oct 26, 2020
@LaurenceSM10
Copy link

Same issue. loading is always false using useLazyQuery

@alamothe
Copy link

alamothe commented Jan 1, 2021

Same issues:

  • For "cache-and-network", when fetchMore is called, it will make two different queries to the backend.
  • For the default "cache-first" policy, when fetchMore is called, it won't update loading (but it doesn't suffer from the first issue).

@Giayychan
Copy link

Same issue.
`
const [getJobAd, getJobAdRes] = useLazyQuery(GET_JOB_AD);

useEffect(() => {
if (router.query.id !== 'new' && router.query.id) getDetail(router.query.id as string);
}, [router]);

const getDetail = async (jobAdId: string) => {
try {
getJobAd({ variables: { id: jobAdId } });
console.log(getJobAdRes, getJobAdRes.loading);
} catch (error) {
console.log(error);
}
};`

getJobAdRes ={
"loading": false,
"networkStatus": 7,
"called": false
}

@anjopater
Copy link

I have the same issue,

    "@apollo/client": "^3.5.10",
    "@apollo/react-hooks": "^4.0.0",

The loading status change from false to true in the first call, but after that doing refech is always false, and checking the networkStatus it's always 7 so I'm not able to get the loading correct status.

@hwillson hwillson added the 🔍 investigate Investigate further label May 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants