Skip to content

Commit

Permalink
feat(mutation): allow passing mutation variables to mutation key getter
Browse files Browse the repository at this point in the history
  • Loading branch information
ElisePatrikainen committed Mar 23, 2024
1 parent c6cd532 commit dab952b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/entry-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import type { EntryNodeKey } from './tree-map'
import type { _ObjectFlat } from './utils'

/**
* Key used to identify a query. Always an array.
* Key used to identify a query or a mutation. Always an array.
*/
export type EntryKey = Array<EntryNodeKey | _ObjectFlat>
15 changes: 15 additions & 0 deletions src/use-mutation.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@ import { expectTypeOf, it } from 'vitest'
import { useMutation } from './use-mutation'

it('types the parameters for the key', () => {
const { mutate } = useMutation({
mutation: (_one: string) => Promise.resolve({ name: 'foo' }),
key(result, one) {
expectTypeOf(one).toBeString()
expectTypeOf(result).toEqualTypeOf<{ name: string }>()
return ['foo']
},
})

mutate('one')
// @ts-expect-error: missing arg
mutate()
})

it('types the parameters for the keys', () => {
const { mutate } = useMutation({
mutation: (_one: string) => Promise.resolve({ name: 'foo' }),
keys(result, one) {
Expand Down
8 changes: 6 additions & 2 deletions src/use-mutation.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { computed, shallowRef } from 'vue'
import type { ComputedRef, MaybeRefOrGetter, ShallowRef } from 'vue'
import type { ComputedRef, ShallowRef } from 'vue'
import { useQueryCache } from './query-store'
import type { EntryKey } from './entry-options'
import type { ErrorDefault } from './types-extension'
import { type _Awaitable, noop } from './utils'

type _MutationKey<TVars, TResult> =
| EntryKey
| ((data: TResult, vars: TVars) => EntryKey)

// TODO: move to a plugin
/**
* The keys to invalidate when a mutation succeeds.
Expand Down Expand Up @@ -69,7 +73,7 @@ export interface UseMutationOptions<
*/
mutation: (vars: TVars, context: NoInfer<TContext>) => Promise<TResult>

key?: MaybeRefOrGetter<EntryKey>
key?: _MutationKey<TVars, TResult>

// TODO: move this to a plugin that calls invalidateEntry()
/**
Expand Down

0 comments on commit dab952b

Please sign in to comment.