-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Query keys with dynamic transports #418
Comments
Thanks for filing the issue, Victor. The best workaround is probably to call |
Another way to handle this would be to refetch the queries when accountId changes, something like: const transport = createConnectTransport({
baseUrl: `/api/accounts/${accountId}/services`
});
const queryClient = useQueryClient();
const previousAccountId = usePrevious(accountId);
useEffect(() => {
if (previousAccountId !== undefined && previousAccountId !== accountId) {
queryClient.refetchQueries();
}
}, [accountId, previousAccountId]); The above will refetch ALL queries, regardless of their transport but it can be tailored to target specific queries based on their query key. edit Perhaps a better solution to this would be to memoize the transport and have an effect triggered on the transport and triggering an effect based on that changing. That way it can also take into account all other options that could be provided to |
With #437, we can make the Transport part of the query key. It will be important to create the transport in a module scope, or to memoize it. For example: const transport = useMemo(() => createConnectTransport({
baseUrl: `/api/accounts/${accountId}/services`,
}), [accountId]);
useQuery(say, {sentence: "hi"}, {transport}); Could be a footgun, but having to keeping track of reference is common in react. |
Thank you for your suggestions! @timostamm in order to address this issue using the new |
@Victor-Mestre-Sybo, no, you do not need to modify the query key. You just have to keep track of your references. For example: // broken, do not use
const transport = createConnectTransport({
baseUrl: `/api/accounts/${accountId}/services`,
});
useQuery(say, {sentence: "hi"}, {transport}); Every time this code runs, a new transport is created, and the key will change. The react hook const transport = useMemo(() => createConnectTransport({
baseUrl: `/api/accounts/${accountId}/services`,
}), [accountId]);
useQuery(say, {sentence: "hi"}, {transport}); If |
Sorry, I don't really understand it, I guess I'm missing something. How does As far as I understand, by doing Therefore, the cache will be the same for a given key even after changing the transport right? |
@Victor-Mestre-Sybo, this PR wires it up: #441 |
@timostamm now it makes sense! it's looking really promising, thank you so much for addressing this issue! |
@paul-sachs raised a good point: For SSR, you want to populate the query cache on the server, then use the that cache in the web browser (you prefetch queries, dehydrate the query client, and hydrate it again). For that to work, query keys must be the same on the server and in the web browser, which isn't guaranteed with createTransportKey (it counts references). We're adding an escape hatch for SSR with #444. We'll get a pre-release version 2 out as soon as possible, which solves this issue. Closing it. |
Thanks again! Regarding the issue you mention with SSR, it would have been helpful that the |
We considered it. The transports from |
Hello, first of all thanks for this amazing project!
We have this setup where our transport contains dynamic parameters, for example:
So we have this issue where, when switching
accountId
the same cache will be used for a given query since the query key does not contain any information about the transport. We have considered either making our own abstraction ofuseQuery
or calling something likequeryClient.removeQueries()
when the transport's baseUrl changes, is there any better alternative (like including this information in the query key)?The text was updated successfully, but these errors were encountered: