|
1 | 1 | import { |
2 | 2 | createQueryKeys, |
3 | | - type inferQueryKeyStore, |
4 | 3 | mergeQueryKeys, |
5 | 4 | } from '@lukemorales/query-key-factory'; |
6 | 5 |
|
7 | | -// separate query keys for each service |
8 | 6 | import { cartKeys } from './carts/query-keys'; |
9 | | -import { client } from './common'; |
10 | | - |
11 | | -const getPostComments = async (id: string) => { |
12 | | - const { data } = await client.get(`posts/${id}/comments`); |
13 | | - return data; |
14 | | -}; |
15 | 7 |
|
16 | 8 | export const postKeys = createQueryKeys('posts', { |
17 | | - list: (filters) => ['posts', filters], |
18 | | - // list: null, // if we don't want to pass any filters |
19 | | - // detail: (id) => [id], // option one |
| 9 | + list: (filters) => [filters], |
20 | 10 | detail: (id) => ({ |
21 | | - queryKey: [id], // [post, detail, id] |
| 11 | + queryKey: [id], |
22 | 12 | contextQueries: { |
23 | | - // comments become part of the contextQueries query key [post, detail, id, comments] |
24 | 13 | comments: { |
25 | 14 | queryKey: null, |
26 | | - queryFn: () => getPostComments(id), |
27 | 15 | }, |
28 | 16 | }, |
29 | | - }), // option two |
30 | | - comments: (id) => [id], // [posts, id, comments] |
| 17 | + }), |
31 | 18 | }); |
32 | 19 |
|
33 | | -export type QueryKeys = inferQueryKeyStore<typeof queryFactory>; |
34 | 20 | export const queryFactory = mergeQueryKeys(postKeys, cartKeys); |
0 commit comments