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/puny-walls-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/query-core': patch
---

Fix streamedQuery to avoid returning undefined when the stream yields no values
2 changes: 1 addition & 1 deletion docs/reference/streamedQuery.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ const query = queryOptions({
- If `TData` is not an array, you must provide a custom `reducer`.
- `initialValue?: TData = TQueryFnData`
- Optional
- Defines the initial data to be used while the first chunk is being fetched.
- Defines the initial data to be used while the first chunk is being fetched, and it is also returned when the stream yields no values.
- It is mandatory when custom `reducer` is provided.
- Defaults to an empty array.
29 changes: 29 additions & 0 deletions packages/query-core/src/__tests__/streamedQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,35 @@ describe('streamedQuery', () => {
unsubscribe()
})

test('should handle empty streams', async () => {
const key = queryKey()

const observer = new QueryObserver(queryClient, {
queryKey: key,
queryFn: streamedQuery({
streamFn: async function* () {},
}),
})

const unsubscribe = observer.subscribe(vi.fn())

expect(observer.getCurrentResult()).toMatchObject({
status: 'pending',
fetchStatus: 'fetching',
data: undefined,
})

await vi.advanceTimersByTimeAsync(50)

expect(observer.getCurrentResult()).toMatchObject({
status: 'success',
fetchStatus: 'idle',
data: [],
})

unsubscribe()
})

test('should replace on refetch', async () => {
const key = queryKey()
const observer = new QueryObserver(queryClient, {
Expand Down
4 changes: 2 additions & 2 deletions packages/query-core/src/streamedQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type StreamedQueryParams<TQueryFnData, TData, TQueryKey extends QueryKey> =
* Set to `'replace'` to write all data to the cache once the stream ends.
* @param reducer - A function to reduce the streamed chunks into the final data.
* Defaults to a function that appends chunks to the end of the array.
* @param initialValue - Initial value to be used while the first chunk is being fetched.
* @param initialValue - Initial value to be used while the first chunk is being fetched, and returned if the stream yields no values.
*/
export function streamedQuery<
TQueryFnData = unknown,
Expand Down Expand Up @@ -94,6 +94,6 @@ export function streamedQuery<
context.client.setQueryData<TData>(context.queryKey, result)
}

return context.client.getQueryData(context.queryKey)!
return context.client.getQueryData(context.queryKey) ?? initialValue
}
}
Loading