Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/core/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ export class Query<TResult, TError> {
}
}

async fetchMore(
fetchMore(
fetchMoreVariable?: unknown,
options?: FetchMoreOptions,
config?: ResolvedQueryConfig<TResult, TError>
Expand Down Expand Up @@ -380,7 +380,7 @@ export class Query<TResult, TError> {
return this.promise
}

private async startFetch(
private startFetch(
config: ResolvedQueryConfig<TResult, TError>,
params: unknown[],
_options?: FetchOptions
Expand All @@ -397,7 +397,7 @@ export class Query<TResult, TError> {
return this.tryFetchData(config, fetchData)
}

private async startInfiniteFetch(
private startInfiniteFetch(
config: ResolvedQueryConfig<TResult, TError>,
params: unknown[],
options?: FetchOptions
Expand Down Expand Up @@ -455,7 +455,7 @@ export class Query<TResult, TError> {
return this.tryFetchData(config, fetchData)
}

private async tryFetchData<T>(
private tryFetchData<T>(
config: ResolvedQueryConfig<TResult, TError>,
fn: QueryFunction<T>
): Promise<T> {
Expand Down
19 changes: 9 additions & 10 deletions src/core/queryObserver.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {
getStatusProps,
isServer,
isDocumentVisible,
isServer,
isValidTimeout,
noop,
} from './utils'
import type { QueryResult, ResolvedQueryConfig } from './types'
import type { Query, Action, FetchMoreOptions, RefetchOptions } from './query'
Expand Down Expand Up @@ -108,23 +109,21 @@ export class QueryObserver<TResult, TError> {
return this.currentQuery.clear()
}

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

async fetchMore(
fetchMore(
fetchMoreVariable?: unknown,
options?: FetchMoreOptions
): Promise<TResult | undefined> {
return this.currentQuery.fetchMore(fetchMoreVariable, options, this.config)
return this.currentQuery
.fetchMore(fetchMoreVariable, options, this.config)
.catch(noop)
}

async fetch(): Promise<TResult | undefined> {
try {
return await this.currentQuery.fetch(undefined, this.config)
} catch {
// ignore
}
fetch(): Promise<TResult | undefined> {
return this.currentQuery.fetch(undefined, this.config).catch(noop)
}

private optionalFetch(): void {
Expand Down
4 changes: 2 additions & 2 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export function uid(): number {

export const isServer = typeof window === 'undefined'

export function noop(): void {
return void 0
export function noop(): undefined {
return undefined
}

export let Console: ConsoleObject = console || {
Expand Down
44 changes: 43 additions & 1 deletion src/react/tests/useInfiniteQuery.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { render, waitFor, fireEvent } from '@testing-library/react'
import * as React from 'react'

import { sleep, queryKey, waitForMs } from './utils'
import { sleep, queryKey, waitForMs, mockConsoleError } from './utils'
import { useInfiniteQuery, useQueryCache } from '..'
import { InfiniteQueryResult } from '../../core'

Expand Down Expand Up @@ -112,6 +112,48 @@ describe('useInfiniteQuery', () => {
})
})

it('should not throw when fetchMore returns an error', async () => {
const consoleMock = mockConsoleError()
const key = queryKey()
let noThrow: boolean

function Page() {
const start = 1
const state = useInfiniteQuery(
key,
async (_key, page: number = start) => {
if (page === 2) {
throw new Error('error')
}
return page
},
{
retry: 1,
retryDelay: 10,
getFetchMore: (lastPage, _pages) => lastPage + 1,
}
)

const { fetchMore } = state

React.useEffect(() => {
setTimeout(async () => {
try {
await fetchMore()
noThrow = true
} catch (error) {}
}, 20)
}, [fetchMore])

return null
}

render(<Page />)

await waitFor(() => expect(noThrow).toBe(true))
consoleMock.mockRestore()
})

it('should keep the previous data when keepPreviousData is set', async () => {
const key = queryKey()
const states: InfiniteQueryResult<string>[] = []
Expand Down
2 changes: 1 addition & 1 deletion src/react/useBaseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getResolvedQueryConfig } from '../core/config'
import { QueryObserver } from '../core/queryObserver'
import { QueryResultBase, QueryKey, QueryConfig } from '../core/types'
import { useErrorResetBoundary } from './ReactQueryErrorResetBoundary'
import { useQueryCache } from '.'
import { useQueryCache } from './ReactQueryCacheProvider'
import { useContextConfig } from './ReactQueryConfigProvider'

export function useBaseQuery<TResult, TError>(
Expand Down