refactor: Introduce granular query types with specific policies for improved caching and data freshness.

This commit is contained in:
pa
2026-03-11 14:37:43 +09:00
parent 14d73b1532
commit a75c4b89f8
23 changed files with 221 additions and 39 deletions
+57
View File
@@ -1,5 +1,16 @@
import { beforeEach, describe, expect, test, vi } from 'vitest';
const mockLogWebRequest = vi.fn();
const mockWithQueryLog = vi.fn(async (fn) => fn());
vi.mock('../../services/appConfig', () => ({
AppDebug: {
debugWebRequests: false
},
logWebRequest: (...args) => mockLogWebRequest(...args),
withQueryLog: (...args) => mockWithQueryLog(...args)
}));
import {
_entityCacheInternals,
fetchWithEntityPolicy,
@@ -13,6 +24,8 @@ describe('entity query cache helpers', () => {
beforeEach(() => {
queryClient.clear();
vi.restoreAllMocks();
mockLogWebRequest.mockClear();
mockWithQueryLog.mockClear();
});
test('reports cache hit for fresh data', async () => {
@@ -75,6 +88,50 @@ describe('entity query cache helpers', () => {
expect(callCount).toBe(2);
});
test('uses label in logs without changing cache behavior', async () => {
const queryKey = ['user', 'usr_9'];
const policy = {
staleTime: 20000,
gcTime: 90000,
retry: 1,
refetchOnWindowFocus: false
};
const queryFn = vi.fn(async () => ({
json: { id: 'usr_9', updated_at: '2026-01-01T00:00:00.000Z' },
params: { userId: 'usr_9' },
ref: { id: 'usr_9', updated_at: '2026-01-01T00:00:00.000Z' }
}));
const first = await fetchWithEntityPolicy({
queryKey,
policy,
queryFn,
label: 'user.dialog'
});
const second = await fetchWithEntityPolicy({
queryKey,
policy,
queryFn
});
expect(first.cache).toBe(false);
expect(second.cache).toBe(true);
expect(mockLogWebRequest).toHaveBeenNthCalledWith(
1,
'[QUERY FETCH]',
'user.dialog',
queryKey,
expect.any(Object)
);
expect(mockLogWebRequest).toHaveBeenNthCalledWith(
2,
'[QUERY CACHE HIT]',
'user',
queryKey,
expect.any(Object)
);
});
test('does not overwrite newer data with older payload', () => {
const queryKey = ['world', 'wrld_1'];
+6 -6
View File
@@ -26,8 +26,8 @@ describe('query policy configuration', () => {
});
expect(entityQueryPolicies.group).toMatchObject({
staleTime: 60000,
gcTime: 300000,
staleTime: 300000,
gcTime: 1800000,
retry: 1,
refetchOnWindowFocus: false
});
@@ -89,8 +89,8 @@ describe('query policy configuration', () => {
test('file-related policies', () => {
expect(entityQueryPolicies.fileAnalysis).toMatchObject({
staleTime: 120000,
gcTime: 600000,
staleTime: 600000,
gcTime: 3600000,
retry: 1,
refetchOnWindowFocus: false
});
@@ -164,8 +164,8 @@ describe('query policy configuration', () => {
const options = toQueryOptions(entityQueryPolicies.group);
expect(options).toEqual({
staleTime: 60000,
gcTime: 300000,
staleTime: 300000,
gcTime: 1800000,
retry: 1,
refetchOnWindowFocus: false
});
+4 -4
View File
@@ -140,10 +140,10 @@ export function patchQueryDataWithRecency({ queryKey, nextData }) {
}
/**
* @param {{queryKey: unknown[], policy: {staleTime: number, gcTime: number, retry: number, refetchOnWindowFocus: boolean}, queryFn: () => Promise<any>}} options
* @param {{queryKey: unknown[], policy: {staleTime: number, gcTime: number, retry: number, refetchOnWindowFocus: boolean}, queryFn: () => Promise<any>, label?: string}} options
* @returns {Promise<{data: any, cache: boolean}>}
*/
export async function fetchWithEntityPolicy({ queryKey, policy, queryFn }) {
export async function fetchWithEntityPolicy({ queryKey, policy, queryFn, label }) {
const queryState = queryClient.getQueryState(queryKey);
const isFresh =
Boolean(queryState?.dataUpdatedAt) &&
@@ -157,9 +157,9 @@ export async function fetchWithEntityPolicy({ queryKey, policy, queryFn }) {
});
if (isFresh) {
logWebRequest('[QUERY CACHE HIT]', queryKey, data);
logWebRequest('[QUERY CACHE HIT]', label || queryKey[0], queryKey, data);
} else {
logWebRequest('[QUERY FETCH]', queryKey, data);
logWebRequest('[QUERY FETCH]', label || queryKey[0], queryKey, data);
}
return {