Skip to content
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

Fix queryKeyGeneration when using invalidateQuery #3728

Merged
Merged
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions packages/blitz-rpc/src/data-client/react-query-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,23 @@ const sanitize =
export const sanitizeQuery = sanitize("query")
export const sanitizeMutation = sanitize("mutation")

type BlitzNoParams = {__blitz_no_params_passed: true}
export const getQueryKeyFromUrlAndParams = (url: string, params: unknown) => {
const queryKey = [url]

const args = typeof params === "function" ? (params as Function)() : params
queryKey.push(serialize(args) as any)
if (
!(typeof params === "object" && (params as BlitzNoParams)["__blitz_no_params_passed"] === true)
) {
const args = typeof params === "function" ? (params as Function)() : params
queryKey.push(serialize(args) as any)
}

return queryKey as [string, any]
}

export function getQueryKey<TInput, TResult, T extends AsyncFunc>(
resolver: T | Resolver<TInput, TResult> | RpcClient<TInput, TResult>,
params?: TInput,
params: TInput | BlitzNoParams = {__blitz_no_params_passed: true},
) {
if (typeof resolver === "undefined") {
throw new Error("getQueryKey is missing the first argument - it must be a resolver function")
Expand All @@ -160,7 +165,7 @@ export function getInfiniteQueryKey<TInput, TResult, T extends AsyncFunc>(

export function invalidateQuery<TInput, TResult, T extends AsyncFunc>(
resolver: T | Resolver<TInput, TResult> | RpcClient<TInput, TResult>,
params?: TInput,
params: TInput | BlitzNoParams = {__blitz_no_params_passed: true},
) {
if (typeof resolver === "undefined") {
throw new Error(
Expand Down