-
Notifications
You must be signed in to change notification settings - Fork 786
Query.onCompleted isn't fired after fetching data from the cache #2177
Comments
I'm trying to update the state of my form with the result of a query, and I'm experiencing the same problem. |
I'm facing the same exact issue as @olistic |
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 And I totally agree that even if it does work in some cases, this is still a dirty hack rather than solution. Apollo team, |
Confirmed - repro: https://codesandbox.io/s/50zyl4xqol |
|
@Dartv Can you provide a small runnable reproduction that demonstrates this? |
I know i can use |
Now That might break the app causing a |
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 |
I'm also getting the issue of not being called when getting it from the cache |
If anyone here is having the same issue for mutations -- I have two identical mutations that call the same I've just chained the function to the actual mutation for the second case.
|
@tab00 I fixed it by using a Hope this works for you! |
@the same thing for me. Now it's impossible to use |
Is there any other option I can use instead of onCompleted, some funciton I can use after the query result is retrieved whether is done on network or from cache? |
Included here too #3535 is the related issue that |
@tresdev we solved it by using the useEffect react hook to react to a completed query: const { data, loading, error } = useQuery(queryName, {
pollInterval: 15000,
})
useEffect(() => {
if (loading || error) {
return
}
// Do your thing with the data here
}, [data, error, loading]) |
@Algram this does not work for me since none of the variables are changing on the second query (data, loading, error, networkStatus). Is there another workaround? |
Please, any chance we can get this fixed? |
It still not working, why this issue gets closed? |
This is still happening, can we get this reopened? |
@hwillson I acknowledge this issue. |
Hi @dqunbp
I'd suggest having one callback for all "completed" events, but pass some argument with the info on how exactly the data has been fetched: from cache or via network. |
I'm facing the same issue. |
use notifyOnNetworkChange when u are using default fetch-policy (when u havent specified anythig). @Darklaki |
This issue is a dealbreaker and kind of makes the default Please reopen this issue as it is a significant bug forcing UI to make graphql calls everytime, even though UI could do fine with results from few seconds back for the same set of input. |
@palerdot I have beein using notifyOnNetworkChange on Query render prop, it does call the onComleted when that data is fetched from network or cache, u dont have to change you'r network policy. |
@PavanBahuguni It does not seem to work for me ... plus I'm using |
I can confirm, this is not working for me too in @apollo/[email protected]
|
Same as @mvirbicianskas If I try to fetch the same data consecutively, it doesn't work |
fetchPolicy: 'network' option fixes it successfully. onCompleted fires every time |
It doesn't fix it: it circumvents the issue by not using the cache. This is about |
It has been 2 years this issues hasn't been fixed and the issue has been closed. |
Reopened here #3968 - hopefully it will get some visibility. Pretty much a show stopper for me. |
I can't believe this is still not fixed. |
Any update regarding this issue? |
fetch policy I agree that onCompleted event must be called when data is retrieved from cache |
almost 2 years and no explanation of the why it is like that or a fix |
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 andonCompleted
handler wasn't executed.However, it works when I change
fetchPolicy
tocache-and-network
ornetwork-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?
The text was updated successfully, but these errors were encountered: