Skip to content

Commit

Permalink
feat(useQuery): enable the throwOnError option of useQuery.refetch (
Browse files Browse the repository at this point in the history
#907)

* feat(useQuery): allow useQuery.refetch to throw an error when the fetch fails

Enables the `throwOnError` configuration option for `useQuery.refetch` that has
as of yet only been present in the documentation but not in the code.

Relates to: #903 #843

* refactor(useQuery): move "useQuery.refetch" options to dedicated type
  • Loading branch information
Garbanas committed Aug 23, 2020
1 parent 7d25d2f commit 2ff6041
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
12 changes: 10 additions & 2 deletions src/core/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ export interface FetchMoreOptions {
previous: boolean
}

export interface RefetchOptions {
throwOnError?: boolean;
}

export enum ActionType {
Failed = 'Failed',
MarkStale = 'MarkStale',
Expand Down Expand Up @@ -224,12 +228,16 @@ export class Query<TResult, TError> {
)
}

async refetch(): Promise<void> {
async refetch(options?: RefetchOptions): Promise<TResult | undefined> {
try {
await this.fetch()
return await this.fetch()
} catch (error) {
if (options?.throwOnError === true) {
throw error;
}
Console.error(error)
}
return;
}

heal(): void {
Expand Down
6 changes: 3 additions & 3 deletions src/core/queryObserver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getStatusProps, isServer, isDocumentVisible, Console } from './utils'
import type { QueryResult, QueryObserverConfig } from './types'
import type { Query, QueryState, Action, FetchMoreOptions } from './query'
import type { Query, QueryState, Action, FetchMoreOptions, RefetchOptions } from './query'

export type UpdateListener<TResult, TError> = (
result: QueryResult<TResult, TError>
Expand Down Expand Up @@ -86,9 +86,9 @@ export class QueryObserver<TResult, TError> {
return this.currentQuery.clear()
}

async refetch(): Promise<void> {
async refetch(options?: RefetchOptions): Promise<TResult | undefined> {
this.currentQuery.updateConfig(this.config)
return this.currentQuery.refetch()
return this.currentQuery.refetch(options)
}

async fetchMore(
Expand Down
4 changes: 2 additions & 2 deletions src/core/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Query, FetchMoreOptions } from './query'
import type { Query, FetchMoreOptions, RefetchOptions } from './query';
import type { QueryCache } from './queryCache'

export type QueryKey =
Expand Down Expand Up @@ -165,7 +165,7 @@ export interface QueryResultBase<TResult, TError = unknown> {
isStale: boolean
isSuccess: boolean
query: Query<TResult, TError>
refetch: () => Promise<void>
refetch: (options?: RefetchOptions) => Promise<TResult | undefined>
status: QueryStatus
updatedAt: number
}
Expand Down

1 comment on commit 2ff6041

@vercel
Copy link

@vercel vercel bot commented on 2ff6041 Aug 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.