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

Method setOptions

packages/query-core/src/queryObserver.ts:189–271  ·  view source on GitHub ↗

* Updates the observer's options. This will re-resolve the query being * observed (switching to a different query if the `queryKey` changed), * trigger a fetch if the new options require one and the observer has * subscribers, recompute the current result, and reschedule the stale and *

(
    options: QueryObserverOptions<
      TQueryFnData,
      TError,
      TData,
      TQueryData,
      TQueryKey
    >,
  )

Source from the content-addressed store, hash-verified

187 * ```
188 */
189 setOptions(
190 options: QueryObserverOptions<
191 TQueryFnData,
192 TError,
193 TData,
194 TQueryData,
195 TQueryKey
196 >,
197 ): void {
198 const prevOptions = this.options
199 const prevQuery = this.#currentQuery
200
201 this.options = this.#client.defaultQueryOptions(options)
202
203 if (
204 this.options.enabled !== undefined &&
205 typeof this.options.enabled !== 'boolean' &&
206 typeof this.options.enabled !== 'function' &&
207 typeof resolveQueryValue(this.options.enabled, this.#currentQuery) !==
208 'boolean'
209 ) {
210 throw new Error(
211 'Expected enabled to be a boolean or a callback that returns a boolean',
212 )
213 }
214
215 this.#updateQuery()
216 this.#currentQuery.setOptions(this.options)
217
218 if (
219 prevOptions._defaulted &&
220 !shallowEqualObjects(this.options, prevOptions)
221 ) {
222 this.#client.getQueryCache().notify({
223 type: 'observerOptionsUpdated',
224 query: this.#currentQuery,
225 observer: this,
226 })
227 }
228
229 const mounted = this.hasListeners()
230
231 // Fetch if there are subscribers
232 if (
233 mounted &&
234 shouldFetchOptionally(
235 this.#currentQuery,
236 prevQuery,
237 this.options,
238 prevOptions,
239 )
240 ) {
241 this.#executeFetch()
242 }
243
244 // Update result
245 this.updateResult()
246

Callers 1

constructorMethod · 0.95

Calls 13

#updateQueryMethod · 0.95
#executeFetchMethod · 0.95
updateResultMethod · 0.95
#updateStaleTimeoutMethod · 0.95
resolveQueryValueFunction · 0.90
shallowEqualObjectsFunction · 0.90
shouldFetchOptionallyFunction · 0.85
defaultQueryOptionsMethod · 0.80
getQueryCacheMethod · 0.80
hasListenersMethod · 0.80

Tested by

no test coverage detected