MCPcopy Create free account
hub / github.com/TanStack/query / useBaseQuery

Function useBaseQuery

packages/solid-query/src/useBaseQuery.ts:124–419  ·  view source on GitHub ↗
(
  options: Accessor<
    UseBaseQueryOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>
  >,
  Observer: typeof QueryObserver,
  queryClient?: Accessor<QueryClient>,
)

Source from the content-addressed store, hash-verified

122 * @returns The reactive query result.
123 */
124export function useBaseQuery<
125 TQueryFnData,
126 TError,
127 TData,
128 TQueryData,
129 TQueryKey extends QueryKey,
130>(
131 options: Accessor<
132 UseBaseQueryOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>
133 >,
134 Observer: typeof QueryObserver,
135 queryClient?: Accessor<QueryClient>,
136) {
137 type ResourceData = QueryObserverResult<TData, TError>
138
139 const resolveClient = useQueryClientResolver(queryClient)
140 const client = createMemo(() => resolveClient())
141 const isRestoring = useIsRestoring()
142 // There are times when we run a query on the server but the resource is never read
143 // This could lead to times when the queryObserver is unsubscribed before the resource has loaded
144 // Causing a time out error. To prevent this we will queue the unsubscribe if the cleanup is called
145 // before the resource has loaded
146 let unsubscribeQueued = false
147
148 const defaultedOptions = createMemo(() => {
149 const defaultOptions = client().defaultQueryOptions(options())
150 defaultOptions._optimisticResults = isRestoring()
151 ? 'isRestoring'
152 : 'optimistic'
153 defaultOptions.structuralSharing = false
154 if (isServer) {
155 defaultOptions.retry = false
156 defaultOptions.throwOnError = true
157 }
158 return defaultOptions
159 })
160 const initialOptions = defaultedOptions()
161
162 const [observer, setObserver] = createSignal(
163 new Observer(client(), defaultedOptions()),
164 )
165
166 let observerResult = observer().getOptimisticResult(defaultedOptions())
167 const [state, setState] =
168 createStore<QueryObserverResult<TData, TError>>(observerResult)
169
170 const createServerSubscriber = (
171 resolve: (
172 data: ResourceData | PromiseLike<ResourceData | undefined> | undefined,
173 ) => void,
174 reject: (reason?: any) => void,
175 ) => {
176 return observer().subscribe((result) => {
177 notifyManager.batchCalls(() => {
178 const query = observer().getCurrentQuery()
179 const unwrappedResult = hydratableObserverResult(query, result)
180
181 if (result.data !== undefined && unwrappedResult.isError) {

Callers 2

useQueryFunction · 0.90
useInfiniteQueryFunction · 0.90

Calls 15

useQueryClientResolverFunction · 0.90
useIsRestoringFunction · 0.90
shouldThrowErrorFunction · 0.90
clientFunction · 0.85
createServerSubscriberFunction · 0.85
createClientSubscriberFunction · 0.85
rejectFunction · 0.85
resolveFunction · 0.85
hydratableObserverResultFunction · 0.85
refetchFunction · 0.85
defaultQueryOptionsMethod · 0.80

Tested by

no test coverage detected