Closed epiqueras closed 3 years ago
Hi @epiqueras, fetch policies have nothing to do with rendering logic.
The method you referenced follows the logic of react a getDerivedStateFromProps https://github.com/facebook/relay/blob/master/packages/react-relay/ReactRelayQueryRenderer.js#L136
The use of the fetchKey is not a hack but its precise purpose https://github.com/relay-tools/relay-hooks#usequery
Hi @morrys, let me add some context.
When you prefetch.next
the same query with the same variables using network-only
, this is making it so that nothing happens.
Yes, i know but when you render usePreloadedQuery , the function getValue recovers the last value resolved, is it right?
Yeah, but there is no new value because it's never fetched.
Yeah, but there is no new value because it's never fetched.
what do you mean? I don't understand
prefetch.next queryA variablesA network-only
-> fetches and rerendersprefetch.next queryA variablesA network-only
-> nothing happens, no fetch, no rerenderBecause you are already displaying and rendering the previously fetched data as if you were using useQuery and call a React setState
prefetch.next
is imperative; it's not a hook. If you call it again, you expect the server's data to have changed, or you are refreshing the page without an actual window unload.
@epiqueras, fetch-query is imperative https://relay.dev/docs/en/fetch-query and not the loadQuery.
And anyway, as you said (with fetchKey), there is an easy way to make the loadQuery behave as you wish.
"network-only": will not reuse locally cached data, and will always send a network request to fetch the query, ignoring any data that might be locally cached in Relay.
Doesn't that contradict this?
Fetch policies are used when fetching is performed. When you do the next twice with the same parameters the behavior of the loadQuery is the same as when the useQuery is re-rendered (if the parameters do not change, the fetch is not re-executed)
From this issue it seems to be necessary to add a paragraph Behavior like this https://relay.dev/docs/en/api-reference#behavior-2 in the documentation
That makes sense. The documentation should be more precise.
What would you recommend as the canonical way of re-fetching the same preloaded query then?
As suggested in the comment above:
And anyway, as you said (with fetchKey), there is an easy way to make the loadQuery behave as you wish.
It is sufficient to use the fetchKey as a counter and increment it before executing a new next.
Ok, I was just wondering if there was another way because it felt hacky.
I think I am having the same issue or something similar. Even changing the fetchKey
I am not able to force a re-fetch. It only re-fetches when the cache expires.
https://github.com/relay-tools/relay-hooks/blob/94de2de505a663a59052a6eb82bb95a56c20086e/src/QueryFetcher.ts#L168-L193
I think it's because you only fetch again when the fetch policy changes. This doesn't let you fetch the same query again without hacking around with the fetch key.