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

Query.onCompleted isn't fired after fetching data from the cache

Open elessarperm opened this issue 8 years ago • 77 comments

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?

elessarperm avatar Jul 12 '18 11:07 elessarperm

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.

olistic avatar Jul 17 '18 16:07 olistic

I'm facing the same exact issue as @olistic Can we get this fixed?

Dartv avatar Aug 09 '18 12:08 Dartv

Any workaround for this?

serranoarevalo avatar Aug 11 '18 12:08 serranoarevalo

Adding: fetchPolicy={"cache-and-network"} to the Query Component fixed it for me.

serranoarevalo avatar Aug 11 '18 12:08 serranoarevalo

@serranoarevalo are you sure? I've tried but it seems doesn't solve the problem.

hxuanhung avatar Aug 13 '18 09:08 hxuanhung

@hxuanhung yup, just tested it again and it still works. https://github.com/nomadcoders/nuber-client/blob/master/src/Routes/EditAccount/EditAccountContainer.tsx#L45

serranoarevalo avatar Aug 13 '18 11:08 serranoarevalo

It works but it's not ultimately a fix. It's a workaround at best and at a cost of extra traffic.

Dartv avatar Aug 13 '18 11:08 Dartv

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)

laspluviosillas avatar Aug 21 '18 17:08 laspluviosillas

I'm having the same problems. Has anyone been able to solve this?

Cabezaa avatar Aug 23 '18 13:08 Cabezaa

I got the same error. Fixed it for now by using no-cache as policy.

franzejr avatar Aug 24 '18 12:08 franzejr

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.

laspluviosillas avatar Aug 24 '18 14:08 laspluviosillas

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.

yantakus avatar Sep 03 '18 06:09 yantakus

Confirmed - repro: https://codesandbox.io/s/50zyl4xqol We'll get this fixed.

hwillson avatar Sep 20 '18 22:09 hwillson

onCompleted still isn't fired after mutation automatically updates cache

Dartv avatar Sep 28 '18 17:09 Dartv

@Dartv Can you provide a small runnable reproduction that demonstrates this?

hwillson avatar Sep 28 '18 18:09 hwillson

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

Dartv avatar Sep 28 '18 18:09 Dartv

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.

vititu avatar Sep 28 '18 23:09 vititu

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.

tab00 avatar Oct 04 '18 10:10 tab00

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.

tab00 avatar Oct 04 '18 10:10 tab00

I'm also getting the issue of not being called when getting it from the cache EDIT: setting notifyOnNetworkStatusChange={true} works

riccoski avatar Oct 10 '18 17:10 riccoski