Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: tanstack-query build issues and bugs in optimistic update #843

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/plugins/tanstack-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
"svelte": "^4.2.1",
"swr": "^2.0.3",
"ts-jest": "^29.0.5",
"tsup": "^8.0.0",
"typescript": "^4.9.4",
"vue": "^3.3.4"
}
Expand Down
8 changes: 8 additions & 0 deletions packages/plugins/tanstack-query/scripts/postbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

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

// tsup incorrectly resolve to legacy types, make a fix here
console.log('Replacing @tanstack/react-query-v5/build/legacy/types');
replace.sync({
files: 'dist/runtime-v5/react*(.d.ts|.d.mts)',
from: /@tanstack\/react-query-v5\/build\/legacy\/types/g,
to: '@tanstack/react-query',
});

console.log('Replacing @tanstack/react-query-v5');
replace.sync({
files: 'dist/runtime-v5/react*(.d.ts|.d.mts|.js|.mjs)',
Expand Down
8 changes: 7 additions & 1 deletion packages/plugins/tanstack-query/src/runtime-v5/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,13 @@ export function useModelMutation<T, R = any, C extends boolean = boolean, Result
modelMeta,
finalOptions,
queryClient.getQueryCache().getAll(),
(queryKey, data) => queryClient.setQueryData<unknown>(queryKey, data),
(queryKey, data) => {
// update query cache
queryClient.setQueryData<unknown>(queryKey, data);
// cancel on-flight queries to avoid redundant cache updates,
// the settlement of the current mutation will trigger a new revalidation
queryClient.cancelQueries({ queryKey }, { revert: false, silent: true });
},
invalidateQueries ? (predicate) => queryClient.invalidateQueries({ predicate }) : undefined,
logging
);
Expand Down
8 changes: 8 additions & 0 deletions packages/plugins/tanstack-query/src/runtime/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ export function setupOptimisticUpdate(
const origOnMutate = options?.onMutate;
const origOnSettled = options?.onSettled;

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

// invalidate on settled
options.onSettled = async (...args: unknown[]) => {
if (invalidate) {
const [, , variables] = args;
Expand Down Expand Up @@ -293,11 +295,17 @@ async function optimisticUpdate(
} = cacheItem;

if (error) {
if (logging) {
console.warn(`Skipping optimistic update for ${JSON.stringify(queryKey)} due to error:`, error);
}
continue;
}

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

Expand Down
20 changes: 12 additions & 8 deletions packages/runtime/src/cross/mutator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,22 @@ export async function applyMutation(
},

update: (model, args) => {
const r = updateMutate(queryModel, resultData, model, args, modelMeta, logging);
if (r) {
resultData = r;
updated = true;
if (model === queryModel) {
const r = updateMutate(queryModel, resultData, model, args, modelMeta, logging);
if (r) {
resultData = r;
updated = true;
}
}
},

delete: (model, args) => {
const r = deleteMutate(queryModel, resultData, model, args, modelMeta, logging);
if (r) {
resultData = r;
updated = true;
if (model === queryModel) {
const r = deleteMutate(queryModel, resultData, model, args, modelMeta, logging);
if (r) {
resultData = r;
updated = true;
}
}
},
});
Expand Down
177 changes: 173 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading