Hello!
I have a pretty simple use case here where I have a query which fetches all contacts for a particular account. In another area of the app, I have an import feature which allows the user to import a csv of contacts. Upon returning to the contacts page, I call refetch on the query, however, the new data is never actually fetched, only the old data is returned without the added contacts. Upon refreshing the app, the new data is properly fetched. Am I missing something?
function isLoading(props) {
const { accounts, contacts } = props;
return accounts.loading || contacts.loading;
}
module.exports = class extends React.Component {
componentDidMount() {
this.props.contacts.refetch();
}
render() {
console.log(isLoading(this.props));
const Footer = () => (
<div className="box-footer text-right">
<Button
label="Create Contact"
size="sm"
onClick={ () => showContact() }
/>
</div>
);
return !isLoading(this.props) ? (
<WidgetBox boxTitle="Contacts" Footer={ Footer }>
{ renderContacts(this.props) }
</WidgetBox>
) : null;
}
};
I looked into react-apollo as well to see if it was something in there and all it seems to be doing is pulling the refetch function off of the observableQuery and applying that to the data returned to the component.
See: https://github.com/apollostack/react-apollo/blob/master/src/graphql.tsx#L61
and: https://github.com/apollostack/react-apollo/blob/master/src/graphql.tsx#L465
@bradabelgit Thanks for opening an issue! The best way for us to verify the issue would be if you could provide a minimal reproduction.
Right now it seems more likely to me that this is happening due to an oversight in your code rather than the refetch function not working properly. Are you sure the function is actually being called?
@helfer I can do that. I did confirm that refetch was being called, but the returned result was the cached result, and not the new result from the server.
Is refetch working for you as expected?
@bradabelgit Is it possible that you have noFetch set for this query? If not, it should be relatively easy to design a failing test case for this (although I believe we already do test refetch in a similar way, and the test passes).
@helfer Nope, I actually tried that passing noFetch to see if the refetch call was actually being executed, and it threw an error as expected. So I have confirmed that refetch is being called, and I know there is new data in the database. But it is never actually sends a new query to the server (from looking at the network in chrome dev tools).
Is anyone else experiencing this?
versions:
[email protected]
Same here.
@bradabelgit and @DanielRuf are you experiencing this error in 0.8.0?
@calebmer We found a working workaround / solution for the project using forceFetch, the version was 0.5.21 and we tried an upgrade to 0.8.0 but I do not know if this solved the problem.
I am having the same issue in a similar situation using v0.8.1
Interesting, so refetch seems to try reading from the store before sending a query if forceFetch is not true. The fix would be to change the condition !forceFetch to fetchType !== FetchType.refetch || !forceFetch. However, seeing this makes me feel like this is an intentional behavior of Apollo Client? I consider this a bug as it seems to make sense that refetch should always force a fetch, but I鈥檇 like to hear from @helfer if this is intentional behavior.
In any case, this can be solved by forceFetch: true for now.
That's odd - refetch is definitely intended to always fetch from the server regardless of other options.
@stubailo good to know 馃槉
I marked this as a bug, it should be an easy fix (see my comment above) if anyone wants to submit a PR 馃憤
I also believe it's not intentional. As I mentioned for other things before, I think it's a side-effect of having lots of arguments/options which means there are a myriad of possible combinations, some of which weren't considered.
Just ran into this issue too.
Just ran into this .. using v0.8.1
Try this PR by @stevewillard which should fix it: https://github.com/apollographql/apollo-client/pull/1264
We want to release it soon 馃槉
I'll try to write a test for that PR this weekend.
Most helpful comment
That's odd -
refetchis definitely intended to always fetch from the server regardless of other options.