question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Not memoize onCompleted and onError never resolves the query

See original GitHub issue

Intended outcome: The following query should log either Completed or Error to the console.

useQuery(QUERY, {
  fetchPolicy: 'network-only',
  onCompleted: () => console.log('Completed'),
  onError: () => console.log('Error'),
})

Actual outcome: The query is never resolved and the component re-renders infinitely.

Is it now recommended to make sure to memoize the query/mutation callbacks (onCompleted, onError, update, etc) when used with fetchPolicy: 'network-only'? The version, @apollo/client@3.0.0-beta.45 resolved the callbacks fine without having to memoize them.

How to reproduce the issue: I’ve created a reproduction on the Code Sandbox: https://codesandbox.io/s/sleepy-mendeleev-1q9jf?file=/src/App.js

Open up the console and you will see loading being logged in the console infinitely. If you however comment the non-memoized onCompleted and onError and uncomment the memoized version and refresh the browser (as below), it will successfully resolve the query and show the data.

const { loading, data } = useQuery(ALL_PEOPLE, {
    fetchPolicy: "network-only",
    // onCompleted: () => console.log("Hello"),
    // onError: () => console.log("error")
    onCompleted: useCallback(() => console.log("Hello"), []),
    onError: useCallback(() => console.log("error"), [])
  });

Versions Reproducible from @apollo/client: 3.0.0-beta.46

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:22
  • Comments:35 (9 by maintainers)

github_iconTop GitHub Comments

7reactions
benjamncommented, Jul 23, 2020

Heads up: we’re not quite ready to publish @apollo/client@3.1.0 yet, but I’ve published an @apollo/client@3.1.0-pre.0 release that includes #6588, if you want to try that in the meantime. 🙏

3reactions
TerrySlackcommented, May 19, 2021

@pcunha29 I put the project on hold, but I will look into it tomorrow and see if I can get it finished for end of next week. Sorry for the delay. I feel your pain and have completely moved away from Apollo.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pitfalls I fell into during Apollo Client 3.0 migration
Basically, you need to avoid adding a new object by using useCallback or useMemo . Not memoize onCompleted and onError never resolves the...
Read more >
open-source-community/apollographql-react-apollo
Prevent inline onError and onCompleted callbacks from being part of the internal memoization that's used to decide when certain after render units of ......
Read more >
react native - update state after useQuery onCompleted... Error
React is warning you that you're trying to update a stateful variable that's no longer there. What's probably happening is that your ...
Read more >
https://raw.githubusercontent.com/apollostack/reac...
If you are using `apollo-client` 2.x and are not ready to update to ... `setState` in the `onError` / `onCompleted` callbacks of the...
Read more >
Reactive Programming with RxJS
After onCompleted is called, further calls to onNext will have no effect. onError Called when an error occurs in the Observable. After it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found