MCPcopy Create free account
hub / github.com/effect-app/libs / useQuery

Function useQuery

packages/vue/src/internal/tanstackQuery.ts:149–286  ·  view source on GitHub ↗
(
    q: RequestHandlerWithInput<I, A, E, R, Request, Name>
  )

Source from the content-addressed store, hash-verified

147 }
148 queryClient.setQueryData(queryKey, (data: A | undefined) => data === undefined ? data : updater(data))
149 }
150})
151
152export const makeTanstackQuery = <R>(
153 getRuntime: () => Context.Context<R>,
154 queryClient: QueryClient
155): MakeQuery2<R> => {
156 // Drop a query's recorded read-dependencies when tanstack evicts it from the cache, so the
157 // registry mirrors the live queries (mirrors the atom engine's `trackReadDependencies` finalizer).
158 queryClient.getQueryCache().subscribe((event) => {
159 if (event.type === "removed") clearQueryReadDependencies(event.query.queryKey)
160 })
161
162 const useQuery: MakeQuery2<R> = <I, A, E, Request extends Req, Name extends string>(
163 q: RequestHandlerWithInput<I, A, E, R, Request, Name>
164 ) =>
165 <TData = A>(
166 arg: I | WatchSource<I> | undefined | WatchSource<Option.Option<I>>,
167 options?: LegacyTanstackOptions<A, CauseException<E>, TData>
168 ) => {
169 const runPromise = makeRunPromise(getRuntime())
170 const queryKey = makeQueryKey(q)
171 const enabled = resolveEnabled(arg, options)
172 const structuralSharing = options?.structuralSharing === false ? false : replaceEqualDeep
173 const tanstackOptions = {
174 ...(options?.staleTime !== undefined ? { staleTime: options.staleTime } : {}),
175 ...(typeof options?.gcTime === "number" ? { gcTime: options.gcTime } : {}),
176 ...(options?.refetchOnWindowFocus !== undefined ? { refetchOnWindowFocus: options.refetchOnWindowFocus } : {}),
177 structuralSharing,
178 ...(options?.refetchInterval !== undefined ? { refetchInterval: options.refetchInterval } : {}),
179 ...(options?.select !== undefined ? { select: options.select } : {})
180 }
181 const resolvedQueryKey = computed(() => {
182 const input = resolveInput(arg, options?.mode)
183 return fullQueryKey(q, queryKey, input)
184 })
185 watch(
186 resolvedQueryKey,
187 (key, _, onCleanup) => onCleanup(registerQueryInvalidationMode(key, options?.invalidation ?? "await")),
188 { immediate: true }
189 )
190 const tanstack = useTanstackQuery<A, CauseException<E>, TData>({
191 ...tanstackOptions,
192 enabled,
193 throwOnError: false,
194 retry: (retryCount: number, error: unknown) => isRetryable(error) && retryCount < 5,
195 queryKey: resolvedQueryKey,
196 queryFn: (
197 { meta, signal }: {
198 readonly meta?: { readonly span?: Tracer.AnySpan | undefined } | undefined
199 readonly signal: AbortSignal
200 }
201 ) =>
202 runPromise(
203 Effect.gen(function*() {
204 const input = resolveInput(arg, options?.mode)!
205 // Record the repository/server read-dependencies seen while fetching, keyed by the
206 // tanstack queryKey, so a later mutation whose writes intersect them derives this query

Callers 5

makeTanstackHarnessFunction · 0.85
queryHelpersForFunction · 0.85
mapQueryFunction · 0.85

Calls 15

makeRunPromiseFunction · 0.90
makeQueryKeyFunction · 0.90
reportRuntimeErrorFunction · 0.90
setQueryReadDependenciesFunction · 0.90
getRuntimeFunction · 0.85
resolveEnabledFunction · 0.85
computedFunction · 0.85
fullQueryKeyFunction · 0.85
swrToQueryFunction · 0.85
injectRegistryFunction · 0.85
handlerMethod · 0.80
isRetryableFunction · 0.70

Tested by 1

makeTanstackHarnessFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…