-
Notifications
You must be signed in to change notification settings - Fork 786
FetchPolicy 'network-only' returns cached value #556
Comments
Thanks @davidye, this sounds like a bug. Could you provide a reproduction with https://github.com/apollographql/react-apollo-error-template? That would help us figure out what's going on. |
I am also seeing the same issue, fetchPolicy: 'network-only', Component receives the previously cached value. versions |
Thanks @irfanlone that would be great! Let me know if you need any help with it 🙂 |
@helfer, Here is the PR reproducing the issue, apollographql/react-apollo-error-template#2. Let me know if it makes sense. Thanks for looking into it. 🙂 |
Thanks @irfanlone. Based on your reproduction it looks like this runs two separate queries. What I'm not sure yet is how the code path is different from the one taken in this test here: https://github.com/apollographql/apollo-client/blob/master/test/client.ts#L1282 |
@irfanlone this is embarrassing, but I figured out what's wrong: The error was actually in our docs, which did not specify the right way to pass the option. In react-apollo you have to pass those options that apply to Apollo Client wrapped inside the
I've fixed it in the docs with this commit: https://github.com/apollographql/react-docs/commit/dd23b58303b6c8415d307b1c48e5ffbec2d45cfc @davidye I think you most likely had the same problem, so I'll close the issue here. If this doesn't fix your issue, please re-open it. |
@helfer, I am sorry I actually didn't give you the right reproduction, I was already including |
We're facing similar problem. Second render of component is rendering from cache.
|
@helfer I'll try to create repo with reproduction of this bug. Could you meanwhile reopen this issue? |
I just made some changes to apollo error template to reproduce this bug. I'm expecting, that last member of list will change on 'refresh' button click, but it's not. Loading data from cache (even |
@chompomonim this may be something that has to be changed in react-apollo. It's possible that react-apollo actually looked at the old forceFetch option, but doesn't look at the fetchPolicy. |
Same behavior here, |
@Aetherall do you know how to fix this? |
I have the same issue: apollo is doing queries only when graphql() created component! |
Please check if options is passed as functions. I was facing issue with When i changed it to following it works fine. |
@davidsims9t My workaround was to use "cache-and-network", I would try @jinxac 's solution first |
This issue has been automatically labled because it has not had recent activity. If you have not received a response from anyone, please mention the repository maintainer (most likely @jbaxleyiii). It will be closed if no further activity occurs. Thank you for your contributions to React Apollo! |
I was facing the same issue, here, but realised that I had a |
fetchPolicy="network-only" still broken in the query component. (@2.5.1) are there any work arounds? |
@wrod7 You could try doing something like this under
|
Disregard! I was on an old version of
🎉 |
can you share you query component? are you using 'network-only' ? @pcorey the default options on the apollo client didnt work for me |
@wrod7 The query is in the reproduction repo. Check it out.. It seems to work as I'd expect with |
Hi guys, I hope you can help me with this, I'm not sure about this behavior and it might be related to this bug. I try Then I add ShowHide.js export default class extends React.Component {
render() {
console.log("Re-rendering query:");
return (
<div>
<BrowserRouter>
<Route to="/" render={props => <Content {...props} />} />
<br />
<Link to="/test">Test</Link> <br />
<Link to="/dashboard">Dashboard</Link> <br />
<Link to="/home">Home</Link> <br />
<Link to="/list">List</Link> <br />
</BrowserRouter>
</div>
)
}
} Content.js class Content extends Component {
render() {
const { data } = this.props;
console.log(
"data received from Query:",
_.get(data, "random.value"),
data
);
return (
<div>
<h1>Open your console!</h1>
<p>
The Query component eventually settled on the above value, which comes
from the network. Click "Hide" to remove the Query component from the
virtual DOM.
</p>
<p>{_.get(data, "random.value")}</p>
{/* <p>Math.random: {Math.random()}</p> */}
<Switch>
<Route path="/test" render={() => <div>test</div>} />
<Route path="/dashboard" render={() => <div>dashboard</div>} />
<Route path="/home" render={() => <div>home</div>} />
<Route path="/list" render={() => <div>list</div>} />
</Switch>
</div>
)
}
}
export default graphql(gql`
{
random {
id
value
}
}`, {
options: {
fetchPolicy: 'network-only'
}
})(Content); And as you can see here, the component re-renders, but the query doesn't fire again |
I've managed to overcome this issue by passing a timestamp to await apolloClient.query({
query: getStatesCitiesQuery,
variables: {
antiCache: +(new Date()), // timestamp HACK
state,
city,
},
});
I thought that maybe my graphql server returns some Caching headers but it does not.
honesly I am not sure what the source of this issue is. Update |
@mieszko4 where are you calling refetch? can you show an example? |
Sure. class ListContainer extends Component {
cachedClasses = []
hasReachedEnd = false
loadingMore = false
state = {
from: moment(),
}
onLoadMore = () => {
const { from } = this.state;
if (!this.hasReachedEnd) {
this.loadingMore = true;
this.setState({
from: from.clone()
.add(7, 'days')
.add(1, 'day'), // another date on subsequent request
});
}
}
render() {
const { from } = this.state;
return (
<Query
query={ListOfClassesQuery}
fetchPolicy="network-only"
notifyOnNetworkStatusChange
variables={{
from: from.format(dateApiFormat), //DATE ONLY
until: from.clone().add(7, 'days').format(dateApiFormat), // DATE ONLY
}}
>
{({
loading, data, error, refetch,
}) => {
if (error) {
return <ErrorMessage>{error}</ErrorMessage>;
}
if (!loading) {
this.loadingMore = false;
if (data.allClasses.edges.length === 0) {
this.hasReachedEnd = true;
} else {
this.cachedClasses = [...this.cachedClasses, ...data.allClasses.edges];
}
}
return (
<List
loading={loading}
loadingMore={this.loadingMore}
refetch={() => { // refreshes the list - called when pulling down or coming to this screen
this.cachedClasses = [];
this.hasReachedEnd = false;
const now = moment();
// THIS FOLLOWING WAS CRITICAL TO ME ---->
const willVariableChange = from.format(dateApiFormat) !== now.format(dateApiFormat);
if (willVariableChange) {
// apollo will refresh data on setState only if variables change
this.setState({
from: now,
});
} else {
// otherwise wee need to force the refresh
// I do not wanted to use setState and refetch at the same time
// because it would then run query twice on some occasions
refetch();
}
}}
allClasses={this.cachedClasses}
onLoadMore={this.onLoadMore}
/>
);
}}
</Query>
);
}
} |
Have the same issue using
|
I was having exactly the same issue. Using |
Neither no-cacho or network-only works for me |
Just worked to me |
This issue has definitely gotten a bit unwieldy. Some people are reporting this issue as fixed, others as not fixed, others are reporting new issues, etc. I'm going to close this issue, but please consider opening a new issue if you're still encountering any problems. Thanks! |
It's still broken! |
Still broken, and graphql's aggressive caching in general is the source of tons of bugs. Unnecessary premature optimization when you're just trying to get a simple cyclic state refresher working. |
All my errors have gone when I put errorPolicy: "all" |
If you already have the data in cache, it yields false->true->false. How did you solve this issue? |
@mieszko4 |
with new version of apollo, using hooks, it works fine for me! |
Tried it with hooks and I'm still getting the cached data when the fetchPolicy is set to "network-only" I realize "no-cache" isn't efficient (new fetch per request) but turning the cache off definitely turned it off:
|
With I make a useQuery request in one component, and then another makes a useQuery request, whichever resolves last then becomes the value of the other component. They are different queries! my client is configured for "network-only", adding "no-cache" option to useQuery fixed the issue. |
Hey, were you able to get this issue resolved? |
I am facing this issue in Facing issue in Has this been fixed for Query Component? Setting a pollInterval does send a new network request. But changing variables doesn't. |
How we end up fixing this is by passing refetch back and after state has updated we call the Not the best solution but thats how we end up solving it without |
This works for me |
Steps to Reproduce
Not sure if this is an issue with react-apollo or apollo-client.
Buggy Behavior
When using fetchPolicy: 'network-only', Component receives the previously cached value.
Expected Behavior
When using fetchPolicy: 'network-only' should only return fresh results from server.
Version
The text was updated successfully, but these errors were encountered: