Skip to content

Commit

Permalink
refactor(queryObserver): get rid of double-defaulting of options in g…
Browse files Browse the repository at this point in the history
…etOptimisticResult

every call to getOptimisticResult already contains defaulted options, which we can enforce on type level by only accepting DefaultedQueryObserverOptions
  • Loading branch information
TkDodo committed Feb 28, 2022
1 parent 02fd793 commit 9d260e7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/core/infiniteQueryObserver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {
DefaultedInfiniteQueryObserverOptions,
FetchNextPageOptions,
FetchPreviousPageOptions,
InfiniteData,
Expand Down Expand Up @@ -84,7 +85,7 @@ export class InfiniteQueryObserver<
}

getOptimisticResult(
options: InfiniteQueryObserverOptions<
options: DefaultedInfiniteQueryObserverOptions<
TQueryFnData,
TError,
TData,
Expand Down
12 changes: 4 additions & 8 deletions src/core/queryObserver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RefetchPageFilters } from './types'
import { DefaultedQueryObserverOptions, RefetchPageFilters } from './types'
import {
isServer,
isValidTimeout,
Expand Down Expand Up @@ -205,21 +205,17 @@ export class QueryObserver<
}

getOptimisticResult(
options: QueryObserverOptions<
options: DefaultedQueryObserverOptions<
TQueryFnData,
TError,
TData,
TQueryData,
TQueryKey
>
): QueryObserverResult<TData, TError> {
const defaultedOptions = this.client.defaultQueryOptions(options)

const query = this.client
.getQueryCache()
.build(this.client, defaultedOptions)
const query = this.client.getQueryCache().build(this.client, options)

return this.createResult(query, defaultedOptions)
return this.createResult(query, options)
}

getCurrentResult(): QueryObserverResult<TData, TError> {
Expand Down
17 changes: 17 additions & 0 deletions src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,23 @@ export interface InfiniteQueryObserverOptions<
TQueryKey
> {}

export type DefaultedInfiniteQueryObserverOptions<
TQueryFnData = unknown,
TError = unknown,
TData = TQueryFnData,
TQueryData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey
> = WithRequired<
InfiniteQueryObserverOptions<
TQueryFnData,
TError,
TData,
TQueryData,
TQueryKey
>,
'useErrorBoundary' | 'refetchOnReconnect'
>

export interface FetchQueryOptions<
TQueryFnData = unknown,
TError = unknown,
Expand Down

0 comments on commit 9d260e7

Please sign in to comment.