|
| 1 | +import { MutationFunction, useMutation, useQueryClient } from '@tanstack/react-query'; |
| 2 | +import { operateOnTags } from '../tags/operateOnTags'; |
| 3 | +import { QueryTagContext, QueryUpdateTag, TagOperationOptions } from '../tags/types'; |
| 4 | +import { updateTags } from '../tags/updateTags'; |
| 5 | +import { WithOptional } from '../type-utils'; |
| 6 | +import { QueryBuilderFrozen } from './QueryBuilderFrozen'; |
| 7 | +import { BuilderConfig } from './types'; |
| 8 | + |
| 9 | +export class QueryBuilderTagsManager<TVars, TData, TError, TKey extends unknown[], TTags extends Record<string, unknown>> { |
| 10 | + private declare _options: BuilderConfig<TVars, TData, TError, TKey>['options']; |
| 11 | + constructor(private builder: QueryBuilderFrozen<TVars, TData, TError, TKey, TTags>) {} |
| 12 | + |
| 13 | + /** |
| 14 | + * This hook returns a function that can be used to operate on queries based on tags. |
| 15 | + * It also returns the mutation object that can be used to track the state of the operation. |
| 16 | + */ |
| 17 | + useOperation(opts?: TagOperationOptions<TTags> | void) { |
| 18 | + const queryClient = useQueryClient(this.builder.config.queryClient); |
| 19 | + const mutationFn: MutationFunction<unknown, TagOperationOptions<TTags> | void> = ( |
| 20 | + { tags = [], operation = 'invalidate', filters, options } = opts || {}, |
| 21 | + ) => operateOnTags({ queryClient, tags, operation }, filters, options); |
| 22 | + |
| 23 | + const mutation = useMutation({ mutationFn }); |
| 24 | + const operate = mutation.mutateAsync; |
| 25 | + |
| 26 | + return [operate, mutation] as const; |
| 27 | + } |
| 28 | + |
| 29 | + readonly operate = ({ tags = [], operation = 'invalidate', filters, options }: TagOperationOptions<TTags>) => |
| 30 | + operateOnTags({ queryClient: this.builder.config.queryClient!, tags, operation }, filters, options); |
| 31 | + |
| 32 | + readonly cancel = (args: Omit<TagOperationOptions<TTags>, 'operation'>) => this.operate({ ...args, operation: 'cancel' }); |
| 33 | + readonly invalidate = (args: Omit<TagOperationOptions<TTags>, 'operation'>) => this.operate({ ...args, operation: 'invalidate' }); |
| 34 | + readonly refetch = (args: Omit<TagOperationOptions<TTags>, 'operation'>) => this.operate({ ...args, operation: 'refetch' }); |
| 35 | + readonly remove = (args: Omit<TagOperationOptions<TTags>, 'operation'>) => this.operate({ ...args, operation: 'remove' }); |
| 36 | + readonly reset = (args: Omit<TagOperationOptions<TTags>, 'operation'>) => this.operate({ ...args, operation: 'reset' }); |
| 37 | + |
| 38 | + /** |
| 39 | + * This function can be used to update the queries in cache based on given tags. |
| 40 | + */ |
| 41 | + readonly update = ({ |
| 42 | + tags = [], |
| 43 | + optimistic = false, |
| 44 | + client = this.builder.config.queryClient, |
| 45 | + ...ctx |
| 46 | + }: { |
| 47 | + tags: readonly QueryUpdateTag<TVars, TData, TError, TTags>[]; |
| 48 | + optimistic?: boolean; |
| 49 | + } & WithOptional<QueryTagContext<TVars, TData, TError>, 'client'>) => |
| 50 | + updateTags({ tags, queryClient: client!, ctx: { client: client!, ...ctx }, optimistic }); |
| 51 | +} |
0 commit comments