feat(openapi-react-query): add queryKeyFn option to createClient#2777
Open
codr wants to merge 2 commits intoopenapi-ts:mainfrom
Open
feat(openapi-react-query): add queryKeyFn option to createClient#2777codr wants to merge 2 commits intoopenapi-ts:mainfrom
codr wants to merge 2 commits intoopenapi-ts:mainfrom
Conversation
Allows callers to namespace TanStack Query keys per client instance, preventing unintended deduplication when two clients share endpoint paths but target different base URLs. Also refactors the shared queryFn into a per-call closure so it no longer destructures from the queryKey — a prerequisite for supporting custom key shapes. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
👷 Deploy request for openapi-ts pending review.Visit the deploys page to approve it
|
🦋 Changeset detectedLatest commit: caad8ea 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 |
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When an app uses two
openapi-fetchclients targeting different servers (e.g. an app API at:8000and a PDM API at:8100), any endpoint path shared between them produces identical TanStack Query keys[method, path, init]. TanStack Query fires only one request and both hooks receive the same cached data — even though they point at different servers.Fix
Add an optional
queryKeyFntocreateClient. When provided, it replaces the default[method, path, init]key, giving callers a way to namespace keys per client instance:This is purely additive — default behaviour is unchanged.
Implementation note
The existing shared
queryFndestructured[method, path, init]from the TanStack-providedqueryKeycontext. Allowing custom key shapes would have silently broken that destructuring. This PR refactorsqueryFninto a per-call closure that capturesmethod,path, andinitfrom the outerqueryOptionscall instead. The same fix is applied to theuseInfiniteQueryinline queryFn.Changes
src/index.ts— exportsQueryKeyFnandCreateClientOptionstypes; threadsqueryKeyFnthroughcreateClient; inlines the queryFn as a closuretest/index.test.tsx— newdescribe("queryKeyFn")block: default key shape, custom key shape, and no-dedup-across-clientsREADME.md— new Options section documentingqueryKeyFnwith two-client example.changeset/query-key-fn.md— minor bump changesetBreaking changes
None — purely additive; passing no
clientOptionsgives identical behaviour to before.