-
-
Notifications
You must be signed in to change notification settings - Fork 798
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix queryKeyGeneration when using invalidateQuery (#3728)
Co-authored-by: beerose <[email protected]>
- Loading branch information
Showing
9 changed files
with
566 additions
and
59 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@blitzjs/rpc": patch | ||
"blitz": patch | ||
--- | ||
|
||
Fix invalidateQuery generating wrong param when no param argument is passed |
This file contains 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
6 changes: 6 additions & 0 deletions
6
integration-tests/react-query-utils/app/queries/getSequence.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const cache = {} | ||
|
||
export default async function getSequence(key: string) { | ||
cache[key] = cache[key] || 0 | ||
return cache[key]++ | ||
} |
55 changes: 55 additions & 0 deletions
55
integration-tests/react-query-utils/pages/page-with-invalidate.tsx
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import React, {Suspense} from "react" | ||
import {BlitzPage} from "@blitzjs/next" | ||
import {invalidateQuery, useQuery} from "@blitzjs/rpc" | ||
import getSequence from "../app/queries/getSequence" | ||
|
||
const useQueryOptions = { | ||
refetchInterval: 0, | ||
refetchOnMount: false, | ||
refetchOnReconnect: false, | ||
refetchOnWindowFocus: false, | ||
} | ||
|
||
const PageWithInvalidateQuery: React.FC = () => { | ||
const [query1, {isFetching: isQ1Fetching}] = useQuery(getSequence, "query1", useQueryOptions) | ||
const [query2, {isFetching: isQ2Fetching}] = useQuery(getSequence, "query2", useQueryOptions) | ||
|
||
const isFetching = isQ1Fetching || isQ2Fetching | ||
|
||
const onRevalidateBoth = async () => { | ||
await invalidateQuery(getSequence) | ||
} | ||
const onRevalidateFirst = async () => { | ||
await invalidateQuery(getSequence, "query1") | ||
} | ||
|
||
return ( | ||
<div> | ||
<h1>Hello from PageWithInvalidateQuery</h1> | ||
<button id="invalidate-both" onClick={onRevalidateBoth}> | ||
Both | ||
</button> | ||
<button id="invalidate-first" onClick={onRevalidateFirst}> | ||
First | ||
</button> | ||
|
||
{isFetching && <h3>Loading...</h3>} | ||
{!isFetching && ( | ||
<div id="data"> | ||
<h2 id="data-first">{query1}</h2> | ||
<h2 id="data-second">{query2}</h2> | ||
</div> | ||
)} | ||
</div> | ||
) | ||
} | ||
|
||
const PageWithInvalidateQueryPage: BlitzPage = () => { | ||
return ( | ||
<Suspense fallback={<h1>Loading...</h1>}> | ||
<PageWithInvalidateQuery /> | ||
</Suspense> | ||
) | ||
} | ||
|
||
export default PageWithInvalidateQueryPage |
This file contains 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
77 changes: 77 additions & 0 deletions
77
packages/blitz-rpc/src/data-client/react-query-utils.test.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import {describe, expect, it} from "vitest" | ||
import superJson from "superjson" | ||
|
||
import {getQueryKey, getQueryKeyFromUrlAndParams} from "./react-query-utils" | ||
import {RpcClient} from "./rpc" | ||
|
||
const API_ENDPOINT = "http://localhost:3000" | ||
|
||
const constructData = (arg: any) => { | ||
return { | ||
data: arg, | ||
expected: superJson.serialize(arg), | ||
} | ||
} | ||
|
||
describe("react-query-utils", () => { | ||
describe("getQueryKeyFromUrlAndParams", () => { | ||
it("returns a query key with string arg", () => { | ||
const {data, expected} = constructData("RandomString") | ||
expect(getQueryKeyFromUrlAndParams(API_ENDPOINT, data)).toEqual([API_ENDPOINT, expected]) | ||
}) | ||
|
||
it("returns a query key with object arg", () => { | ||
const {data, expected} = constructData({id: 1, name: "test", field: undefined}) | ||
expect(getQueryKeyFromUrlAndParams(API_ENDPOINT, data)).toEqual([API_ENDPOINT, expected]) | ||
}) | ||
|
||
it("returns a query key with undefined arg", () => { | ||
const {data, expected} = constructData(undefined) | ||
expect(getQueryKeyFromUrlAndParams(API_ENDPOINT, data)).toEqual([API_ENDPOINT, expected]) | ||
}) | ||
|
||
it("returns a query key with null arg", () => { | ||
const {data, expected} = constructData(null) | ||
expect(getQueryKeyFromUrlAndParams(API_ENDPOINT, data)).toEqual([API_ENDPOINT, expected]) | ||
}) | ||
|
||
it("if no argument is passed it returns only url", () => { | ||
const queryKey = getQueryKeyFromUrlAndParams(API_ENDPOINT) | ||
expect(queryKey).toEqual([API_ENDPOINT]) | ||
}) | ||
}) | ||
|
||
describe("getQueryKey", () => { | ||
// @ts-expect-error - we just need these 3 params | ||
const query: RpcClient<{}, null> = { | ||
_resolverName: "randomQuery", | ||
_resolverType: "query", | ||
_routePath: API_ENDPOINT, | ||
} | ||
|
||
it("returns a query key with string arg", () => { | ||
const {data, expected} = constructData("RandomString") | ||
expect(getQueryKey(query, data)).toEqual([API_ENDPOINT, expected]) | ||
}) | ||
|
||
it("returns a query key with object arg", () => { | ||
const {data, expected} = constructData({id: 1, name: "test", field: undefined}) | ||
expect(getQueryKey(query, data)).toEqual([API_ENDPOINT, expected]) | ||
}) | ||
|
||
it("returns a query key with undefined arg", () => { | ||
const {data, expected} = constructData(undefined) | ||
expect(getQueryKey(query, data)).toEqual([API_ENDPOINT, expected]) | ||
}) | ||
|
||
it("returns a query key with null arg", () => { | ||
const {data, expected} = constructData(null) | ||
expect(getQueryKey(query, data)).toEqual([API_ENDPOINT, expected]) | ||
}) | ||
|
||
it("if no argument is passed it returns only url", () => { | ||
const queryKey = getQueryKey(query) | ||
expect(queryKey).toEqual([API_ENDPOINT]) | ||
}) | ||
}) | ||
}) |
This file contains 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
Oops, something went wrong.