@tanstack/solid-query 4.24.9 → 5.0.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/cjs/index.js +298 -0
- package/build/cjs/index.js.map +1 -0
- package/build/esm/index.js +283 -0
- package/build/esm/index.js.map +1 -0
- package/build/source/QueryClientProvider.jsx +21 -0
- package/build/source/__tests__/QueryClientProvider.test.jsx +120 -0
- package/build/{solid → source}/__tests__/createInfiniteQuery.test.jsx +228 -372
- package/build/{solid → source}/__tests__/createMutation.test.jsx +174 -165
- package/build/{solid → source}/__tests__/createQueries.test.jsx +86 -367
- package/build/{solid → source}/__tests__/createQuery.test.jsx +991 -943
- package/build/{solid → source}/__tests__/createQuery.types.test.jsx +35 -24
- package/build/{solid → source}/__tests__/suspense.test.jsx +177 -148
- package/build/{solid → source}/__tests__/transition.test.jsx +7 -4
- package/build/{solid → source}/__tests__/useIsFetching.test.jsx +68 -85
- package/build/{solid → source}/__tests__/useIsMutating.test.jsx +78 -93
- package/build/{solid → source}/__tests__/utils.jsx +3 -9
- package/build/source/createBaseQuery.js +147 -0
- package/build/source/createInfiniteQuery.js +6 -0
- package/build/{solid → source}/createMutation.js +7 -9
- package/build/source/createQueries.js +32 -0
- package/build/source/createQuery.js +6 -0
- package/build/{solid → source}/index.js +5 -3
- package/build/source/setBatchUpdatesFn.js +3 -0
- package/build/source/useIsFetching.js +12 -0
- package/build/source/useIsMutating.js +12 -0
- package/build/source/utils.js +7 -0
- package/build/types/QueryClientProvider.d.ts +9 -0
- package/build/{lib → types}/__tests__/utils.d.ts +3 -8
- package/build/types/createBaseQuery.d.ts +4 -0
- package/build/types/createInfiniteQuery.d.ts +3 -0
- package/build/types/createMutation.d.ts +3 -0
- package/build/{lib → types}/createQueries.d.ts +10 -8
- package/build/types/createQuery.d.ts +11 -0
- package/build/{lib → types}/index.d.ts +4 -3
- package/build/types/setBatchUpdatesFn.d.ts +1 -0
- package/build/types/types.d.ts +33 -0
- package/build/types/useIsFetching.d.ts +8 -0
- package/build/types/useIsMutating.d.ts +8 -0
- package/build/types/utils.d.ts +1 -0
- package/build/umd/index.js +2 -0
- package/build/umd/index.js.map +1 -0
- package/package.json +25 -17
- package/src/QueryClientProvider.tsx +17 -86
- package/src/__tests__/QueryClientProvider.test.tsx +37 -140
- package/src/__tests__/createInfiniteQuery.test.tsx +277 -508
- package/src/__tests__/createMutation.test.tsx +177 -225
- package/src/__tests__/createQueries.test.tsx +180 -528
- package/src/__tests__/createQuery.test.tsx +970 -1200
- package/src/__tests__/createQuery.types.test.tsx +30 -25
- package/src/__tests__/suspense.test.tsx +141 -178
- package/src/__tests__/transition.test.tsx +7 -4
- package/src/__tests__/useIsFetching.test.tsx +77 -122
- package/src/__tests__/useIsMutating.test.tsx +83 -128
- package/src/__tests__/utils.tsx +4 -11
- package/src/createBaseQuery.ts +148 -60
- package/src/createInfiniteQuery.ts +15 -94
- package/src/createMutation.ts +9 -63
- package/src/createQueries.ts +44 -55
- package/src/createQuery.ts +42 -127
- package/src/index.ts +6 -3
- package/src/setBatchUpdatesFn.ts +4 -0
- package/src/types.ts +81 -75
- package/src/useIsFetching.ts +12 -44
- package/src/useIsMutating.ts +13 -29
- package/src/utils.ts +5 -79
- package/build/lib/QueryClientProvider.d.ts +0 -24
- package/build/lib/QueryClientProvider.esm.js +0 -74
- package/build/lib/QueryClientProvider.esm.js.map +0 -1
- package/build/lib/QueryClientProvider.js +0 -80
- package/build/lib/QueryClientProvider.js.map +0 -1
- package/build/lib/QueryClientProvider.mjs +0 -74
- package/build/lib/QueryClientProvider.mjs.map +0 -1
- package/build/lib/createBaseQuery.d.ts +0 -4
- package/build/lib/createBaseQuery.esm.js +0 -93
- package/build/lib/createBaseQuery.esm.js.map +0 -1
- package/build/lib/createBaseQuery.js +0 -97
- package/build/lib/createBaseQuery.js.map +0 -1
- package/build/lib/createBaseQuery.mjs +0 -93
- package/build/lib/createBaseQuery.mjs.map +0 -1
- package/build/lib/createInfiniteQuery.d.ts +0 -5
- package/build/lib/createInfiniteQuery.esm.js +0 -20
- package/build/lib/createInfiniteQuery.esm.js.map +0 -1
- package/build/lib/createInfiniteQuery.js +0 -24
- package/build/lib/createInfiniteQuery.js.map +0 -1
- package/build/lib/createInfiniteQuery.mjs +0 -20
- package/build/lib/createInfiniteQuery.mjs.map +0 -1
- package/build/lib/createMutation.d.ts +0 -6
- package/build/lib/createMutation.esm.js +0 -45
- package/build/lib/createMutation.esm.js.map +0 -1
- package/build/lib/createMutation.js +0 -49
- package/build/lib/createMutation.js.map +0 -1
- package/build/lib/createMutation.mjs +0 -45
- package/build/lib/createMutation.mjs.map +0 -1
- package/build/lib/createQueries.esm.js +0 -54
- package/build/lib/createQueries.esm.js.map +0 -1
- package/build/lib/createQueries.js +0 -58
- package/build/lib/createQueries.js.map +0 -1
- package/build/lib/createQueries.mjs +0 -54
- package/build/lib/createQueries.mjs.map +0 -1
- package/build/lib/createQuery.d.ts +0 -23
- package/build/lib/createQuery.esm.js +0 -25
- package/build/lib/createQuery.esm.js.map +0 -1
- package/build/lib/createQuery.js +0 -29
- package/build/lib/createQuery.js.map +0 -1
- package/build/lib/createQuery.mjs +0 -25
- package/build/lib/createQuery.mjs.map +0 -1
- package/build/lib/index.esm.js +0 -9
- package/build/lib/index.esm.js.map +0 -1
- package/build/lib/index.js +0 -31
- package/build/lib/index.js.map +0 -1
- package/build/lib/index.mjs +0 -9
- package/build/lib/index.mjs.map +0 -1
- package/build/lib/types.d.ts +0 -47
- package/build/lib/useIsFetching.d.ts +0 -7
- package/build/lib/useIsFetching.esm.js +0 -29
- package/build/lib/useIsFetching.esm.js.map +0 -1
- package/build/lib/useIsFetching.js +0 -33
- package/build/lib/useIsFetching.js.map +0 -1
- package/build/lib/useIsFetching.mjs +0 -29
- package/build/lib/useIsFetching.mjs.map +0 -1
- package/build/lib/useIsMutating.d.ts +0 -8
- package/build/lib/useIsMutating.esm.js +0 -22
- package/build/lib/useIsMutating.esm.js.map +0 -1
- package/build/lib/useIsMutating.js +0 -26
- package/build/lib/useIsMutating.js.map +0 -1
- package/build/lib/useIsMutating.mjs +0 -22
- package/build/lib/useIsMutating.mjs.map +0 -1
- package/build/lib/utils.d.ts +0 -14
- package/build/lib/utils.esm.js +0 -63
- package/build/lib/utils.esm.js.map +0 -1
- package/build/lib/utils.js +0 -72
- package/build/lib/utils.js.map +0 -1
- package/build/lib/utils.mjs +0 -63
- package/build/lib/utils.mjs.map +0 -1
- package/build/solid/QueryClientProvider.jsx +0 -49
- package/build/solid/__tests__/QueryClientProvider.test.jsx +0 -185
- package/build/solid/createBaseQuery.js +0 -81
- package/build/solid/createInfiniteQuery.js +0 -16
- package/build/solid/createQueries.js +0 -39
- package/build/solid/createQuery.js +0 -16
- package/build/solid/useIsFetching.js +0 -23
- package/build/solid/useIsMutating.js +0 -16
- package/build/solid/utils.js +0 -45
- package/build/umd/index.development.js +0 -3572
- package/build/umd/index.development.js.map +0 -1
- package/build/umd/index.production.js +0 -2
- package/build/umd/index.production.js.map +0 -1
- /package/build/{solid → source}/types.js +0 -0
- /package/build/{lib → types}/__tests__/QueryClientProvider.test.d.ts +0 -0
- /package/build/{lib → types}/__tests__/createInfiniteQuery.test.d.ts +0 -0
- /package/build/{lib → types}/__tests__/createMutation.test.d.ts +0 -0
- /package/build/{lib → types}/__tests__/createQueries.test.d.ts +0 -0
- /package/build/{lib → types}/__tests__/createQuery.test.d.ts +0 -0
- /package/build/{lib → types}/__tests__/createQuery.types.test.d.ts +0 -0
- /package/build/{lib → types}/__tests__/suspense.test.d.ts +0 -0
- /package/build/{lib → types}/__tests__/transition.test.d.ts +0 -0
- /package/build/{lib → types}/__tests__/useIsFetching.test.d.ts +0 -0
- /package/build/{lib → types}/__tests__/useIsMutating.test.d.ts +0 -0
package/src/createBaseQuery.ts
CHANGED
|
@@ -1,16 +1,28 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unnecessary-condition */
|
|
2
|
+
// Had to disable the lint rule because isServer type is defined as false
|
|
3
|
+
// in solid-js/web package. I'll create a GitHub issue with them to see
|
|
4
|
+
// why that happens.
|
|
5
|
+
import type {
|
|
6
|
+
QueryClient,
|
|
7
|
+
QueryKey,
|
|
8
|
+
QueryObserver,
|
|
9
|
+
QueryObserverResult,
|
|
10
|
+
} from '@tanstack/query-core'
|
|
11
|
+
import { hydrate } from '@tanstack/query-core'
|
|
12
|
+
import { notifyManager } from '@tanstack/query-core'
|
|
13
|
+
import type { Accessor } from 'solid-js'
|
|
14
|
+
import { isServer } from 'solid-js/web'
|
|
5
15
|
import {
|
|
6
|
-
onMount,
|
|
7
|
-
onCleanup,
|
|
8
16
|
createComputed,
|
|
17
|
+
createMemo,
|
|
9
18
|
createResource,
|
|
10
19
|
on,
|
|
11
|
-
|
|
20
|
+
onCleanup,
|
|
21
|
+
onMount,
|
|
12
22
|
} from 'solid-js'
|
|
13
23
|
import { createStore, unwrap } from 'solid-js/store'
|
|
24
|
+
import { useQueryClient } from './QueryClientProvider'
|
|
25
|
+
import type { CreateBaseQueryOptions } from './types'
|
|
14
26
|
import { shouldThrowError } from './utils'
|
|
15
27
|
|
|
16
28
|
// Base Query Function that is used to create the query.
|
|
@@ -21,81 +33,157 @@ export function createBaseQuery<
|
|
|
21
33
|
TQueryData,
|
|
22
34
|
TQueryKey extends QueryKey,
|
|
23
35
|
>(
|
|
24
|
-
options:
|
|
25
|
-
TQueryFnData,
|
|
26
|
-
TError,
|
|
27
|
-
TData,
|
|
28
|
-
TQueryData,
|
|
29
|
-
TQueryKey
|
|
36
|
+
options: Accessor<
|
|
37
|
+
CreateBaseQueryOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>
|
|
30
38
|
>,
|
|
31
39
|
Observer: typeof QueryObserver,
|
|
32
|
-
)
|
|
33
|
-
|
|
34
|
-
const
|
|
35
|
-
|
|
40
|
+
queryClient?: () => QueryClient,
|
|
41
|
+
) {
|
|
42
|
+
const client = createMemo(() => useQueryClient(queryClient?.()))
|
|
43
|
+
|
|
44
|
+
const defaultedOptions = client().defaultQueryOptions(options())
|
|
36
45
|
defaultedOptions._optimisticResults = 'optimistic'
|
|
37
|
-
|
|
46
|
+
if (isServer) {
|
|
47
|
+
defaultedOptions.retry = false
|
|
48
|
+
defaultedOptions.throwErrors = true
|
|
49
|
+
}
|
|
50
|
+
const observer = new Observer(client(), defaultedOptions)
|
|
38
51
|
|
|
39
52
|
const [state, setState] = createStore<QueryObserverResult<TData, TError>>(
|
|
40
|
-
// @ts-ignore
|
|
41
53
|
observer.getOptimisticResult(defaultedOptions),
|
|
42
54
|
)
|
|
43
55
|
|
|
44
|
-
const
|
|
56
|
+
const createServerSubscriber = (
|
|
57
|
+
resolve: (
|
|
58
|
+
data:
|
|
59
|
+
| QueryObserverResult<TData, TError>
|
|
60
|
+
| PromiseLike<QueryObserverResult<TData, TError> | undefined>
|
|
61
|
+
| undefined,
|
|
62
|
+
) => void,
|
|
63
|
+
reject: (reason?: any) => void,
|
|
64
|
+
) => {
|
|
65
|
+
return observer.subscribe((result) => {
|
|
66
|
+
notifyManager.batchCalls(() => {
|
|
67
|
+
const unwrappedResult = { ...unwrap(result) }
|
|
68
|
+
if (unwrappedResult.isError) {
|
|
69
|
+
if (process.env['NODE_ENV'] === 'development') {
|
|
70
|
+
console.error(unwrappedResult.error)
|
|
71
|
+
}
|
|
72
|
+
reject(unwrappedResult.error)
|
|
73
|
+
}
|
|
74
|
+
if (unwrappedResult.isSuccess) {
|
|
75
|
+
resolve(unwrappedResult)
|
|
76
|
+
}
|
|
77
|
+
})()
|
|
78
|
+
})
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const createClientSubscriber = () => {
|
|
82
|
+
return observer.subscribe((result) => {
|
|
83
|
+
notifyManager.batchCalls(() => {
|
|
84
|
+
const unwrappedResult = { ...unwrap(result) }
|
|
85
|
+
// If the query has data we dont suspend but instead mutate the resource
|
|
86
|
+
// This could happen when placeholderData/initialData is defined
|
|
87
|
+
if (
|
|
88
|
+
queryResource()?.data &&
|
|
89
|
+
unwrappedResult.data &&
|
|
90
|
+
!queryResource.loading
|
|
91
|
+
) {
|
|
92
|
+
setState(unwrappedResult)
|
|
93
|
+
mutate(state)
|
|
94
|
+
} else {
|
|
95
|
+
setState(unwrappedResult)
|
|
96
|
+
refetch()
|
|
97
|
+
}
|
|
98
|
+
})()
|
|
99
|
+
})
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Unsubscribe is set lazily, so that we can subscribe after hydration when needed.
|
|
104
|
+
*/
|
|
105
|
+
let unsubscribe: (() => void) | null = null
|
|
106
|
+
|
|
107
|
+
const [queryResource, { refetch, mutate }] = createResource<
|
|
108
|
+
QueryObserverResult<TData, TError> | undefined
|
|
109
|
+
>(
|
|
45
110
|
() => {
|
|
46
|
-
return new Promise((resolve) => {
|
|
47
|
-
if (
|
|
48
|
-
|
|
49
|
-
|
|
111
|
+
return new Promise((resolve, reject) => {
|
|
112
|
+
if (isServer) {
|
|
113
|
+
unsubscribe = createServerSubscriber(resolve, reject)
|
|
114
|
+
} else {
|
|
115
|
+
if (!unsubscribe) {
|
|
116
|
+
unsubscribe = createClientSubscriber()
|
|
50
117
|
}
|
|
51
|
-
|
|
118
|
+
}
|
|
119
|
+
if (!state.isLoading) {
|
|
120
|
+
resolve(state)
|
|
52
121
|
}
|
|
53
122
|
})
|
|
54
123
|
},
|
|
55
|
-
|
|
124
|
+
{
|
|
125
|
+
initialValue: state,
|
|
56
126
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
refetch()
|
|
60
|
-
})
|
|
127
|
+
// If initialData is provided, we resolve the resource immediately
|
|
128
|
+
ssrLoadFrom: options().initialData ? 'initial' : 'server',
|
|
61
129
|
|
|
62
|
-
|
|
130
|
+
get deferStream() {
|
|
131
|
+
return options().deferStream
|
|
132
|
+
},
|
|
63
133
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
134
|
+
/**
|
|
135
|
+
* If this resource was populated on the server (either sync render, or streamed in over time), onHydrated
|
|
136
|
+
* will be called. This is the point at which we can hydrate the query cache state, and setup the query subscriber.
|
|
137
|
+
*
|
|
138
|
+
* Leveraging onHydrated allows us to plug into the async and streaming support that solidjs resources already support.
|
|
139
|
+
*
|
|
140
|
+
* Note that this is only invoked on the client, for queries that were originally run on the server.
|
|
141
|
+
*/
|
|
142
|
+
onHydrated(_k, info) {
|
|
143
|
+
if (info.value) {
|
|
144
|
+
hydrate(client(), {
|
|
145
|
+
queries: [
|
|
146
|
+
{
|
|
147
|
+
queryKey: defaultedOptions.queryKey,
|
|
148
|
+
queryHash: defaultedOptions.queryHash,
|
|
149
|
+
state: info.value,
|
|
150
|
+
},
|
|
151
|
+
],
|
|
152
|
+
})
|
|
74
153
|
}
|
|
75
|
-
setState(unwrap(unwrappedResult))
|
|
76
|
-
mutate(() => unwrap(result.data))
|
|
77
|
-
refetch()
|
|
78
|
-
})
|
|
79
|
-
})
|
|
80
154
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
155
|
+
if (!unsubscribe) {
|
|
156
|
+
/**
|
|
157
|
+
* Do not refetch query on mount if query was fetched on server,
|
|
158
|
+
* even if `staleTime` is not set.
|
|
159
|
+
*/
|
|
160
|
+
const newOptions = { ...defaultedOptions }
|
|
161
|
+
if (defaultedOptions.staleTime || !defaultedOptions.initialData) {
|
|
162
|
+
newOptions.refetchOnMount = false
|
|
163
|
+
}
|
|
164
|
+
// Setting the options as an immutable object to prevent
|
|
165
|
+
// wonky behavior with observer subscriptions
|
|
166
|
+
observer.setOptions(newOptions)
|
|
167
|
+
setState(observer.getOptimisticResult(newOptions))
|
|
168
|
+
unsubscribe = createClientSubscriber()
|
|
169
|
+
}
|
|
170
|
+
},
|
|
171
|
+
},
|
|
172
|
+
)
|
|
89
173
|
|
|
90
|
-
onCleanup(() =>
|
|
174
|
+
onCleanup(() => {
|
|
175
|
+
if (unsubscribe) {
|
|
176
|
+
unsubscribe()
|
|
177
|
+
unsubscribe = null
|
|
178
|
+
}
|
|
179
|
+
})
|
|
91
180
|
|
|
92
181
|
onMount(() => {
|
|
93
182
|
observer.setOptions(defaultedOptions, { listeners: false })
|
|
94
183
|
})
|
|
95
184
|
|
|
96
185
|
createComputed(() => {
|
|
97
|
-
|
|
98
|
-
observer.setOptions(newDefaultedOptions)
|
|
186
|
+
observer.setOptions(client().defaultQueryOptions(options()))
|
|
99
187
|
})
|
|
100
188
|
|
|
101
189
|
createComputed(
|
|
@@ -105,7 +193,7 @@ export function createBaseQuery<
|
|
|
105
193
|
if (
|
|
106
194
|
state.isError &&
|
|
107
195
|
!state.isFetching &&
|
|
108
|
-
shouldThrowError(observer.options.
|
|
196
|
+
shouldThrowError(observer.options.throwErrors, [
|
|
109
197
|
state.error,
|
|
110
198
|
observer.getCurrentQuery(),
|
|
111
199
|
])
|
|
@@ -122,11 +210,11 @@ export function createBaseQuery<
|
|
|
122
210
|
prop: keyof QueryObserverResult<TData, TError>,
|
|
123
211
|
): any {
|
|
124
212
|
if (prop === 'data') {
|
|
125
|
-
return
|
|
213
|
+
return queryResource()?.data
|
|
126
214
|
}
|
|
127
215
|
return Reflect.get(target, prop)
|
|
128
216
|
},
|
|
129
217
|
}
|
|
130
218
|
|
|
131
|
-
return new Proxy(state, handler)
|
|
219
|
+
return new Proxy(state, handler)
|
|
132
220
|
}
|
|
@@ -1,116 +1,37 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
QueryObserver,
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
QueryKey,
|
|
4
|
+
QueryClient,
|
|
5
|
+
RegisteredError,
|
|
6
|
+
InfiniteData,
|
|
5
7
|
} from '@tanstack/query-core'
|
|
6
8
|
import { InfiniteQueryObserver } from '@tanstack/query-core'
|
|
7
9
|
import type {
|
|
8
10
|
CreateInfiniteQueryOptions,
|
|
9
11
|
CreateInfiniteQueryResult,
|
|
10
|
-
SolidQueryKey,
|
|
11
12
|
} from './types'
|
|
12
13
|
import { createBaseQuery } from './createBaseQuery'
|
|
13
|
-
import {
|
|
14
|
-
import { createStore } from 'solid-js/store'
|
|
15
|
-
import { parseQueryArgs } from './utils'
|
|
14
|
+
import { createMemo } from 'solid-js'
|
|
16
15
|
|
|
17
|
-
export function createInfiniteQuery<
|
|
18
|
-
TQueryFnData = unknown,
|
|
19
|
-
TError = unknown,
|
|
20
|
-
TData = TQueryFnData,
|
|
21
|
-
TQueryKey extends SolidQueryKey = SolidQueryKey,
|
|
22
|
-
>(
|
|
23
|
-
options: CreateInfiniteQueryOptions<
|
|
24
|
-
TQueryFnData,
|
|
25
|
-
TError,
|
|
26
|
-
TData,
|
|
27
|
-
TQueryFnData,
|
|
28
|
-
TQueryKey
|
|
29
|
-
>,
|
|
30
|
-
): CreateInfiniteQueryResult<TData, TError>
|
|
31
|
-
export function createInfiniteQuery<
|
|
32
|
-
TQueryFnData = unknown,
|
|
33
|
-
TError = unknown,
|
|
34
|
-
TData = TQueryFnData,
|
|
35
|
-
TQueryKey extends SolidQueryKey = SolidQueryKey,
|
|
36
|
-
>(
|
|
37
|
-
queryKey: TQueryKey,
|
|
38
|
-
options?: Omit<
|
|
39
|
-
CreateInfiniteQueryOptions<
|
|
40
|
-
TQueryFnData,
|
|
41
|
-
TError,
|
|
42
|
-
TData,
|
|
43
|
-
TQueryFnData,
|
|
44
|
-
TQueryKey
|
|
45
|
-
>,
|
|
46
|
-
'queryKey'
|
|
47
|
-
>,
|
|
48
|
-
): CreateInfiniteQueryResult<TData, TError>
|
|
49
|
-
export function createInfiniteQuery<
|
|
50
|
-
TQueryFnData = unknown,
|
|
51
|
-
TError = unknown,
|
|
52
|
-
TData = TQueryFnData,
|
|
53
|
-
TQueryKey extends SolidQueryKey = SolidQueryKey,
|
|
54
|
-
>(
|
|
55
|
-
queryKey: TQueryKey,
|
|
56
|
-
queryFn: QueryFunction<TQueryFnData, ReturnType<TQueryKey>>,
|
|
57
|
-
options?: Omit<
|
|
58
|
-
CreateInfiniteQueryOptions<
|
|
59
|
-
TQueryFnData,
|
|
60
|
-
TError,
|
|
61
|
-
TData,
|
|
62
|
-
TQueryFnData,
|
|
63
|
-
TQueryKey
|
|
64
|
-
>,
|
|
65
|
-
'queryKey' | 'queryFn'
|
|
66
|
-
>,
|
|
67
|
-
): CreateInfiniteQueryResult<TData, TError>
|
|
68
16
|
export function createInfiniteQuery<
|
|
69
17
|
TQueryFnData,
|
|
70
|
-
TError,
|
|
71
|
-
TData = TQueryFnData
|
|
72
|
-
TQueryKey extends
|
|
18
|
+
TError = RegisteredError,
|
|
19
|
+
TData = InfiniteData<TQueryFnData>,
|
|
20
|
+
TQueryKey extends QueryKey = QueryKey,
|
|
21
|
+
TPageParam = unknown,
|
|
73
22
|
>(
|
|
74
|
-
|
|
75
|
-
| TQueryKey
|
|
76
|
-
| CreateInfiniteQueryOptions<
|
|
77
|
-
TQueryFnData,
|
|
78
|
-
TError,
|
|
79
|
-
TData,
|
|
80
|
-
TQueryFnData,
|
|
81
|
-
TQueryKey
|
|
82
|
-
>,
|
|
83
|
-
arg2?:
|
|
84
|
-
| QueryFunction<TQueryFnData, ReturnType<TQueryKey>>
|
|
85
|
-
| CreateInfiniteQueryOptions<
|
|
86
|
-
TQueryFnData,
|
|
87
|
-
TError,
|
|
88
|
-
TData,
|
|
89
|
-
TQueryFnData,
|
|
90
|
-
TQueryKey
|
|
91
|
-
>,
|
|
92
|
-
arg3?: CreateInfiniteQueryOptions<
|
|
23
|
+
options: CreateInfiniteQueryOptions<
|
|
93
24
|
TQueryFnData,
|
|
94
25
|
TError,
|
|
95
26
|
TData,
|
|
96
|
-
|
|
97
|
-
|
|
27
|
+
TQueryKey,
|
|
28
|
+
TPageParam
|
|
98
29
|
>,
|
|
30
|
+
queryClient?: () => QueryClient,
|
|
99
31
|
): CreateInfiniteQueryResult<TData, TError> {
|
|
100
|
-
// The parseQuery Args functions helps normalize the arguments into the correct form.
|
|
101
|
-
// Whatever the parameters are, they are normalized into the correct form.
|
|
102
|
-
const [parsedOptions, setParsedOptions] = createStore(
|
|
103
|
-
parseQueryArgs(arg1, arg2, arg3),
|
|
104
|
-
)
|
|
105
|
-
|
|
106
|
-
// Watch for changes in the options and update the parsed options.
|
|
107
|
-
createComputed(() => {
|
|
108
|
-
const newParsedOptions = parseQueryArgs(arg1, arg2, arg3)
|
|
109
|
-
setParsedOptions(newParsedOptions)
|
|
110
|
-
})
|
|
111
|
-
|
|
112
32
|
return createBaseQuery(
|
|
113
|
-
|
|
33
|
+
createMemo(() => options()),
|
|
114
34
|
InfiniteQueryObserver as typeof QueryObserver,
|
|
35
|
+
queryClient,
|
|
115
36
|
) as CreateInfiniteQueryResult<TData, TError>
|
|
116
37
|
}
|
package/src/createMutation.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import {
|
|
1
|
+
import type { QueryClient, RegisteredError } from '@tanstack/query-core'
|
|
2
|
+
import { MutationObserver } from '@tanstack/query-core'
|
|
3
3
|
import { useQueryClient } from './QueryClientProvider'
|
|
4
4
|
import type {
|
|
5
5
|
CreateMutateFunction,
|
|
@@ -13,70 +13,18 @@ import { shouldThrowError } from './utils'
|
|
|
13
13
|
// HOOK
|
|
14
14
|
export function createMutation<
|
|
15
15
|
TData = unknown,
|
|
16
|
-
TError =
|
|
16
|
+
TError = RegisteredError,
|
|
17
17
|
TVariables = void,
|
|
18
18
|
TContext = unknown,
|
|
19
19
|
>(
|
|
20
20
|
options: CreateMutationOptions<TData, TError, TVariables, TContext>,
|
|
21
|
-
)
|
|
22
|
-
export function createMutation<
|
|
23
|
-
TData = unknown,
|
|
24
|
-
TError = unknown,
|
|
25
|
-
TVariables = void,
|
|
26
|
-
TContext = unknown,
|
|
27
|
-
>(
|
|
28
|
-
mutationFn: MutationFunction<TData, TVariables>,
|
|
29
|
-
options?: Omit<
|
|
30
|
-
CreateMutationOptions<TData, TError, TVariables, TContext>,
|
|
31
|
-
'mutationFn'
|
|
32
|
-
>,
|
|
33
|
-
): CreateMutationResult<TData, TError, TVariables, TContext>
|
|
34
|
-
export function createMutation<
|
|
35
|
-
TData = unknown,
|
|
36
|
-
TError = unknown,
|
|
37
|
-
TVariables = void,
|
|
38
|
-
TContext = unknown,
|
|
39
|
-
>(
|
|
40
|
-
mutationKey: MutationKey,
|
|
41
|
-
options?: Omit<
|
|
42
|
-
CreateMutationOptions<TData, TError, TVariables, TContext>,
|
|
43
|
-
'mutationKey'
|
|
44
|
-
>,
|
|
45
|
-
): CreateMutationResult<TData, TError, TVariables, TContext>
|
|
46
|
-
export function createMutation<
|
|
47
|
-
TData = unknown,
|
|
48
|
-
TError = unknown,
|
|
49
|
-
TVariables = void,
|
|
50
|
-
TContext = unknown,
|
|
51
|
-
>(
|
|
52
|
-
mutationKey: MutationKey,
|
|
53
|
-
mutationFn?: MutationFunction<TData, TVariables>,
|
|
54
|
-
options?: Omit<
|
|
55
|
-
CreateMutationOptions<TData, TError, TVariables, TContext>,
|
|
56
|
-
'mutationKey' | 'mutationFn'
|
|
57
|
-
>,
|
|
58
|
-
): CreateMutationResult<TData, TError, TVariables, TContext>
|
|
59
|
-
export function createMutation<
|
|
60
|
-
TData = unknown,
|
|
61
|
-
TError = unknown,
|
|
62
|
-
TVariables = void,
|
|
63
|
-
TContext = unknown,
|
|
64
|
-
>(
|
|
65
|
-
arg1:
|
|
66
|
-
| MutationKey
|
|
67
|
-
| MutationFunction<TData, TVariables>
|
|
68
|
-
| CreateMutationOptions<TData, TError, TVariables, TContext>,
|
|
69
|
-
arg2?:
|
|
70
|
-
| MutationFunction<TData, TVariables>
|
|
71
|
-
| CreateMutationOptions<TData, TError, TVariables, TContext>,
|
|
72
|
-
arg3?: CreateMutationOptions<TData, TError, TVariables, TContext>,
|
|
21
|
+
queryClient?: () => QueryClient,
|
|
73
22
|
): CreateMutationResult<TData, TError, TVariables, TContext> {
|
|
74
|
-
const
|
|
75
|
-
const queryClient = useQueryClient({ context: options.context })
|
|
23
|
+
const client = useQueryClient(queryClient?.())
|
|
76
24
|
|
|
77
25
|
const observer = new MutationObserver<TData, TError, TVariables, TContext>(
|
|
78
|
-
|
|
79
|
-
options,
|
|
26
|
+
client,
|
|
27
|
+
options(),
|
|
80
28
|
)
|
|
81
29
|
|
|
82
30
|
const mutate: CreateMutateFunction<TData, TError, TVariables, TContext> = (
|
|
@@ -95,9 +43,7 @@ export function createMutation<
|
|
|
95
43
|
})
|
|
96
44
|
|
|
97
45
|
createComputed(() => {
|
|
98
|
-
|
|
99
|
-
setOptions(newParsedOptions)
|
|
100
|
-
observer.setOptions(newParsedOptions)
|
|
46
|
+
observer.setOptions(options())
|
|
101
47
|
})
|
|
102
48
|
|
|
103
49
|
createComputed(
|
|
@@ -106,7 +52,7 @@ export function createMutation<
|
|
|
106
52
|
() => {
|
|
107
53
|
if (
|
|
108
54
|
state.isError &&
|
|
109
|
-
shouldThrowError(observer.options.
|
|
55
|
+
shouldThrowError(observer.options.throwErrors, [state.error])
|
|
110
56
|
) {
|
|
111
57
|
throw state.error
|
|
112
58
|
}
|
package/src/createQueries.ts
CHANGED
|
@@ -1,23 +1,29 @@
|
|
|
1
|
-
import { createComputed, onCleanup, onMount } from 'solid-js'
|
|
2
|
-
import type { QueryFunction } from '@tanstack/query-core'
|
|
3
|
-
import { QueriesObserver } from '@tanstack/query-core'
|
|
4
|
-
import { useQueryClient } from './QueryClientProvider'
|
|
5
1
|
import type {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
2
|
+
QueriesPlaceholderDataFunction,
|
|
3
|
+
QueryClient,
|
|
4
|
+
QueryFunction,
|
|
5
|
+
QueryKey,
|
|
6
|
+
RegisteredError,
|
|
7
|
+
} from '@tanstack/query-core'
|
|
8
|
+
import { notifyManager, QueriesObserver } from '@tanstack/query-core'
|
|
9
|
+
import { createComputed, onCleanup, onMount } from 'solid-js'
|
|
10
10
|
import { createStore, unwrap } from 'solid-js/store'
|
|
11
|
-
import {
|
|
11
|
+
import { useQueryClient } from './QueryClientProvider'
|
|
12
|
+
import type { CreateQueryResult, SolidQueryOptions } from './types'
|
|
12
13
|
|
|
13
14
|
// This defines the `UseQueryOptions` that are accepted in `QueriesOptions` & `GetOptions`.
|
|
14
|
-
//
|
|
15
|
+
// `placeholderData` function does not have a parameter
|
|
15
16
|
type CreateQueryOptionsForCreateQueries<
|
|
16
17
|
TQueryFnData = unknown,
|
|
17
|
-
TError =
|
|
18
|
+
TError = RegisteredError,
|
|
18
19
|
TData = TQueryFnData,
|
|
19
|
-
TQueryKey extends
|
|
20
|
-
> = Omit<
|
|
20
|
+
TQueryKey extends QueryKey = QueryKey,
|
|
21
|
+
> = Omit<
|
|
22
|
+
SolidQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
|
|
23
|
+
'placeholderData'
|
|
24
|
+
> & {
|
|
25
|
+
placeholderData?: TQueryFnData | QueriesPlaceholderDataFunction<TQueryFnData>
|
|
26
|
+
}
|
|
21
27
|
|
|
22
28
|
// Avoid TS depth-limit error in case of large array literal
|
|
23
29
|
type MAXIMUM_DEPTH = 20
|
|
@@ -46,18 +52,13 @@ type GetOptions<T> =
|
|
|
46
52
|
queryFn?: QueryFunction<infer TQueryFnData, infer TQueryKey>
|
|
47
53
|
select: (data: any) => infer TData
|
|
48
54
|
}
|
|
49
|
-
? CreateQueryOptionsForCreateQueries<
|
|
50
|
-
TQueryFnData,
|
|
51
|
-
unknown,
|
|
52
|
-
TData,
|
|
53
|
-
() => TQueryKey
|
|
54
|
-
>
|
|
55
|
+
? CreateQueryOptionsForCreateQueries<TQueryFnData, Error, TData, TQueryKey>
|
|
55
56
|
: T extends { queryFn?: QueryFunction<infer TQueryFnData, infer TQueryKey> }
|
|
56
57
|
? CreateQueryOptionsForCreateQueries<
|
|
57
58
|
TQueryFnData,
|
|
58
|
-
|
|
59
|
+
Error,
|
|
59
60
|
TQueryFnData,
|
|
60
|
-
|
|
61
|
+
TQueryKey
|
|
61
62
|
>
|
|
62
63
|
: // Fallback
|
|
63
64
|
CreateQueryOptionsForCreateQueries
|
|
@@ -139,30 +140,26 @@ export type QueriesResults<
|
|
|
139
140
|
any
|
|
140
141
|
>[]
|
|
141
142
|
? // Dynamic-size (homogenous) UseQueryOptions array: map directly to array of results
|
|
142
|
-
CreateQueryResult<
|
|
143
|
+
CreateQueryResult<
|
|
144
|
+
unknown extends TData ? TQueryFnData : TData,
|
|
145
|
+
unknown extends TError ? RegisteredError : TError
|
|
146
|
+
>[]
|
|
143
147
|
: // Fallback
|
|
144
148
|
CreateQueryResult[]
|
|
145
149
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
const queryClient = useQueryClient(
|
|
150
|
+
export function createQueries<T extends any[]>(
|
|
151
|
+
queriesOptions: () => {
|
|
152
|
+
queries: readonly [...QueriesOptions<T>]
|
|
153
|
+
queryClient?: QueryClient
|
|
154
|
+
},
|
|
155
|
+
): QueriesResults<T> {
|
|
156
|
+
const queryClient = useQueryClient(queriesOptions().queryClient)
|
|
153
157
|
|
|
154
|
-
const
|
|
155
|
-
|
|
156
|
-
) => {
|
|
157
|
-
const normalizedOptions = { ...options, queryKey: options.queryKey?.() }
|
|
158
|
-
const defaultedOptions = queryClient.defaultQueryOptions(normalizedOptions)
|
|
158
|
+
const defaultedQueries = queriesOptions().queries.map((options) => {
|
|
159
|
+
const defaultedOptions = queryClient.defaultQueryOptions(options)
|
|
159
160
|
defaultedOptions._optimisticResults = 'optimistic'
|
|
160
161
|
return defaultedOptions
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
const defaultedQueries = queriesOptions.queries.map((options) =>
|
|
164
|
-
normalizeOptions(options),
|
|
165
|
-
)
|
|
162
|
+
})
|
|
166
163
|
|
|
167
164
|
const observer = new QueriesObserver(queryClient, defaultedQueries)
|
|
168
165
|
|
|
@@ -170,20 +167,10 @@ export function createQueries<T extends any[]>(queriesOptions: {
|
|
|
170
167
|
observer.getOptimisticResult(defaultedQueries),
|
|
171
168
|
)
|
|
172
169
|
|
|
173
|
-
const taskQueue: Array<() => void> = []
|
|
174
|
-
|
|
175
170
|
const unsubscribe = observer.subscribe((result) => {
|
|
176
|
-
|
|
171
|
+
notifyManager.batchCalls(() => {
|
|
177
172
|
setState(unwrap(result))
|
|
178
|
-
})
|
|
179
|
-
|
|
180
|
-
scheduleMicrotask(() => {
|
|
181
|
-
const taskToRun = taskQueue.pop()
|
|
182
|
-
if (taskToRun) {
|
|
183
|
-
taskToRun()
|
|
184
|
-
taskQueue.splice(0, taskQueue.length)
|
|
185
|
-
}
|
|
186
|
-
})
|
|
173
|
+
})()
|
|
187
174
|
})
|
|
188
175
|
|
|
189
176
|
onCleanup(unsubscribe)
|
|
@@ -193,10 +180,12 @@ export function createQueries<T extends any[]>(queriesOptions: {
|
|
|
193
180
|
})
|
|
194
181
|
|
|
195
182
|
createComputed(() => {
|
|
196
|
-
const
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
183
|
+
const updatedQueries = queriesOptions().queries.map((options) => {
|
|
184
|
+
const defaultedOptions = queryClient.defaultQueryOptions(options)
|
|
185
|
+
defaultedOptions._optimisticResults = 'optimistic'
|
|
186
|
+
return defaultedOptions
|
|
187
|
+
})
|
|
188
|
+
observer.setQueries(updatedQueries)
|
|
200
189
|
})
|
|
201
190
|
|
|
202
191
|
return state as QueriesResults<T>
|