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
5 changes: 5 additions & 0 deletions .changeset/stable-streamed-query.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/query-core': major
---

Stabilize `streamedQuery`. Replace imports of `experimental_streamedQuery` with `streamedQuery` in v6.
56 changes: 28 additions & 28 deletions docs/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1068,10 +1068,6 @@
"label": "Functions / dehydrateQuery",
"to": "framework/react/reference/functions/dehydrateQuery"
},
{
"label": "Functions / experimental_streamedQuery",
"to": "framework/react/reference/functions/experimental_streamedQuery"
},
{
"label": "Functions / hashKey",
"to": "framework/react/reference/functions/hashKey"
Expand Down Expand Up @@ -1124,6 +1120,10 @@
"label": "Functions / shouldThrowError",
"to": "framework/react/reference/functions/shouldThrowError"
},
{
"label": "Functions / streamedQuery",
"to": "framework/react/reference/functions/streamedQuery"
},
{
"label": "Functions / useInfiniteQuery",
"to": "framework/react/reference/functions/useInfiniteQuery"
Expand Down Expand Up @@ -1937,10 +1937,6 @@
"label": "Functions / dehydrateQuery",
"to": "framework/vue/reference/functions/dehydrateQuery"
},
{
"label": "Functions / experimental_streamedQuery",
"to": "framework/vue/reference/functions/experimental_streamedQuery"
},
{
"label": "Functions / hashKey",
"to": "framework/vue/reference/functions/hashKey"
Expand Down Expand Up @@ -1993,6 +1989,10 @@
"label": "Functions / shouldThrowError",
"to": "framework/vue/reference/functions/shouldThrowError"
},
{
"label": "Functions / streamedQuery",
"to": "framework/vue/reference/functions/streamedQuery"
},
{
"label": "Functions / useInfiniteQuery",
"to": "framework/vue/reference/functions/useInfiniteQuery"
Expand Down Expand Up @@ -2694,10 +2694,6 @@
"label": "Functions / dehydrateQuery",
"to": "framework/solid/reference/functions/dehydrateQuery"
},
{
"label": "Functions / experimental_streamedQuery",
"to": "framework/solid/reference/functions/experimental_streamedQuery"
},
{
"label": "Functions / hashKey",
"to": "framework/solid/reference/functions/hashKey"
Expand Down Expand Up @@ -2750,6 +2746,10 @@
"label": "Functions / shouldThrowError",
"to": "framework/solid/reference/functions/shouldThrowError"
},
{
"label": "Functions / streamedQuery",
"to": "framework/solid/reference/functions/streamedQuery"
},
{
"label": "Functions / useInfiniteQuery",
"to": "framework/solid/reference/functions/useInfiniteQuery"
Expand Down Expand Up @@ -3483,10 +3483,6 @@
"label": "Functions / dehydrateQuery",
"to": "framework/svelte/reference/functions/dehydrateQuery"
},
{
"label": "Functions / experimental_streamedQuery",
"to": "framework/svelte/reference/functions/experimental_streamedQuery"
},
{
"label": "Functions / getIsRestoringContext",
"to": "framework/svelte/reference/functions/getIsRestoringContext"
Expand Down Expand Up @@ -3555,6 +3551,10 @@
"label": "Functions / shouldThrowError",
"to": "framework/svelte/reference/functions/shouldThrowError"
},
{
"label": "Functions / streamedQuery",
"to": "framework/svelte/reference/functions/streamedQuery"
},
{
"label": "Functions / useHydrate",
"to": "framework/svelte/reference/functions/useHydrate"
Expand Down Expand Up @@ -4300,10 +4300,6 @@
"label": "Functions / dehydrateQuery",
"to": "framework/lit/reference/functions/dehydrateQuery"
},
{
"label": "Functions / experimental_streamedQuery",
"to": "framework/lit/reference/functions/experimental_streamedQuery"
},
{
"label": "Functions / hashKey",
"to": "framework/lit/reference/functions/hashKey"
Expand Down Expand Up @@ -4356,6 +4352,10 @@
"label": "Functions / shouldThrowError",
"to": "framework/lit/reference/functions/shouldThrowError"
},
{
"label": "Functions / streamedQuery",
"to": "framework/lit/reference/functions/streamedQuery"
},
{
"label": "Functions / useIsFetching",
"to": "framework/lit/reference/functions/useIsFetching"
Expand Down Expand Up @@ -5025,10 +5025,6 @@
"label": "Functions / dehydrateQuery",
"to": "framework/angular/reference/functions/dehydrateQuery"
},
{
"label": "Functions / experimental_streamedQuery",
"to": "framework/angular/reference/functions/experimental_streamedQuery"
},
{
"label": "Functions / hashKey",
"to": "framework/angular/reference/functions/hashKey"
Expand Down Expand Up @@ -5117,6 +5113,10 @@
"label": "Functions / shouldThrowError",
"to": "framework/angular/reference/functions/shouldThrowError"
},
{
"label": "Functions / streamedQuery",
"to": "framework/angular/reference/functions/streamedQuery"
},
{
"label": "Functions / toResource",
"to": "framework/angular/reference/functions/toResource"
Expand Down Expand Up @@ -5834,10 +5834,6 @@
"label": "Functions / dehydrateQuery",
"to": "framework/preact/reference/functions/dehydrateQuery"
},
{
"label": "Functions / experimental_streamedQuery",
"to": "framework/preact/reference/functions/experimental_streamedQuery"
},
{
"label": "Functions / hashKey",
"to": "framework/preact/reference/functions/hashKey"
Expand Down Expand Up @@ -5890,6 +5886,10 @@
"label": "Functions / shouldThrowError",
"to": "framework/preact/reference/functions/shouldThrowError"
},
{
"label": "Functions / streamedQuery",
"to": "framework/preact/reference/functions/streamedQuery"
},
{
"label": "Functions / useInfiniteQuery",
"to": "framework/preact/reference/functions/useInfiniteQuery"
Expand Down
2 changes: 1 addition & 1 deletion docs/framework/vue/guides/suspense.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default defineComponent({

## How `suspense()` resolves

- If the query has no data or its data is stale, it fetches the query and resolves with the result once that fetch resolves. This is usually when the query function finishes, but can be earlier, such as after the first chunk of an `experimental_streamedQuery` or when `setQueryData` sets data while the fetch is in flight.
- If the query has no data or its data is stale, it fetches the query and resolves with the result once that fetch resolves. This is usually when the query function finishes, but can be earlier, such as after the first chunk of an `streamedQuery` or when `setQueryData` sets data while the fetch is in flight.
- If the data is fresh, it resolves immediately without refetching.
- While the query is disabled (`enabled: false`), it waits until the query is enabled, so it never resolves for a query that stays disabled. On the server, this blocks the render, see [SSR](./ssr.md#suspense-of-a-query-that-stays-disabled-blocks-the-render-on-the-server).
- If the fetch fails, it resolves with the query result in the error state. It rejects with the error only when `throwOnError` is (or returns) `true`.
Expand Down
2 changes: 1 addition & 1 deletion examples/lit/chat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A chat UI that streams the answer into the query cache with `streamedQuery`.

## How it works

- `src/services/messages/queries.ts` defines `messagesQueries.detail(id, question)`, a `queryOptions` factory whose `queryFn` is `experimental_streamedQuery` (imported as `streamedQuery`) with `staleTime: Infinity`.
- `src/services/messages/queries.ts` defines `messagesQueries.detail(id, question)`, a `queryOptions` factory whose `queryFn` is `streamedQuery` with `staleTime: Infinity`.
- `src/services/messages/api.ts` returns an async iterable that yields the answer one word at a time with a random delay and stops when the `signal` aborts.
- `src/chat-message.ts` calls `createQueryController` with `messagesQueries.detail(...)`. `data` grows as chunks arrive and is joined into the answer, and `isFetching` stays `true` until the stream ends.
- `src/main.ts` subclasses `QueryClientProvider` to set the shared `QueryClient` and renders one `<chat-message>` per submitted question.
Expand Down
5 changes: 1 addition & 4 deletions examples/lit/chat/src/services/messages/queries.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
queryOptions,
experimental_streamedQuery as streamedQuery,
} from '@tanstack/lit-query'
import { queryOptions, streamedQuery } from '@tanstack/lit-query'
import { messagesAPI } from './api'

export const messagesQueries = {
Expand Down
2 changes: 1 addition & 1 deletion examples/preact/chat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A chat UI that streams the answer into the query cache with `streamedQuery`.

## How it works

- `src/services/messages/queries.ts` defines `messagesQueries.detail(id, question)`, a `queryOptions` factory whose `queryFn` is `experimental_streamedQuery` (imported as `streamedQuery`) with `staleTime: Infinity`.
- `src/services/messages/queries.ts` defines `messagesQueries.detail(id, question)`, a `queryOptions` factory whose `queryFn` is `streamedQuery` with `staleTime: Infinity`.
- `src/services/messages/api.ts` returns an async iterable that yields the answer one word at a time with a random delay and stops when the `signal` aborts.
- In `src/index.tsx`, each `ChatMessage` calls `useQuery(messagesQueries.detail(id, question))`. `data` grows as chunks arrive and is joined into the answer, and `isFetching` stays `true` until the stream ends.

Expand Down
5 changes: 1 addition & 4 deletions examples/preact/chat/src/services/messages/queries.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
queryOptions,
experimental_streamedQuery as streamedQuery,
} from '@tanstack/preact-query'
import { queryOptions, streamedQuery } from '@tanstack/preact-query'
import { messagesAPI } from './api'

export const messagesQueries = {
Expand Down
2 changes: 1 addition & 1 deletion examples/react/chat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A chat UI that streams the answer into the query cache with `streamedQuery`.

## How it works

- `src/services/messages/queries.ts` defines `messagesQueries.detail(id, question)` with `queryOptions`, using `experimental_streamedQuery` (imported as `streamedQuery`) as the `queryFn` and `staleTime: Infinity`.
- `src/services/messages/queries.ts` defines `messagesQueries.detail(id, question)` with `queryOptions`, using `streamedQuery` as the `queryFn` and `staleTime: Infinity`.
- `src/services/messages/api.ts` returns an async iterable that yields a canned answer word by word with random delays and stops when the query's `signal` is aborted.
- `src/index.tsx` renders a `ChatMessage` per question, which calls `useQuery(messagesQueries.detail(id, question))`. `data` is the array of chunks received so far, joined into the answer, and `isFetching` stays `true` until the stream ends.

Expand Down
5 changes: 1 addition & 4 deletions examples/react/chat/src/services/messages/queries.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
queryOptions,
experimental_streamedQuery as streamedQuery,
} from '@tanstack/react-query'
import { queryOptions, streamedQuery } from '@tanstack/react-query'
import { messagesAPI } from './api'

export const messagesQueries = {
Expand Down
2 changes: 1 addition & 1 deletion examples/solid/chat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A chat UI that streams the answer into the query cache with `streamedQuery`.

## How it works

- `src/services/messages/queries.ts` defines `messagesQueries.detail(id, question)` with `queryOptions`. Its `queryFn` is `streamedQuery` (imported as `experimental_streamedQuery`), which appends each chunk from `streamFn` to `data` as it arrives, and `staleTime: Infinity` keeps a finished answer from being refetched.
- `src/services/messages/queries.ts` defines `messagesQueries.detail(id, question)` with `queryOptions`. Its `queryFn` is `streamedQuery`, which appends each chunk from `streamFn` to `data` as it arrives, and `staleTime: Infinity` keeps a finished answer from being refetched.
- `src/services/messages/api.ts` returns an async iterable that yields a canned answer word by word with random delays and stops when the query's `signal` is aborted.
- `src/index.tsx` renders a `ChatMessage` per question, which reads the streamed words from `useQuery` and joins them. `isFetching` stays `true` until the stream ends, which shows the "..." indicator.

Expand Down
5 changes: 1 addition & 4 deletions examples/solid/chat/src/services/messages/queries.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
queryOptions,
experimental_streamedQuery as streamedQuery,
} from '@tanstack/solid-query'
import { queryOptions, streamedQuery } from '@tanstack/solid-query'
import { messagesAPI } from './api'

export const messagesQueries = {
Expand Down
2 changes: 1 addition & 1 deletion examples/svelte/chat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A chat UI that streams the answer into the query cache with `streamedQuery`.

## How it works

- `src/lib/services/messages/queries.ts` defines `messagesQueries.detail(id, question)` with `queryOptions`. Its `queryFn` is `streamedQuery` (imported as `experimental_streamedQuery`), which appends each chunk from `streamFn` to `data` as it arrives, and `staleTime: Infinity` keeps a finished answer from being refetched.
- `src/lib/services/messages/queries.ts` defines `messagesQueries.detail(id, question)` with `queryOptions`. Its `queryFn` is `streamedQuery`, which appends each chunk from `streamFn` to `data` as it arrives, and `staleTime: Infinity` keeps a finished answer from being refetched.
- `src/lib/services/messages/api.ts` returns an async iterable that yields a canned answer word by word with random delays and stops when the query's `signal` is aborted.
- `src/lib/Example.svelte` keeps the list of questions and renders a `ChatMessage` for each one.
- `src/lib/ChatMessage.svelte` reads the streamed words from `createQuery` and joins them. `isFetching` stays `true` until the stream ends, which shows the "..." indicator.
Expand Down
5 changes: 1 addition & 4 deletions examples/svelte/chat/src/lib/services/messages/queries.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
queryOptions,
experimental_streamedQuery as streamedQuery,
} from '@tanstack/svelte-query'
import { queryOptions, streamedQuery } from '@tanstack/svelte-query'
import { messagesAPI } from './api'

export const messagesQueries = {
Expand Down
2 changes: 1 addition & 1 deletion examples/vue/chat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A chat UI that streams the answer into the query cache with `streamedQuery`.
## How it works

- `src/services/messages/api.ts` returns an async iterable that yields a canned answer one word at a time with a random delay, and stops when the `signal` aborts.
- `src/services/messages/queries.ts` defines `messagesQueries.detail(id, question)`, a `queryOptions` factory whose `queryFn` is `experimental_streamedQuery` (imported as `streamedQuery`). Each yielded word is appended to `data`, and `staleTime: Infinity` keeps a finished answer from refetching.
- `src/services/messages/queries.ts` defines `messagesQueries.detail(id, question)`, a `queryOptions` factory whose `queryFn` is `streamedQuery`. Each yielded word is appended to `data`, and `staleTime: Infinity` keeps a finished answer from refetching.
- `src/ChatMessage.vue` passes a getter to `useQuery` for one question and renders `data` joined into a sentence, with `isFetching` marking the answer as still in progress.
- `src/App.vue` adds a question with a new `crypto.randomUUID()` id on submit, so every question gets its own query.

Expand Down
5 changes: 1 addition & 4 deletions examples/vue/chat/src/services/messages/queries.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
queryOptions,
experimental_streamedQuery as streamedQuery,
} from '@tanstack/vue-query'
import { queryOptions, streamedQuery } from '@tanstack/vue-query'
import { messagesAPI } from './api'

export const messagesQueries = {
Expand Down
4 changes: 2 additions & 2 deletions packages/preact-query/src/__tests__/useSuspenseQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {
QueryCache,
QueryClient,
QueryErrorResetBoundary,
experimental_streamedQuery,
skipToken,
streamedQuery,
useQueryErrorResetBoundary,
useSuspenseInfiniteQuery,
useSuspenseQuery,
Expand Down Expand Up @@ -1071,7 +1071,7 @@ describe('useSuspenseQuery', () => {
function Page() {
const { data } = useSuspenseQuery({
queryKey: key,
queryFn: experimental_streamedQuery({
queryFn: streamedQuery({
streamFn: () => numberGenerator(),
}),
})
Expand Down
2 changes: 1 addition & 1 deletion packages/query-core/src/__tests__/streamedQuery.test-d.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expectTypeOf, it } from 'vitest'
import { streamedQuery } from '../streamedQuery'
import { streamedQuery } from '..'
import type { QueryClient, QueryFunction, QueryKey, QueryMeta } from '..'

function numberStream(): AsyncIterable<number> {
Expand Down
3 changes: 1 addition & 2 deletions packages/query-core/src/__tests__/streamedQuery.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { createKey, sleep } from '@tanstack/query-test-utils'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { QueryClient, QueryObserver } from '..'
import { streamedQuery } from '../streamedQuery'
import { QueryClient, QueryObserver, streamedQuery } from '..'

describe('streamedQuery', () => {
let queryClient: QueryClient
Expand Down
2 changes: 1 addition & 1 deletion packages/query-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export {
} from './utils'
export type { MutationFilters, QueryFilters, SkipToken, Updater } from './utils'

export { streamedQuery as experimental_streamedQuery } from './streamedQuery'
export { streamedQuery } from './streamedQuery'

// Types
export type {
Expand Down
21 changes: 11 additions & 10 deletions packages/query-core/src/streamedQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import type {
import type { FetchSignalContext } from './utils'

type BaseStreamedQueryParams<TQueryFnData, TQueryKey extends QueryKey> = {
/** The function that returns an `AsyncIterable` to stream data from. */
/** Returns an `AsyncIterable` or a promise that resolves to one. */
streamFn: (
context: QueryFunctionContext<TQueryKey>,
) => AsyncIterable<TQueryFnData> | Promise<AsyncIterable<TQueryFnData>>
/**
* Defines how refetches are handled.
* - `'reset'` (default): erases all data and puts the query back into `pending` state.
* - `'reset'` (default): resets the query to its initial state before reading the stream.
* - `'append'`: appends new data to the existing data.
* - `'replace'`: writes all data to the cache once the stream ends.
*/
Expand All @@ -35,13 +35,12 @@ type ReducibleStreamedQueryParams<
TQueryKey extends QueryKey,
> = BaseStreamedQueryParams<TQueryFnData, TQueryKey> & {
/**
* Reduces streamed chunks into the final data shape. Required whenever `TData` is not an
* array, since there is no default way to accumulate non-array chunks.
* Combines the accumulated data with the next chunk. Provide `initialValue` with a custom reducer.
*/
reducer: (acc: TData, chunk: TQueryFnData) => TData
/**
* The value used while the first chunk is being fetched, and returned if the stream yields no
* values. Required together with a custom `reducer`.
* The starting value for the reducer when there is no cached data or when
* `refetchMode` is `'replace'` during a refetch.
*/
initialValue: TData
}
Expand All @@ -51,10 +50,12 @@ type StreamedQueryParams<TQueryFnData, TData, TQueryKey extends QueryKey> =
| ReducibleStreamedQueryParams<TQueryFnData, TData, TQueryKey>

/**
* This is a helper function to create a query function that streams data from an AsyncIterable.
* Data will be an Array of all the chunks received.
* The query will be in a 'pending' state until the first chunk of data is received, but will go to 'success' after that.
* The query will stay in fetchStatus 'fetching' until the stream ends.
* Creates a query function that streams data from an `AsyncIterable`.
* By default, the query collects chunks into an array in the order they arrive.
* If there is no cached data, the first chunk moves the query from `pending` to `success`.
* If the stream ends without producing any chunks, the query uses the cached data or
* the initial value. With a `'replace'` refetch, it uses the initial value.
* The query's `fetchStatus` stays `fetching` while the stream is active.
* Explicit cancellation stops further cache updates, even if `streamFn` does not read `signal`.
* If `streamFn` does not read `signal`, the stream continues after the last observer unsubscribes.
* @param options - The `streamFn` that returns an AsyncIterable to stream data from, and the optional
Expand Down
Loading
Loading