Skip to content

Commit 08ac462

Browse files
committed
fix(tanstack-query): non-zenstack queries are accidentally visited during optimistic update
Fixes #1774
1 parent aae9b60 commit 08ac462

File tree

2 files changed

+85
-3
lines changed

2 files changed

+85
-3
lines changed

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,15 +385,20 @@ async function optimisticUpdate(
385385
state: { data, error },
386386
} = cacheItem;
387387

388+
if (!isZenStackQueryKey(queryKey)) {
389+
// skip non-zenstack queries
390+
continue;
391+
}
392+
388393
if (error) {
389394
if (logging) {
390395
console.warn(`Skipping optimistic update for ${JSON.stringify(queryKey)} due to error:`, error);
391396
}
392397
continue;
393398
}
394399

395-
const [_, queryModel, queryOperation, queryArgs, { optimisticUpdate }] = queryKey as QueryKey;
396-
if (!optimisticUpdate) {
400+
const [_, queryModel, queryOperation, queryArgs, queryOptions] = queryKey;
401+
if (!queryOptions?.optimisticUpdate) {
397402
if (logging) {
398403
console.log(`Skipping optimistic update for ${JSON.stringify(queryKey)} due to opt-out`);
399404
}
@@ -450,3 +455,15 @@ async function optimisticUpdate(
450455
}
451456
}
452457
}
458+
459+
function isZenStackQueryKey(queryKey: readonly unknown[]): queryKey is QueryKey {
460+
if (queryKey.length < 5) {
461+
return false;
462+
}
463+
464+
if (queryKey[0] !== QUERY_KEY_PREFIX) {
465+
return false;
466+
}
467+
468+
return true;
469+
}

packages/plugins/tanstack-query/tests/react-hooks-v5.test.tsx

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44
/* eslint-disable @typescript-eslint/no-explicit-any */
55
/* eslint-disable @typescript-eslint/ban-ts-comment */
6-
import { QueryClient, QueryClientProvider } from '@tanstack/react-query-v5';
6+
import { QueryClient, QueryClientProvider, useQuery } from '@tanstack/react-query-v5';
77
import { act, renderHook, waitFor } from '@testing-library/react';
88
import nock from 'nock';
99
import React from 'react';
@@ -863,4 +863,69 @@ describe('Tanstack Query React Hooks V5 Test', () => {
863863
expect(cacheData[0].name).toBe('foohooray');
864864
});
865865
});
866+
867+
it('optimistic update mixed with non-zenstack queries', async () => {
868+
const { queryClient, wrapper } = createWrapper();
869+
870+
// non-zenstack query
871+
const { result: myQueryResult } = renderHook(
872+
() => useQuery({ queryKey: ['myQuery'], queryFn: () => ({ data: 'myData' }) }),
873+
{
874+
wrapper,
875+
}
876+
);
877+
await waitFor(() => {
878+
expect(myQueryResult.current.data).toEqual({ data: 'myData' });
879+
});
880+
881+
const data: any[] = [];
882+
883+
nock(makeUrl('User', 'findMany'))
884+
.get(/.*/)
885+
.reply(200, () => {
886+
console.log('Querying data:', JSON.stringify(data));
887+
return { data };
888+
})
889+
.persist();
890+
891+
const { result } = renderHook(
892+
() => useModelQuery('User', makeUrl('User', 'findMany'), undefined, { optimisticUpdate: true }),
893+
{
894+
wrapper,
895+
}
896+
);
897+
await waitFor(() => {
898+
expect(result.current.data).toHaveLength(0);
899+
});
900+
901+
nock(makeUrl('User', 'create'))
902+
.post(/.*/)
903+
.reply(200, () => {
904+
console.log('Not mutating data');
905+
return { data: null };
906+
});
907+
908+
const { result: mutationResult } = renderHook(
909+
() =>
910+
useModelMutation('User', 'POST', makeUrl('User', 'create'), modelMeta, {
911+
optimisticUpdate: true,
912+
invalidateQueries: false,
913+
}),
914+
{
915+
wrapper,
916+
}
917+
);
918+
919+
act(() => mutationResult.current.mutate({ data: { name: 'foo' } }));
920+
921+
await waitFor(() => {
922+
const cacheData: any = queryClient.getQueryData(
923+
getQueryKey('User', 'findMany', undefined, { infinite: false, optimisticUpdate: true })
924+
);
925+
expect(cacheData).toHaveLength(1);
926+
expect(cacheData[0].$optimistic).toBe(true);
927+
expect(cacheData[0].id).toBeTruthy();
928+
expect(cacheData[0].name).toBe('foo');
929+
});
930+
});
866931
});

0 commit comments

Comments
 (0)