Skip to content

Commit

Permalink
Merge pull request #176 from IdoPesok/mutation-keys
Browse files Browse the repository at this point in the history
Mutation keys
IdoPesok authored Jul 14, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents d3a9a15 + 6b422db commit b36e469
Showing 2 changed files with 18 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/healthy-fishes-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"zsa-react-query": patch
---

Added ability to pass a key factory for mutation keys
17 changes: 13 additions & 4 deletions packages/zsa-react-query/src/index.ts
Original file line number Diff line number Diff line change
@@ -31,7 +31,10 @@ export const createServerActionsKeyFactory = <
}

export const setupServerActionHooks = <
const TFactory extends
const TQueryFactory extends
| Readonly<ServerActionsKeyFactory<string[]>>
| undefined,
const TMutationFactory extends
| Readonly<ServerActionsKeyFactory<string[]>>
| undefined,
>(args: {
@@ -40,13 +43,18 @@ export const setupServerActionHooks = <
useMutation: typeof import("@tanstack/react-query").useMutation
useInfiniteQuery: typeof import("@tanstack/react-query").useInfiniteQuery
}
queryKeyFactory?: TFactory
queryKeyFactory?: TQueryFactory
mutationKeyFactory?: TMutationFactory
}) => {
const { useQuery, useMutation, useInfiniteQuery } = args.hooks

type TQueryKey = TFactory extends undefined
type TQueryKey = TQueryFactory extends undefined
? Readonly<unknown[]>
: Readonly<ServerActionKeys<Exclude<TQueryFactory, undefined>>>

type TMutationKey = TMutationFactory extends undefined
? Readonly<unknown[]>
: Readonly<ServerActionKeys<Exclude<TFactory, undefined>>>
: Readonly<ServerActionKeys<Exclude<TMutationFactory, undefined>>>

const useServerActionInfiniteQuery = <
TPageParam extends unknown,
@@ -184,6 +192,7 @@ export const setupServerActionHooks = <
"mutationFn"
> & {
returnError?: TReturnError
mutationKey?: TMutationKey
},
queryClient?: Parameters<typeof useMutation>[1]
): ReturnType<TUseMutation<THandler, TReturnError>> => {

0 comments on commit b36e469

Please sign in to comment.