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

Method createResult

packages/query-core/src/queryObserver.ts:588–756  ·  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

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

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