| 138 | * ``` |
| 139 | */ |
| 140 | export class QueryCache extends Subscribable<QueryCacheListener> { |
| 141 | #queries: QueryStore |
| 142 | |
| 143 | constructor(public config: QueryCacheConfig = {}) { |
| 144 | super() |
| 145 | this.#queries = new Map<string, Query>() |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Returns the existing `Query` instance for the given options' `queryKey`/`queryHash`, or |
| 150 | * builds and adds a new one to the cache if none exists yet. Used by framework adapters and |
| 151 | * plugins (e.g. broadcast/persistence) that need to get-or-create a `Query` directly, bypassing |
| 152 | * the reactive `QueryObserver` machinery. |
| 153 | * @param client - The client the query belongs to, used to default its options. |
| 154 | * @param options - The query options, including the `queryKey`. A new query is created with the |
| 155 | * options defaulted by {@link QueryClient#defaultQueryOptions}. |
| 156 | * @param state - The initial state of a newly created query, e.g. when hydrating. Ignored if the |
| 157 | * query already exists. |
| 158 | * @returns The existing or newly created query. |
| 159 | * @example |
| 160 | * ```ts |
| 161 | * const queryCache = queryClient.getQueryCache() |
| 162 | * |
| 163 | * const query = queryCache.build(queryClient, { |
| 164 | * queryKey: ['posts'], |
| 165 | * queryFn: fetchPosts, |
| 166 | * }) |
| 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 | } |
nothing calls this directly
no outgoing calls
no test coverage detected