react-apollo icon indicating copy to clipboard operation
react-apollo copied to clipboard

useLazyQuery queries repeatedly

Open ajones55555 opened this issue 7 years ago • 5 comments

Intended outcome: After a successful call of the function generated from useLazyQuery, the data object should update.

Actual outcome: Query is called repeatedly (spamming)

Details I have something like

const [getUser, {data: CurrentUserDataInDb}] = useLazyQuery(getUserQuery, {client})

useEffect(() => {
    console.log("test")
    getUser()
})

I believe this is an issue with Apollo rather than my code, because the console.log is only called once, but the query is spammed (viewed in network tab)

The client is also defined in this component (the reason I'm passing it in manually). Could that be the issue?

ajones55555 avatar Oct 15 '19 05:10 ajones55555

The way you have it, useEffect will run after every render. Are you sure console.log is only getting called once? Some browsers will lump together console logs if they have the same message.

To run it only on the first render, you need to pass an empty array as the second argument to useEffect.

dylanwulf avatar Oct 15 '19 15:10 dylanwulf

It probably called only once in Network tab of browser dev tools. However useEffect will cause it to be called multiple times. The reason it shows called only once in browser dev tools is because of Apollo caching, so after 1st time ,it will take the data from cache, however re-render will still happen. You can test this theory by supplying fetchPolicy as network-only.

You have to provide 2nd parameter to useEffect to have fetching function to be called only once. aka add [] as second parameter.

chattling avatar Oct 15 '19 18:10 chattling

@chattling @dylanwulf In my local, I actually did pass in a [token] which is not affected by the data from this query. The console.log is definitely only called once -> i'm using chrome and there is no number next to it lumping anything together. And the network tab is showing the repeated call, seems to be skipping the apollo cache entirely which is weird because the query is successful. Thanks for the attempts. Any other ideas?

ajones55555 avatar Oct 15 '19 20:10 ajones55555

No other ideas, sorry. Could you create a runnable reproduction of the issue? The Apollo team will usually respond quicker if you have a reproduction that clearly shows the issue. (It might be a little while before they respond though. They're currently hard at work trying to get apollo-client v3 ready for the graphql summit)

dylanwulf avatar Oct 15 '19 20:10 dylanwulf

I solved this by moving my client creation functionality to a parent component of the component which needed this useLazyQuery. Even with useMemo to memoize the client, it appears that it was still being recreated, which updates the useLazyQuery function, which rerendered the component, which recreated the client, creating an endless loop. Hope this helps someone in the future!

ajones55555 avatar Oct 16 '19 04:10 ajones55555