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

Function makeTanstackQuery

packages/vue/src/internal/tanstackQuery.ts:139–289  ·  view source on GitHub ↗
(
  getRuntime: () => Context.Context<R>,
  queryClient: QueryClient
)

Source from the content-addressed store, hash-verified

137 update: <I, A, E, R, Request extends Req, Name extends string>(
138 _registry: ReturnType<typeof injectRegistry>,
139 query: RequestHandlerWithInput<I, A, E, R, Request, Name>,
140 input: I,
141 updater: (data: NoInfer<A>) => NoInfer<A>
142 ) => {
143 const queryKey = fullQueryKey(query, makeQueryKey(query), input)
144 if (queryClient.getQueryData(queryKey) === undefined) {
145 console.warn(`Query ${query.id} has not been used yet; nothing to update`)
146 return
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: (

Callers 6

makeClientFunction · 0.90
query-span.test.tsFile · 0.85
makeTanstackHarnessFunction · 0.85

Calls 2

subscribeMethod · 0.65

Tested by 1

makeTanstackHarnessFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…