Skip to content

Commit 75928f5

Browse files
committed
Added new queryKeys pattern for cache tagging
1 parent a5e5800 commit 75928f5

File tree

4 files changed

+64
-18
lines changed

4 files changed

+64
-18
lines changed

src/Exceptionless.Web/ClientApp/src/lib/api/eventsApi.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
import { createQuery } from '@tanstack/svelte-query';
1+
import { createQuery, useQueryClient } from '@tanstack/svelte-query';
22
import type { PersistentEvent } from '$lib/models/api';
33
import { FetchClient, type ProblemDetails } from '$api/FetchClient';
44
import { derived, readable, type Readable } from 'svelte/store';
55

6-
export const queryKey: string = 'PersistentEvent';
6+
export const queryKeys = {
7+
all: ['PersistentEvent'] as const,
8+
allWithFilters: (filters: string) => [...queryKeys.all, { filters }] as const,
9+
stacks: (id: string | null) => [...queryKeys.all, 'stacks', id] as const,
10+
stackWithFilters: (id: string | null, filters: string) => [...queryKeys.stacks(id), { filters }] as const,
11+
id: (id: string | null) => [...queryKeys.all, id] as const
12+
};
713

814
export function getEventByIdQuery(id: string | Readable<string | null>) {
915
const readableId = typeof id === 'string' || id === null ? readable(id) : id;
1016
return createQuery<PersistentEvent, ProblemDetails>(
1117
derived(readableId, ($id) => ({
1218
enabled: !!$id,
13-
queryKey: [queryKey, $id],
19+
queryKey: queryKeys.id($id),
1420
queryFn: async ({ signal }: { signal: AbortSignal }) => {
1521
const { getJSON } = new FetchClient();
1622
const response = await getJSON<PersistentEvent>(`events/${$id}`, {
@@ -28,21 +34,27 @@ export function getEventByIdQuery(id: string | Readable<string | null>) {
2834
}
2935

3036
export function getEventsByStackIdQuery(stackId: string | Readable<string | null>, limit: number = 10) {
37+
const queryClient = useQueryClient();
3138
const readableStackId = typeof stackId === 'string' || stackId === null ? readable(stackId) : stackId;
32-
return createQuery<PersistentEvent, ProblemDetails>(
39+
return createQuery<PersistentEvent[], ProblemDetails>(
3340
derived(readableStackId, ($id) => ({
3441
enabled: !!$id,
35-
queryKey: [queryKey, 'stack', $id],
42+
queryClient,
43+
queryKey: queryKeys.stacks($id),
3644
queryFn: async ({ signal }: { signal: AbortSignal }) => {
3745
const { getJSON } = new FetchClient();
38-
const response = await getJSON<PersistentEvent>(`stacks/${$id}/events`, {
46+
const response = await getJSON<PersistentEvent[]>(`stacks/${$id}/events`, {
3947
signal,
4048
params: {
4149
limit
4250
}
4351
});
4452

4553
if (response.ok) {
54+
response.data?.forEach((event) => {
55+
queryClient.setQueryData(queryKeys.id(event.id!), event);
56+
});
57+
4658
return response.data!;
4759
}
4860

src/Exceptionless.Web/ClientApp/src/lib/api/projectsApi.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@ import { derived, readable, type Readable } from 'svelte/store';
33
import type { ViewProject } from '$lib/models/api';
44
import { FetchClient, type FetchClientResponse, type ProblemDetails } from '$api/FetchClient';
55

6-
export const queryKey: string = 'Project';
6+
export const queryKeys = {
7+
all: ['Project'] as const,
8+
allWithFilters: (filters: string) => [...queryKeys.all, { filters }] as const,
9+
id: (id: string | null) => [...queryKeys.all, id] as const
10+
};
711

812
export function getProjectByIdQuery(id: string | Readable<string | null>) {
913
const readableId = typeof id === 'string' || id === null ? readable(id) : id;
1014
return createQuery<ViewProject, ProblemDetails>(
1115
derived(readableId, ($id) => ({
1216
enabled: !!$id,
13-
queryKey: [queryKey, $id],
17+
queryKey: queryKeys.id($id),
1418
queryFn: async ({ signal }: { signal: AbortSignal }) => {
1519
const { getJSON } = new FetchClient();
1620
const response = await getJSON<ViewProject>(`projects/${$id}`, {
@@ -30,7 +34,7 @@ export function getProjectByIdQuery(id: string | Readable<string | null>) {
3034
export function mutatePromoteTab(id: string) {
3135
const client = useQueryClient();
3236
return createMutation<FetchClientResponse<unknown>, ProblemDetails, { name: string }>({
33-
mutationKey: [queryKey, id],
37+
mutationKey: queryKeys.id(id),
3438
mutationFn: async ({ name }) => {
3539
const { post } = new FetchClient();
3640
const response = await post(`projects/${id}/promotedtabs`, undefined, {
@@ -44,15 +48,15 @@ export function mutatePromoteTab(id: string) {
4448
throw response.problem;
4549
},
4650
onSettled: () => {
47-
client.invalidateQueries({ queryKey: [queryKey, id] });
51+
client.invalidateQueries({ queryKey: queryKeys.id(id) });
4852
}
4953
});
5054
}
5155

5256
export function mutateDemoteTab(id: string) {
5357
const client = useQueryClient();
5458
return createMutation<FetchClientResponse<unknown>, ProblemDetails, { name: string }>({
55-
mutationKey: [queryKey, id],
59+
mutationKey: queryKeys.id(id),
5660
mutationFn: async ({ name }) => {
5761
const { remove } = new FetchClient();
5862
const response = await remove(`projects/${id}/promotedtabs`, {
@@ -66,7 +70,7 @@ export function mutateDemoteTab(id: string) {
6670
throw response.problem;
6771
},
6872
onSettled: () => {
69-
client.invalidateQueries({ queryKey: [queryKey, id] });
73+
client.invalidateQueries({ queryKey: queryKeys.id(id) });
7074
}
7175
});
7276
}

src/Exceptionless.Web/ClientApp/src/lib/api/stacksApi.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,39 @@
1-
import { createQuery } from '@tanstack/svelte-query';
1+
import { createQuery, useQueryClient } from '@tanstack/svelte-query';
22
import type { Stack } from '$lib/models/api';
33
import { FetchClient, type ProblemDetails } from '$api/FetchClient';
44
import { derived, readable, type Readable } from 'svelte/store';
55

6-
export const queryKey: string = 'Stack';
6+
export const queryKeys = {
7+
all: ['Stack'] as const,
8+
allWithFilters: (filters: string) => [...queryKeys.all, { filters }] as const,
9+
id: (id: string | null) => [...queryKeys.all, id] as const
10+
};
11+
12+
export async function prefetchStack(id: string) {
13+
const queryClient = useQueryClient();
14+
await queryClient.prefetchQuery<Stack, ProblemDetails>({
15+
queryKey: queryKeys.id(id),
16+
queryFn: async ({ signal }: { signal: AbortSignal }) => {
17+
const { getJSON } = new FetchClient();
18+
const response = await getJSON<Stack>(`stacks/${id}`, {
19+
signal
20+
});
21+
22+
if (response.ok) {
23+
return response.data!;
24+
}
25+
26+
throw response.problem;
27+
}
28+
});
29+
}
730

831
export function getStackByIdQuery(id: string | Readable<string | null>) {
932
const readableId = typeof id === 'string' || id === null ? readable(id) : id;
1033
return createQuery<Stack, ProblemDetails>(
1134
derived(readableId, ($id) => ({
1235
enabled: !!$id,
13-
queryKey: [queryKey, $id],
36+
queryKey: queryKeys.id($id),
1437
queryFn: async ({ signal }: { signal: AbortSignal }) => {
1538
const { getJSON } = new FetchClient();
1639
const response = await getJSON<Stack>(`stacks/${$id}`, {

src/Exceptionless.Web/ClientApp/src/lib/api/usersApi.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
11
import { derived } from 'svelte/store';
2-
import { createQuery } from '@tanstack/svelte-query';
2+
import { createQuery, useQueryClient } from '@tanstack/svelte-query';
33

44
import { FetchClient, type ProblemDetails } from '$api/FetchClient';
55
import type { User } from '$lib/models/api';
66
import { accessToken } from '$api/auth';
77

8-
export const queryKey: string = 'User';
8+
export const queryKeys = {
9+
all: ['User'] as const,
10+
id: (id: string | null) => [...queryKeys.all, id] as const,
11+
me: () => [...queryKeys.all, 'me'] as const
12+
};
913

1014
export function getMeQuery() {
15+
const queryClient = useQueryClient();
1116
return createQuery<User, ProblemDetails>(
1217
derived(accessToken, ($accessToken) => ({
1318
enabled: !!$accessToken,
14-
queryKey: [queryKey],
19+
queryClient,
20+
queryKey: queryKeys.me(),
1521
queryFn: async ({ signal }: { signal: AbortSignal }) => {
1622
const { getJSON } = new FetchClient();
1723
const response = await getJSON<User>('users/me', {
1824
signal
1925
});
2026

2127
if (response.ok) {
28+
queryClient.setQueryData(queryKeys.id(response.data!.id!), response.data);
2229
return response.data!;
2330
}
2431

0 commit comments

Comments
 (0)