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
16 changes: 16 additions & 0 deletions .changeset/adapter-error-boundary-options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'@tanstack/angular-query': major
'@tanstack/lit-query': major
'@tanstack/preact-query': major
'@tanstack/query-core': major
'@tanstack/react-query': major
'@tanstack/solid-query': major
'@tanstack/svelte-query': major
'@tanstack/vue-query': major
---

Move error boundary options out of query-core. React, Preact, Solid, and Vue still accept `throwOnError` in their adapter options and client defaults. Import `QueryClient` from the adapter to configure those defaults. Imperative refetch methods still accept `throwOnError` to control promise rejection.

Query observers now notify listeners when the result's error changes, even when the component does not track `error`. Set `notifyOnChangeProps: 'none'` to suppress all notifications and opt out of the query's `throwOnError` boundary handling.

In React and Preact, `useQueries` skips `combine` when a query error needs to reach the error boundary.
10 changes: 10 additions & 0 deletions .changeset/adapter-suspense-policy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@tanstack/query-core': major
'@tanstack/react-query': major
'@tanstack/preact-query': major
'@tanstack/lit-query': major
'@tanstack/svelte-query': major
'@tanstack/vue-query': major
---

Remove the `suspense` observer option from query-core. React and Preact select Suspense behavior through their dedicated hooks.
5 changes: 0 additions & 5 deletions .changeset/query-core-queries-observer-throw-on-error.md

This file was deleted.

2 changes: 1 addition & 1 deletion .changeset/static-notification-modes.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@

Limit `notifyOnChangeProps` to the static values `'all'` and `'none'`.

Replace `[]` with `'none'` to suppress observer notifications while the query stays subscribed and can fetch. Error changes still notify the observer when `throwOnError` is set. Keep `'all'` to receive every result change.
Replace `[]` with `'none'` to suppress observer notifications while the query stays subscribed and can fetch. Keep `'all'` to receive every result change.

For non-empty lists or callbacks, omit the option to use the adapter's default or choose a static value. In React Native, use `subscribed: isFocused` if the screen can unsubscribe when it loses focus.
4 changes: 2 additions & 2 deletions docs/framework/react/guides/render-optimizations.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ The top level object returned from `useQuery`, `useInfiniteQuery`, `useMutation`

## tracked properties

React Query will only trigger a re-render if one of the properties returned from `useQuery` is actually "used". This is done by using [Proxy object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy). This avoids a lot of unnecessary re-renders, e.g. because properties like `isFetching` or `isStale` might change often, but are not used in the component.
React Query tracks which `useQuery` result properties the component reads with a [Proxy object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy). This avoids re-renders when unused properties such as `isFetching` or `isStale` change.

Set `notifyOnChangeProps: 'all'` to notify the component whenever the query result changes. Set `notifyOnChangeProps: 'none'` to suppress these notifications while the query can still fetch. If you set `throwOnError`, changes to the error still notify the component. You can set these options in the query defaults or on individual queries.
Changes to the query error notify the component even if it does not read `error`. Set `notifyOnChangeProps: 'all'` to receive every result change. Set `notifyOnChangeProps: 'none'` to suppress result notifications and disable `throwOnError` for that observer. The query can still fetch. You can set these options in the query defaults or on individual queries.

> Note: The get trap of a proxy is invoked by accessing a property, either via destructuring or by accessing it directly. If you use object rest destructuring, you will disable this optimization. We have a [lint rule](../../../eslint/no-rest-destructuring.md) to guard against this pitfall.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ describe('injectMutation', () => {

it('publishes mutation failures to observers and cache signals without global reporting', async () => {
vi.useRealTimers()
queryClient.setDefaultOptions({ mutations: { throwOnError: true } })
const cacheReport = vi.fn<(error: Error) => void>()
queryClient.getMutationCache().config.onError = cacheReport
const report = vi.spyOn(TestBed.inject(ErrorHandler), 'handleError')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe('injectQueries', () => {
it('should keep failed refetch state and cached data readable without global reporting', async () => {
vi.useRealTimers()
queryClient.setDefaultOptions({
queries: { retry: false, throwOnError: true },
queries: { retry: false },
})
const report = vi.spyOn(TestBed.inject(ErrorHandler), 'handleError')
const error = new Error('query failed')
Expand Down
2 changes: 1 addition & 1 deletion packages/angular-query/src/__tests__/inject-query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('injectQuery', () => {
it('should keep failed refetch state and cached data readable without global reporting', async () => {
vi.useRealTimers()
queryClient.setDefaultOptions({
queries: { retry: false, throwOnError: true },
queries: { retry: false },
})
const report = vi.spyOn(TestBed.inject(ErrorHandler), 'handleError')
const error = new Error('query failed')
Expand Down
14 changes: 6 additions & 8 deletions packages/angular-query/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ type SignalFunction<T extends () => any> = T & Signal<ReturnType<T>>

/**
* The options accepted by `injectQuery`, with cache data projected through an optional `select`. Same as
* {@link QueryObserverOptions} from `@tanstack/query-core`, minus `notifyOnChangeProps`, `suspense`, and
* `throwOnError`: Angular owns result notifications and exposes errors through signals rather than error
* boundaries.
* {@link QueryObserverOptions} from `@tanstack/query-core`, minus `notifyOnChangeProps`. Angular owns result
* notifications and exposes errors through signals.
* @template TQueryFnData - The type your `queryFn` resolves to.
* @template TError - The type of errors your `queryFn` may throw.
* @template TData - The type `data` ends up as after `select` runs. Defaults to `TQueryFnData` when no
Expand All @@ -43,7 +42,7 @@ export type CreateQueryOptions<
TQueryKey extends QueryKey = QueryKey,
> = OmitKeyof<
QueryObserverOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>,
'notifyOnChangeProps' | 'suspense' | 'throwOnError'
'notifyOnChangeProps'
>

type CreateStatusBasedQueryResult<
Expand Down Expand Up @@ -232,8 +231,7 @@ export interface DefinedInfiniteQueryNarrowing<
/**
* The options accepted by `injectInfiniteQuery`, including page parameters and pagination callbacks. Same as
* {@link InfiniteQueryObserverOptions} from `@tanstack/query-core` β€” for the infinite-query-specific options
* (`getNextPageParam`, `initialPageParam`, etc.) β€” minus `notifyOnChangeProps`, `suspense`, and
* `throwOnError`, like {@link CreateQueryOptions}.
* (`getNextPageParam`, `initialPageParam`, etc.) β€” minus `notifyOnChangeProps`, like {@link CreateQueryOptions}.
* @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.
* @template TError - The type of errors your `queryFn` may throw.
* @template TData - The type `data` ends up as after `select` runs. Defaults to `TQueryFnData` here, though
Expand All @@ -256,7 +254,7 @@ export interface CreateInfiniteQueryOptions<
TQueryKey,
TPageParam
>,
'notifyOnChangeProps' | 'suspense' | 'throwOnError'
'notifyOnChangeProps'
> {}

/**
Expand Down Expand Up @@ -359,7 +357,7 @@ export interface CreateMutationOptions<
TOnMutateResult = unknown,
> extends OmitKeyof<
MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>,
'_defaulted' | 'throwOnError'
'_defaulted'
> {}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ describe('createQueriesController', () => {
const key5 = createKey()

type BizError = { code: number }
const throwOnError = (_error: BizError) => true
const retry = (_failureCount: number, _error: BizError) => true

// Array.map preserves TQueryFnData
const result1 = createQueriesController(
Expand All @@ -589,7 +589,7 @@ describe('createQueriesController', () => {
queries: Array(50).map((_, i) => ({
queryKey: ['key', i] as const,
queryFn: () => i + 10,
throwOnError,
retry,
})),
},
new QueryClient(),
Expand Down Expand Up @@ -625,7 +625,7 @@ describe('createQueriesController', () => {
queryKey: ['key', i] as const,
queryFn: () => i + 10,
select: (data: number) => data.toString(),
throwOnError,
retry,
})),
},
new QueryClient(),
Expand Down Expand Up @@ -654,7 +654,7 @@ describe('createQueriesController', () => {
{
queryKey: key5,
queryFn: () => 'string',
throwOnError,
retry,
},
],
},
Expand All @@ -674,7 +674,7 @@ describe('createQueriesController', () => {
expectTypeOf(result3[3].data).toEqualTypeOf<string | undefined>()
// select takes precedence over queryFn
expectTypeOf(result3[2].data).toEqualTypeOf<number | undefined>()
// infer TError from throwOnError
// infer TError from retry
expectTypeOf(result3[3].error).toEqualTypeOf<BizError | null>()

// initialData/placeholderData are enforced
Expand All @@ -701,7 +701,7 @@ describe('createQueriesController', () => {
new QueryClient(),
)()

// select and throwOnError params are "indirectly" enforced
// select and retry params are "indirectly" enforced
createQueriesController(
new Host(),
{
Expand Down Expand Up @@ -729,7 +729,7 @@ describe('createQueriesController', () => {
{
queryKey: key5,
queryFn: () => 'string',
throwOnError,
retry,
},
],
},
Expand Down Expand Up @@ -782,7 +782,7 @@ describe('createQueriesController', () => {
queryKey: key5,
queryFn: () => 'string',
select: (a: string) => parseInt(a),
throwOnError,
retry,
},
],
},
Expand Down Expand Up @@ -834,7 +834,7 @@ describe('createQueriesController', () => {
{
queryKey: key5,
queryFn: () => 'string',
throwOnError,
retry,
},
],
} as const,
Expand Down
11 changes: 8 additions & 3 deletions packages/lit-query/src/createQueriesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import type {
QueryKey,
QueryObserverOptions,
QueryObserverResult,
ThrowOnError,
} from '@tanstack/query-core'
import type { ReactiveControllerHost } from 'lit'
import type { Accessor, ValueAccessor } from './accessor.js'
Expand Down Expand Up @@ -63,7 +62,10 @@ type GetCreateQueriesInput<T> = T extends {
| QueryFunction<infer TQueryFnData, infer TQueryKey>
| SkipTokenForCreateQueries
select?: (data: any) => infer TData
throwOnError?: ThrowOnError<any, infer TError, any, any>
retry?:
| boolean
| number
| ((failureCount: number, error: infer TError) => boolean)
}
? CreateQueriesInputForController<
TQueryFnData,
Expand Down Expand Up @@ -114,7 +116,10 @@ type GetCreateQueriesResult<T> = T extends {
| QueryFunction<infer TQueryFnData, any>
| SkipTokenForCreateQueries
select?: (data: any) => infer TData
throwOnError?: ThrowOnError<any, infer TError, any, any>
retry?:
| boolean
| number
| ((failureCount: number, error: infer TError) => boolean)
}
? GetDefinedOrUndefinedCreateQueriesResult<
T,
Expand Down
88 changes: 88 additions & 0 deletions packages/preact-query/src/QueryClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { QueryClient as CoreQueryClient } from '@tanstack/query-core'
import type {
DefaultOptions as CoreDefaultOptions,
QueryClientConfig as CoreQueryClientConfig,
DefaultError,
MutationKey,
QueryKey,
} from '@tanstack/query-core'
import type {
ThrowOnError,
UseBaseQueryOptions,
UseMutationOptions,
} from './types'

/** Client defaults, including the adapter's error boundary options. */
export interface DefaultOptions<
TError = DefaultError,
> extends CoreDefaultOptions<TError> {
/** Default query options, including whether to throw errors to an error boundary. */
queries?: CoreDefaultOptions<TError>['queries'] &
Pick<UseBaseQueryOptions<unknown, TError>, 'throwOnError'>
/** Default mutation options, including whether to throw errors to an error boundary. */
mutations?: CoreDefaultOptions<TError>['mutations'] &
Pick<UseMutationOptions<unknown, TError, unknown>, 'throwOnError'>
}

/** Configuration for a query client with adapter-specific defaults. */
export interface QueryClientConfig extends CoreQueryClientConfig {
/** Default options for all queries and mutations created through this client. */
defaultOptions?: DefaultOptions
}

/** A query client that accepts the adapter's error boundary options as defaults. */
export interface QueryClient extends CoreQueryClient {
/** {@inheritDoc CoreQueryClient.getDefaultOptions} */
getDefaultOptions: () => DefaultOptions
/** {@inheritDoc CoreQueryClient.setDefaultOptions} */
setDefaultOptions: (options: DefaultOptions) => void

/** {@inheritDoc CoreQueryClient.setQueryDefaults} */
setQueryDefaults: <
TQueryFnData = unknown,
TError = DefaultError,
TData = TQueryFnData,
TQueryData = TQueryFnData,
>(
queryKey: QueryKey,
options: Partial<
Omit<
UseBaseQueryOptions<TQueryFnData, TError, TData, TQueryData>,
'queryKey' | 'subscribed'
>
>,
) => void

/** {@inheritDoc CoreQueryClient.getQueryDefaults} */
getQueryDefaults: (queryKey: QueryKey) => ReturnType<
CoreQueryClient['getQueryDefaults']
> & {
throwOnError?: ThrowOnError<any, any, any, any>
}

/** {@inheritDoc CoreQueryClient.setMutationDefaults} */
setMutationDefaults: <
TData = unknown,
TError = DefaultError,
TVariables = void,
TOnMutateResult = unknown,
>(
mutationKey: MutationKey,
options: Omit<
UseMutationOptions<TData, TError, TVariables, TOnMutateResult>,
'mutationKey'
>,
) => void

/** {@inheritDoc CoreQueryClient.getMutationDefaults} */
getMutationDefaults: (
mutationKey: MutationKey,
) => ReturnType<CoreQueryClient['getMutationDefaults']> &
Pick<UseMutationOptions<any, any, any, any>, 'throwOnError'>
}

/** Creates a query client. The constructor is shared with query-core. */
export const QueryClient: {
new (config?: QueryClientConfig): QueryClient
readonly prototype: QueryClient
} = CoreQueryClient
2 changes: 1 addition & 1 deletion packages/preact-query/src/QueryClientProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createContext } from 'preact'
import { useContext, useEffect } from 'preact/hooks'
import type { QueryClient } from '@tanstack/query-core'
import type { ComponentChildren, VNode } from 'preact'
import type { QueryClient } from './QueryClient'

/**
* The context that `useQueryClient` reads from. `QueryClientProvider` is the normal way to set it.
Expand Down
Loading
Loading