Skip to content

Commit 08d317d

Browse files
authored
fix: tanstack-query build issues and bugs in optimistic update (#843)
1 parent 9a0895b commit 08d317d

File tree

6 files changed

+209
-13
lines changed

6 files changed

+209
-13
lines changed

packages/plugins/tanstack-query/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
"svelte": "^4.2.1",
118118
"swr": "^2.0.3",
119119
"ts-jest": "^29.0.5",
120+
"tsup": "^8.0.0",
120121
"typescript": "^4.9.4",
121122
"vue": "^3.3.4"
122123
}

packages/plugins/tanstack-query/scripts/postbuild.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
const replace = require('replace-in-file');
44

5+
// tsup incorrectly resolve to legacy types, make a fix here
6+
console.log('Replacing @tanstack/react-query-v5/build/legacy/types');
7+
replace.sync({
8+
files: 'dist/runtime-v5/react*(.d.ts|.d.mts)',
9+
from: /@tanstack\/react-query-v5\/build\/legacy\/types/g,
10+
to: '@tanstack/react-query',
11+
});
12+
513
console.log('Replacing @tanstack/react-query-v5');
614
replace.sync({
715
files: 'dist/runtime-v5/react*(.d.ts|.d.mts|.js|.mjs)',

packages/plugins/tanstack-query/src/runtime-v5/react.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,13 @@ export function useModelMutation<T, R = any, C extends boolean = boolean, Result
159159
modelMeta,
160160
finalOptions,
161161
queryClient.getQueryCache().getAll(),
162-
(queryKey, data) => queryClient.setQueryData<unknown>(queryKey, data),
162+
(queryKey, data) => {
163+
// update query cache
164+
queryClient.setQueryData<unknown>(queryKey, data);
165+
// cancel on-flight queries to avoid redundant cache updates,
166+
// the settlement of the current mutation will trigger a new revalidation
167+
queryClient.cancelQueries({ queryKey }, { revert: false, silent: true });
168+
},
163169
invalidateQueries ? (predicate) => queryClient.invalidateQueries({ predicate }) : undefined,
164170
logging
165171
);

packages/plugins/tanstack-query/src/runtime/common.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ export function setupOptimisticUpdate(
246246
const origOnMutate = options?.onMutate;
247247
const origOnSettled = options?.onSettled;
248248

249+
// optimistic update on mutate
249250
options.onMutate = async (...args: unknown[]) => {
250251
const [variables] = args;
251252
await optimisticUpdate(
@@ -260,6 +261,7 @@ export function setupOptimisticUpdate(
260261
return origOnMutate?.(...args);
261262
};
262263

264+
// invalidate on settled
263265
options.onSettled = async (...args: unknown[]) => {
264266
if (invalidate) {
265267
const [, , variables] = args;
@@ -293,11 +295,17 @@ async function optimisticUpdate(
293295
} = cacheItem;
294296

295297
if (error) {
298+
if (logging) {
299+
console.warn(`Skipping optimistic update for ${JSON.stringify(queryKey)} due to error:`, error);
300+
}
296301
continue;
297302
}
298303

299304
const [_, queryModel, queryOp, _queryArgs, { optimisticUpdate }] = queryKey as QueryKey;
300305
if (!optimisticUpdate) {
306+
if (logging) {
307+
console.log(`Skipping optimistic update for ${JSON.stringify(queryKey)} due to opt-out`);
308+
}
301309
continue;
302310
}
303311

packages/runtime/src/cross/mutator.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,22 @@ export async function applyMutation(
6666
},
6767

6868
update: (model, args) => {
69-
const r = updateMutate(queryModel, resultData, model, args, modelMeta, logging);
70-
if (r) {
71-
resultData = r;
72-
updated = true;
69+
if (model === queryModel) {
70+
const r = updateMutate(queryModel, resultData, model, args, modelMeta, logging);
71+
if (r) {
72+
resultData = r;
73+
updated = true;
74+
}
7375
}
7476
},
7577

7678
delete: (model, args) => {
77-
const r = deleteMutate(queryModel, resultData, model, args, modelMeta, logging);
78-
if (r) {
79-
resultData = r;
80-
updated = true;
79+
if (model === queryModel) {
80+
const r = deleteMutate(queryModel, resultData, model, args, modelMeta, logging);
81+
if (r) {
82+
resultData = r;
83+
updated = true;
84+
}
8185
}
8286
},
8387
});

pnpm-lock.yaml

Lines changed: 173 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)