Query.onCompleted isn't fired after fetching data from the cache
Intended outcome:
I tried to make a side-effect when query result is fetched using <Query onCompleted={...}>
Actual outcome:
onCompleted fired after the real network request, but when I changed variables and then switched them back, query result was returned from the cache and onCompleted handler wasn't executed.
However, it works when I change fetchPolicy to cache-and-network or network-only.
How to reproduce the issue:
Make a <Query> that should log result into console when completed. Run it with some variables,
then change it to cause re-render, then return variables back to make it re-render result from cache.
onCompleted handler won't be fired.
fetchPolicy should be cache-first
Versions npmPackages: apollo-boost: ^0.1.10 => 0.1.10 apollo-cache-inmemory: ^1.2.5 => 1.2.5 apollo-client: ^2.3.5 => 2.3.5 apollo-link: ^1.2.2 => 1.2.2 apollo-link-http: ^1.5.4 => 1.5.4 react-apollo: ^2.1.9 => 2.1.9
If it's not a bug, but a feature, can you explain how could I perform a side-effect after every query completion regardless of it uses cache or not?
I'm trying to update the state of my form with the result of a query, and I'm experiencing the same problem. onCompleted is not fired when the data is retrieved from the Apollo cache. This is an important issue as one cannot rely on the onCompleted callback if data from the cache (which plays an important role in Apollo) does not trigger it.
I'm facing the same exact issue as @olistic Can we get this fixed?
Any workaround for this?
Adding: fetchPolicy={"cache-and-network"} to the Query Component fixed it for me.
@serranoarevalo are you sure? I've tried but it seems doesn't solve the problem.
@hxuanhung yup, just tested it again and it still works. https://github.com/nomadcoders/nuber-client/blob/master/src/Routes/EditAccount/EditAccountContainer.tsx#L45
It works but it's not ultimately a fix. It's a workaround at best and at a cost of extra traffic.
I'm having similar issues, except nothing resolves mine. onCompleted never fires regardless of what I do. (and yes I tried changing the fetchPolicy to a variety of things)
I'm having the same problems. Has anyone been able to solve this?
I got the same error. Fixed it for now by using no-cache as policy.
FYI, for everyone having this issue, you can work around it by managing the query manually through withApollo instead of using the Query react tag. Here's an example.
import React from 'react';
import { withApollo } from 'react-apollo';
class Page extends React.Component {
constructor(props) {
super();
this.state = {
loading: true,
data: {},
};
this.fetchData(props);
}
async fetchData(props) {
const { client } = props;
const result = await client.query({
query: YOUR_QUERY_HERE, /* other options, e.g. variables: {} */
});
this.setState({
data: result.data,
loading: false,
});
}
render() {
const { data, loading } = this.state;
/*
... manipulate data and loading from state in here ...
*/
}
}
export default withApollo(Page);
This approach relies on state so does not work with functional components, and is of course much uglier than simply using a Query tag, but if you really need a solution this approach does the job, and does not require something drastic like skipping on caching.
I can confirm that fetchPolicy='cache-and-network' doesn't always invoke onCompleted callback with this codesandbox: https://codesandbox.io/s/6zqzy802nr
And I totally agree that even if it does work in some cases, this is still a dirty hack rather than solution.
Apollo team, onCompleted should be always fired, that's simple logic.
Confirmed - repro: https://codesandbox.io/s/50zyl4xqol We'll get this fixed.
onCompleted still isn't fired after mutation automatically updates cache
@Dartv Can you provide a small runnable reproduction that demonstrates this?
I know i can use onCompleted on mutation, but shouldn't Query onCompleted be also called since the cache was updated?
@hwillson here https://codesandbox.io/s/mm537nmqr9
Now onCompleted is being called on every render. Before it was called once.
That might break the app causing a Maximum update depth exceeded error if try to setSate with the data provided.
I get error "Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate." in browser console after updating to 2.2.4, preventing any rendering. The problem didn't occur in 2.1.11. Please fix this problem.
I've just downgraded to 2.1.11 and the component renders correctly.
I have a parent component that passes a function as a prop to a child component. That function calls setState, so that the child can update the parent's state.
I'm also getting the issue of not being called when getting it from the cache EDIT: setting notifyOnNetworkStatusChange={true} works