ref: expose function to safeParse an queryKey into our ApiQueryKey schema#114026
Merged
ref: expose function to safeParse an queryKey into our ApiQueryKey schema#114026
Conversation
TkDodo
commented
Apr 27, 2026
| queryClient.invalidateQueries({ | ||
| predicate: query => { | ||
| const key = query.queryKey[0]; | ||
| const key = safeParseQueryKey(query.queryKey)?.url; |
Collaborator
Author
There was a problem hiding this comment.
note: this query.queryKey[0] does not work with v2 QueryKeys 🚨
Comment on lines
-19
to
+22
| function isIssueEndpointUrl(query: any) { | ||
| // v2 keys have metadata at [0] and URL at [1] | ||
| const key = query.queryKey; | ||
| const url = | ||
| typeof key[0] === 'object' && key[0]?.version === 'v2' | ||
| ? (key[1] ?? '') | ||
| : (key[0] ?? ''); | ||
| return issueApiEndpointRegexp.test(String(url)); | ||
| function isIssueEndpointUrl(query: {queryKey: readonly unknown[]}) { | ||
| const url = safeParseQueryKey(query.queryKey)?.url; | ||
| return url !== undefined && issueApiEndpointRegexp.test(url); |
Collaborator
Author
There was a problem hiding this comment.
this parsing here did work with v1 and v2, but was still manual and might return false-positives for non api queries
| ): queryKey is V1InfiniteQueryKey | V2InfiniteQueryKey { | ||
| return typeof queryKey[0] === 'object' && queryKey[0].infinite; | ||
| ): ParsedQueryKey { | ||
| return queryKeySchema.parse(queryKey); |
Collaborator
Author
There was a problem hiding this comment.
since the input is ApiQueryKey | InfiniteApiQueryKey, it should be safe here to do .parse (which can throw)
Comment on lines
-279
to
+287
| const [url, options] = query.queryKey as [ | ||
| string, | ||
| {query?: {referrer?: string}} | undefined, | ||
| ]; | ||
| return url === eventsUrl && options?.query?.referrer === 'replay_details'; | ||
| const queryKey = safeParseQueryKey(query.queryKey); | ||
| if (!queryKey) { | ||
| return false; | ||
| } | ||
| return ( | ||
| queryKey.url === eventsUrl && | ||
| queryKey.options?.query?.referrer === 'replay_details' | ||
| ); |
Collaborator
Author
There was a problem hiding this comment.
totally unsafe assertion here before
JonasBa
approved these changes
Apr 27, 2026
cleptric
pushed a commit
that referenced
this pull request
May 5, 2026
…hema (#114026) when used for a `predicate` inside a `QueryFilter`, what we get as input is `unknown[]`. However, our `parseQueryKey` could only pass api query keys of v1 or v2 schema. When queries are created manually without `useApiQuery` (v1) or `apiOptions` (v2) they might not conform to that structure. Therefore it’s unsafe to use `parseQueryKey` for `predicate` filter functions, and it would also type error. This PR introduces a `safeParseQueryKey` function that uses a zod schema to safely parse any QueryKey and transforms the various versions into a single, readable structure. It will return `undefined` if the structure doesn’t match. All places where we did manual `predicate` matching have been updated.
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.
when used for a
predicateinside aQueryFilter, what we get as input isunknown[]. However, ourparseQueryKeycould only pass api query keys of v1 or v2 schema. When queries are created manually withoutuseApiQuery(v1) orapiOptions(v2) they might not conform to that structure.Therefore it’s unsafe to use
parseQueryKeyforpredicatefilter functions, and it would also type error.This PR introduces a
safeParseQueryKeyfunction that uses a zod schema to safely parse any QueryKey and transforms the various versions into a single, readable structure. It will returnundefinedif the structure doesn’t match.All places where we did manual
predicatematching have been updated.