-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
fix(streamedQuery): fall back to initialValue when stream yields no values #9876
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(streamedQuery): fall back to initialValue when stream yields no values #9876
Conversation
…alues Add failing test for empty streams and update implementation to avoid returning undefined. Includes changeset for patch release.
🦋 Changeset detectedLatest commit: 9ac9722 The changes in this PR will be included in the next version bump. This PR includes changesets to release 19 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
WalkthroughA patch-level fix ensures streamedQuery returns the provided Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant C as Caller
participant SQ as streamedQuery
participant S as Stream (async generator)
alt Stream yields chunks
C->>SQ: invoke streamFn
SQ->>S: start stream
S-->>SQ: chunk1
SQ-->>C: emit chunk1
S-->>SQ: chunkN
SQ-->>C: emit chunkN
SQ-->>C: complete
else Stream yields no chunks
C->>SQ: invoke streamFn
SQ->>S: start stream
S-->>SQ: (no items)
SQ-->>C: return initialValue (fallback)
SQ-->>C: complete
end
Note over SQ: Return now uses nullish coalescing to fall back to initialValue
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Suggested reviewers
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🔇 Additional comments (1)
Tip 📝 Customizable high-level summaries are now available!You can now customize how CodeRabbit generates the high-level summary in your pull requests — including its content, structure, tone, and formatting.
Example:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (2)
packages/query-core/src/streamedQuery.ts (1)
44-44: Update documentation to reflect new behavior.The JSDoc for
initialValueshould mention that it's also returned when the stream yields no values, not just "while the first chunk is being fetched."Apply this diff to clarify the documentation:
- * @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.packages/query-core/src/__tests__/streamedQuery.test.tsx (1)
131-160: LGTM! Test correctly validates empty stream handling.The test properly covers the scenario where an
AsyncIterableyields no values, ensuringstreamedQueryreturns theinitialValue(empty array) instead ofundefined.Optional: Consider simplifying the empty stream syntax.
The empty async iterator on line 138 could be more concise:
- const observer = new QueryObserver(queryClient, { - queryKey: key, - queryFn: streamedQuery({ - streamFn: () => { return {async *[Symbol.asyncIterator]() {}}}, - }), - }) + const observer = new QueryObserver(queryClient, { + queryKey: key, + queryFn: streamedQuery({ + streamFn: async function* () {}, + }), + })Also consider removing the blank lines at 132 and 159 for consistency with other tests in the file.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
.changeset/puny-walls-eat.md(1 hunks)packages/query-core/src/__tests__/streamedQuery.test.tsx(1 hunks)packages/query-core/src/streamedQuery.ts(1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📚 Learning: 2025-09-02T17:57:33.184Z
Learnt from: TkDodo
Repo: TanStack/query PR: 9612
File: packages/query-async-storage-persister/src/asyncThrottle.ts:0-0
Timestamp: 2025-09-02T17:57:33.184Z
Learning: When importing from tanstack/query-core in other TanStack Query packages like query-async-storage-persister, a workspace dependency "tanstack/query-core": "workspace:*" needs to be added to the package.json.
Applied to files:
.changeset/puny-walls-eat.md
📚 Learning: 2025-11-02T22:52:33.071Z
Learnt from: DogPawHat
Repo: TanStack/query PR: 9835
File: packages/query-core/src/__tests__/queryClient.test-d.tsx:242-256
Timestamp: 2025-11-02T22:52:33.071Z
Learning: In the TanStack Query codebase, the new `query` and `infiniteQuery` methods support the `select` option for data transformation, while the legacy `fetchQuery` and `fetchInfiniteQuery` methods do not support `select` and should reject it at the type level.
Applied to files:
.changeset/puny-walls-eat.mdpackages/query-core/src/streamedQuery.ts
🧬 Code graph analysis (1)
packages/query-core/src/__tests__/streamedQuery.test.tsx (1)
packages/query-core/src/streamedQuery.ts (1)
streamedQuery(46-99)
🔇 Additional comments (2)
packages/query-core/src/streamedQuery.ts (1)
97-97: LGTM! Correct fix for empty streams.The nullish coalescing operator properly handles the case where
getQueryDatareturnsundefined(when the stream yields no values and nothing was written to cache), falling back toinitialValueas expected..changeset/puny-walls-eat.md (1)
1-5: LGTM! Changeset is well-documented.The patch-level bump is appropriate for this bug fix, and the description clearly explains the change.
TkDodo
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please also update the docs:
query/docs/reference/streamedQuery.md
Lines 41 to 45 in b25412a
| - `initialValue?: TData = TQueryFnData` | |
| - Optional | |
| - Defines the initial data to be used while the first chunk is being fetched. | |
| - It is mandatory when custom `reducer` is provided. | |
| - Defaults to an empty array. |
|
View your CI Pipeline Execution ↗ for commit 9ac9722
☁️ Nx Cloud last updated this comment at |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #9876 +/- ##
===========================================
+ Coverage 45.78% 59.81% +14.03%
===========================================
Files 200 129 -71
Lines 8413 5627 -2786
Branches 1923 1539 -384
===========================================
- Hits 3852 3366 -486
+ Misses 4113 1956 -2157
+ Partials 448 305 -143 🚀 New features to boost your workflow:
|
Add failing test for empty streams and update implementation to avoid returning undefined. Includes changeset for patch release.
🎯 Changes
should handle empty streams) that reproduces the issue wherestreamedQuerythrows when the underlying AsyncIterable yields no values.streamedQueryimplementation to fall back toinitialValuewhengetQueryDatareturnsundefined.@tanstack/query-core.✅ Checklist
pnpm run test:pr.🚀 Release Impact
Summary by CodeRabbit
Bug Fixes
Tests
Documentation
Chores