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

Method build

packages/query-core/src/queryCache.ts:169–200  ·  view source on GitHub ↗

* Returns the existing `Query` instance for the given options' `queryKey`/`queryHash`, or * builds and adds a new one to the cache if none exists yet. Used by framework adapters and * plugins (e.g. broadcast/persistence) that need to get-or-create a `Query` directly, bypassing * the reactiv

(
    client: QueryClient,
    options: WithRequired<
      QueryOptions<TQueryFnData, TError, TData, TQueryKey>,
      'queryKey'
    >,
    state?: QueryState<TData, TError>,
  )

Source from the content-addressed store, hash-verified

167 * ```
168 */
169 build<
170 TQueryFnData = unknown,
171 TError = DefaultError,
172 TData = TQueryFnData,
173 TQueryKey extends QueryKey = QueryKey,
174 >(
175 client: QueryClient,
176 options: WithRequired<
177 QueryOptions<TQueryFnData, TError, TData, TQueryKey>,
178 'queryKey'
179 >,
180 state?: QueryState<TData, TError>,
181 ): Query<TQueryFnData, TError, TData, TQueryKey> {
182 const queryKey = options.queryKey
183 const queryHash =
184 options.queryHash ?? hashQueryKeyByOptions(queryKey, options)
185 let query = this.get<TQueryFnData, TError, TData, TQueryKey>(queryHash)
186
187 if (!query) {
188 query = new Query({
189 client,
190 queryKey,
191 queryHash,
192 options: client.defaultQueryOptions(options),
193 state,
194 defaultOptions: client.getQueryDefaults(queryKey),
195 })
196 this.add(query)
197 }
198
199 return query
200 }
201
202 /**
203 * Adds a query to the cache.

Callers 15

buildQueryFunction · 0.45
buildMutationFunction · 0.45
makeStateFunction · 0.45
Devtools.test.tsxFile · 0.45
executeFunction · 0.45
persist.test.tsFile · 0.45

Calls 5

getMethod · 0.95
addMethod · 0.95
hashQueryKeyByOptionsFunction · 0.90
defaultQueryOptionsMethod · 0.80
getQueryDefaultsMethod · 0.45

Tested by 5

buildQueryFunction · 0.36
buildMutationFunction · 0.36
makeStateFunction · 0.36
executeFunction · 0.36
executeMutationFunction · 0.36