|
1 | 1 | import { operateOnTags } from '../tags/operateOnTags'; |
2 | 2 | import { resolveTags } from '../tags/resolveTags'; |
3 | | -import { QueryTagContext, QueryTagOption, QueryUpdateTagObject } from '../tags/types'; |
4 | | -import { UpdateTagsUndoer, undoUpdateTags, updateTags } from '../tags/updateTags'; |
5 | | -import { MiddlewareFn } from './createMiddlewareFunction'; |
| 3 | +import type { QueryTagContext, QueryTagOption, QueryUpdateTagObject } from '../tags/types'; |
| 4 | +import { type UpdateTagsUndoer, undoUpdateTags, updateTags } from '../tags/updateTags'; |
| 5 | +import type { MiddlewareFn } from './createMiddlewareFunction'; |
6 | 6 |
|
7 | 7 | type CreateUpdateMiddleware = <TVars, TData, TError, TKey extends unknown[], TTags extends Record<string, unknown>>( |
8 | 8 | tags: QueryTagOption<TVars, TData, TError, QueryUpdateTagObject<TVars, TData, TError, TTags>>[], |
9 | 9 | ) => MiddlewareFn<TVars, TData, TError, TKey>; |
10 | 10 |
|
11 | 11 | export const createUpdateMiddleware: CreateUpdateMiddleware = (tags) => |
12 | | - async function updateMiddleware(ctx, next, throwError, config) { |
| 12 | + async function updateMiddleware(ctx, next, config) { |
13 | 13 | if (ctx.operationType !== 'mutation') return next(ctx); |
14 | 14 |
|
15 | 15 | type TagObj = QueryUpdateTagObject<any, any, any, any>; |
16 | 16 | type TagCtx = QueryTagContext<any>; |
17 | 17 |
|
18 | 18 | let undos: UpdateTagsUndoer[] | null = null; |
19 | 19 | const invalidates: TagObj[] = []; |
20 | | - const optCtx: TagCtx = { client: ctx.client, vars: ctx.vars, data: undefined }; |
| 20 | + const preCtx: TagCtx = { client: ctx.client, vars: ctx.vars, data: undefined }; |
21 | 21 |
|
22 | 22 | try { |
23 | | - const optUpdates = resolveTags<any, TagObj>({ tags, ...optCtx }).filter((u) => u.optimistic); |
| 23 | + const preUpdates = resolveTags({ tags, ...preCtx }).filter((u) => u.optimistic); |
24 | 24 | undos = updateTags({ |
25 | 25 | queryClient: ctx.client, |
26 | | - tags: optUpdates.filter((x) => x.updater), |
27 | | - ctx: optCtx, |
| 26 | + tags: preUpdates.filter((x) => x.updater), |
| 27 | + ctx: preCtx, |
28 | 28 | optimistic: true, |
29 | 29 | }); |
30 | | - invalidates.push(...optUpdates); |
| 30 | + invalidates.push(...preUpdates); |
31 | 31 |
|
32 | | - const optToInvalidate = optUpdates.filter((tag) => ['pre', 'both'].includes(tag.invalidate || 'both')); |
| 32 | + const optToInvalidate = preUpdates.filter((tag) => ['pre', 'both'].includes(tag.invalidate || 'both')); |
33 | 33 | operateOnTags({ queryClient: ctx.client, tags: optToInvalidate }, { refetchType: 'none' }); |
34 | 34 |
|
35 | 35 | const data = await next(ctx); |
36 | 36 |
|
37 | | - const pesCtx: TagCtx = { ...optCtx, data }; |
38 | | - const pesUpdates = resolveTags<any, TagObj>({ tags, ...pesCtx }).filter((u) => !u.optimistic); |
39 | | - updateTags({ queryClient: ctx.client, tags: pesUpdates.filter((x) => x.updater), ctx: pesCtx }); |
40 | | - invalidates.push(...pesUpdates); |
| 37 | + const postCtx: TagCtx = { ...preCtx, data }; |
| 38 | + const postUpdates = resolveTags({ tags, ...postCtx }).filter((u) => !u.optimistic); |
| 39 | + updateTags({ queryClient: ctx.client, tags: postUpdates.filter((x) => x.updater), ctx: postCtx }); |
| 40 | + invalidates.push(...postUpdates); |
41 | 41 |
|
42 | 42 | return data; |
43 | 43 | } catch (error) { |
44 | 44 | if (undos?.length) undoUpdateTags(undos, ctx.client); |
45 | 45 |
|
46 | | - const pesCtx: TagCtx = { ...optCtx, error }; |
47 | | - const pesUpdates = resolveTags<any, TagObj>({ tags, ...pesCtx }).filter((u) => !u.optimistic); |
48 | | - invalidates.push(...pesUpdates); |
| 46 | + const postCtx: TagCtx = { ...preCtx, error }; |
| 47 | + const postUpdates = resolveTags({ tags, ...postCtx }).filter((u) => !u.optimistic); |
| 48 | + invalidates.push(...postUpdates); |
49 | 49 |
|
50 | 50 | throw error; |
51 | 51 | } finally { |
|
0 commit comments