Open History to see the last commit
1import { queryOptions } from '@tanstack/react-query'
2β
3export function todosOptions(userId: string) {
4 const api = useApiClient()
5 return queryOptions({
6 // β
passes: 'api' is in allowlist.variables
7 queryKey: ['todos', userId],
8 queryFn: () => api.fetchTodos(userId),
9 })
10}
11β
12export function todosByApiOptions(userId: string) {
13 const todoApi = useApiClient()
14 // β fails: 'api' is in allowlist.variables, but this variable is named 'todoApi'
15 // eslint-disable-next-line @tanstack/query/exhaustive-deps -- The following dependencies are missing in your queryKey: todoApi
16 return queryOptions({
17 queryKey: ['todos', userId],
18 queryFn: () => todoApi.fetchTodos(userId),
19 })
20}
21β
22export function todosWithTrackingOptions(
23 tracker: AnalyticsClient,
24 userId: string,
25) {
26 return queryOptions({
27 // β
passes: AnalyticsClient is in allowlist.types
28 queryKey: ['todos', userId],
29 queryFn: async () => {
30 tracker.track('todos')
31 return fetch(`/api/todos?userId=${userId}`).then((r) => r.json())
32 },
33 })
34}
35β
36export function todosWithClientOptions(client: ApiClient, userId: string) {
37 // β fails: AnalyticsClient is in allowlist.types, but this param is typed as ApiClient
38 // eslint-disable-next-line @tanstack/query/exhaustive-deps -- The following dependencies are missing in your queryKey: client
39 return queryOptions({
40 queryKey: ['todos', userId],
41 queryFn: () => client.fetchTodos(userId),
42 })
43}
44β
45interface ApiClient {
46 fetchTodos: (userId: string) => Promise<Array<{ id: string }>>
47}
48β
49interface AnalyticsClient {
50 track: (event: string) => Promise<void>
51}
52β
53function useApiClient(): ApiClient {
54 throw new Error('not implemented')
55}