-
-
Notifications
You must be signed in to change notification settings - Fork 559
fix(openapi-react-query): add client parameter to queryKey #1981
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
Conversation
🦋 Changeset detectedLatest commit: f056e57 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
0959a10
to
5857d4b
Compare
Thanks for the contribution! It is a really odd behavior that keys conflicts even tho you are using different clients as each one should have its own |
I believe my test illustrates the issue well. I ran the added test on the revision before this PR, and the test failed. The test uses
From my understanding, the TanStack Query Client manages the To be treated as separate queries, at least one of the following must be different:
Typically, the query client is shared, and as shown in this fix, the query key was the same, which led me to suspect that this was the issue.
|
Thanks for the detailed explanation. I think we should keep the original behavior as it is how react query should behave: One query client, same cache. You must provide a different query client explicitly or implicitly through a provider. I don't see any reason why you would have this use case but with the introduced changes in this PR it would not be possible anymore to have two different fetchClient that "share the same cache". const client1 = createFetchClient<api1>()
const client2 = createFetchClient<api2>()
const query1 = createQueryClient(client1)
const query2 = createQueryClient(client2)
// They can share the same keys
query1.useQuery('get', '/posts')
query2.useQuery('get', '/posts')
query2.useQuery('get', '/posts', {}, queryClient2) // Use a different QueryClient |
So, from my understanding, if there are multiple base urls, then I hadn’t initially considered sharing cache key, but I can see how it might be desirable if the only difference lies in the middleware of In a situation where two different One advantage of maintaining this limitation, as mentioned, is the ability to treat different On the other hand, when Could you provide an example of the highlighted scenario above? |
I never had encountered a requirement like this, but we could imagine something with API versioning and a persistent storage for cache where you incrementally move usage from one fetch client (API v1) to second fetch client (API v2). You might still want your user to have the initial cache from API v1 even after moving some parts of your app to API v2. Anyway we are covering really uncommon cases. And in fact we already provide a way to cover both cases by allowing the user to provide a specific query client but I can understand this might be painful. A solution to cover both cases would be to give to the user the ability to provide a const client1 = createQueryClient(fetchClient, { prefixQueryKey: ['cache2'] })
const client2 = createQueryClient(fetchClient, { prefixQueryKey: ['cache1'] }) |
The example you provided seems plausible, and the suggested approach looks good. Below are some detailed considerations I thought about, and I’m curious if you have any other opinions on them. They are listed in order of preference:
The reason I believe a string key is sufficient is that React already uses string as key, and verifying that an unknown type doesn’t introduce serialization issues can be challenging. |
I don't think we should think about react keys but about react-query keys. The default value should be undefined and the type must match the The type of |
Background
How to Review
I'm unclear on the purpose of the added queryOptions parameter. If it's necessary, last commit can be excluded.options parameter is preserved for not being breaking.
Checklist
docs/
updated (if necessary)pnpm run update:examples
run (only applicable for openapi-typescript)