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