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

Method createResult

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

* Computes the result for a query and options from the query's state, applying `select`, * `placeholderData`, and the derived flags. Subclasses override it to add their own fields. * @param query - The query to compute the result for. * @param options - The observer options to compute the r

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

Source from the content-addressed store, hash-verified

587 * @returns The computed result.
588 */
589 protected createResult(
590 query: Query<TQueryFnData, TError, TQueryData, TQueryKey>,
591 options: QueryObserverOptions<
592 TQueryFnData,
593 TError,
594 TData,
595 TQueryData,
596 TQueryKey
597 >,
598 ): QueryObserverResult<TData, TError> {
599 const prevQuery = this.#currentQuery
600 const prevOptions = this.options
601 const prevResult = this.#currentResult as
602 | QueryObserverResult<TData, TError>
603 | undefined
604 const prevResultState = this.#currentResultState
605 const prevResultOptions = this.#currentResultOptions
606 const queryChange = query !== prevQuery
607 const queryInitialState = queryChange
608 ? query.state
609 : this.#currentQueryInitialState
610
611 const { state } = query
612 let newState = { ...state }
613 let isPlaceholderData = false
614 let data: TData | undefined
615
616 // Optimistically set result in fetching state if needed
617 if (options._optimisticResults) {
618 const mounted = this.hasListeners()
619
620 const fetchOnMount = !mounted && shouldFetchOnMount(query, options)
621
622 const fetchOptionally =
623 mounted && shouldFetchOptionally(query, prevQuery, options, prevOptions)
624
625 if (fetchOnMount || fetchOptionally) {
626 newState = {
627 ...newState,
628 ...fetchState(state.data, query.options),
629 }
630 }
631 if (options._optimisticResults === 'isRestoring') {
632 newState.fetchStatus = 'idle'
633 }
634 }
635
636 let { error, errorUpdatedAt, status } = newState
637
638 // Per default, use query data
639 data = newState.data as unknown as TData
640 let skipSelect = false
641
642 // use placeholderData if needed
643 if (
644 options.placeholderData !== undefined &&
645 data === undefined &&
646 status === 'pending'

Callers 3

getOptimisticResultMethod · 0.95
fetchOptimisticMethod · 0.95
updateResultMethod · 0.95

Calls 8

fetchStateFunction · 0.90
replaceDataFunction · 0.90
resolveQueryValueFunction · 0.90
shouldFetchOnMountFunction · 0.85
shouldFetchOptionallyFunction · 0.85
isStaleFunction · 0.85
hasListenersMethod · 0.80
isFetchedMethod · 0.80

Tested by

no test coverage detected