-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Julio García
committed
Aug 10, 2021
1 parent
65357cc
commit b779760
Showing
3 changed files
with
34 additions
and
31 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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export { default as useMutation } from "./useMutation"; | ||
export { default as useQuery } from "./useQuery"; | ||
export { default as wrapRootComponent } from "./wrapRootComponent"; | ||
export { invalidateQuery } from "./utils"; |
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
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,33 @@ | ||
import { QueryClient } from "react-query"; | ||
import { AnyFunction } from "./ts-helpers"; | ||
import { useQueryClient as useRQQueryClient } from "react-query"; | ||
|
||
interface ExtendedQueryClient extends QueryClient { | ||
invalidateQuery: (queryKey: AnyFunction) => void; | ||
invalidateQueryKey: (queryKey: string) => void; | ||
} | ||
|
||
function useQueryClient( | ||
...args: Parameters<typeof useRQQueryClient> | ||
): ExtendedQueryClient { | ||
const client = useRQQueryClient(...args) as ExtendedQueryClient; | ||
|
||
client.invalidateQueryKey = function (queryKey: string) { | ||
this.invalidateQueries(queryKey); | ||
}; | ||
client.invalidateQuery = function (queryKey: AnyFunction) { | ||
// This is not a arbitrary convertion. queryKey works differently between the TS world | ||
// and the runtime world: | ||
// - In the TS world, this is a function | ||
// - In the runtime world, this is a string | ||
const queryKeyAsString = queryKey as unknown as string; | ||
this.invalidateQueryKey(queryKeyAsString); | ||
}; | ||
|
||
client.invalidateQueryKey = client.invalidateQueryKey.bind(client); | ||
client.invalidateQuery = client.invalidateQuery.bind(client); | ||
|
||
return client; | ||
} | ||
|
||
export default useQueryClient; |