-
Notifications
You must be signed in to change notification settings - Fork 245
feat(query-db-collection): support more top-level query options #1665
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@tanstack/query-db-collection': minor | ||
| --- | ||
|
|
||
| Add top-level Query Collection support for additional Query observer options while preserving QueryClient defaultOptions behavior. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,9 +56,9 @@ The `queryCollectionOptions` function accepts the following options: | |
|
|
||
| ### Query Options | ||
|
|
||
| Query Collections use TanStack Query internally, but `queryCollectionOptions` is not a full `QueryObserverOptions` pass-through. It exposes only the Query options supported by the collection adapter. Fields that affect row materialization, collection identity, and synchronization are handled by the adapter itself. | ||
| Query Collections use TanStack Query internally and expose supported Query observer options as top-level `queryCollectionOptions` fields. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Docs update should be a separate PR that we merge when these features are released.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll do a release right away |
||
|
|
||
| The following Query options are forwarded to the underlying Query observer: | ||
| The following top-level Query Collection options are forwarded to the underlying Query observer: | ||
|
|
||
| - `select`: Function that extracts the row array TanStack DB materializes from a wrapped Query response | ||
| - `enabled`: Whether the query should automatically run (default: `true`) | ||
|
|
@@ -67,9 +67,28 @@ The following Query options are forwarded to the underlying Query observer: | |
| - `retryDelay`: Delay between retries | ||
| - `staleTime`: How long data is considered fresh | ||
| - `gcTime`: How long unused query data stays in the Query cache | ||
| - `refetchOnWindowFocus`: Whether to refetch when the window regains focus | ||
| - `refetchOnReconnect`: Whether to refetch when the network reconnects | ||
| - `refetchOnMount`: Whether to refetch when the observer mounts | ||
| - `networkMode`: Query network mode | ||
| - `meta`: Metadata passed to the query function context. Query Collections may add `loadSubsetOptions` for on-demand queries. | ||
|
|
||
| Except for `meta`, these options are only passed to TanStack Query when you define them. If you omit them, `QueryClient.defaultOptions` can still apply. | ||
| ```ts | ||
| const todosCollection = createCollection( | ||
| queryCollectionOptions({ | ||
| queryKey: ["todos"], | ||
| queryFn: fetchTodos, | ||
| queryClient, | ||
| getKey: (todo) => todo.id, | ||
| refetchOnWindowFocus: true, | ||
| refetchOnReconnect: true, | ||
| refetchOnMount: "always", | ||
| networkMode: "online", | ||
| }) | ||
| ) | ||
| ``` | ||
|
|
||
| Top-level `meta` is always merged by Query Collection so it can add on-demand `loadSubsetOptions`. Other supported top-level Query options are only passed to TanStack Query when you define them. If you omit them, `QueryClient.defaultOptions` can still apply. | ||
|
|
||
| Some fields are owned or reinterpreted by the collection adapter rather than treated as ordinary Query option pass-through: | ||
|
|
||
|
|
@@ -81,20 +100,19 @@ Some fields are owned or reinterpreted by the collection adapter rather than tre | |
| - `getKey`: Extracts each row's stable TanStack DB key. | ||
| - Mutation handlers such as `onInsert`, `onUpdate`, and `onDelete`. | ||
|
|
||
| Other TanStack Query options are not currently exposed through `queryCollectionOptions`. Common examples include: | ||
| Some TanStack Query fields are owned or reinterpreted by Query Collection and are intentionally not exposed as ordinary Query observer options: | ||
|
|
||
| - `queryKey`, `queryFn`, and `queryClient` | ||
| - `select` (Query Collection uses this for row extraction, not TanStack Query's observer-level `select` contract) | ||
| - `meta` (merged by Query Collection so on-demand `loadSubsetOptions` can be included) | ||
| - `subscribed` (Query Collection owns the observer subscription lifecycle) | ||
| - `structuralSharing` and `notifyOnChangeProps` (managed by Query Collection synchronization) | ||
|
|
||
| - `initialData` | ||
| - `placeholderData` | ||
| - `refetchOnWindowFocus` | ||
| - `refetchOnReconnect` | ||
| - `refetchOnMount` | ||
| - `networkMode` | ||
| - `throwOnError` | ||
| - configurable `structuralSharing` | ||
| Other semantic options, such as `initialData`, `placeholderData`, and TanStack Query observer-level `select`, are intentionally deferred until their Query Collection behavior is explicitly designed. | ||
|
|
||
| ### Using with `queryOptions(...)` | ||
|
|
||
| If your app already uses TanStack Query's `queryOptions` helper (e.g. from `@tanstack/react-query`), you can spread those options into `queryCollectionOptions`. Note that `queryFn` must be explicitly provided since query collections require it both in types and at runtime: | ||
| If your app already uses TanStack Query's `queryOptions` helper (e.g. from `@tanstack/react-query`), you can spread compatible top-level options into `queryCollectionOptions`. Note that `queryFn` must be explicitly provided since query collections require it both in types and at runtime, and Query Collection's `select` option is for row extraction rather than TanStack Query observer-level selection: | ||
|
|
||
| ```typescript | ||
| import { QueryClient } from "@tanstack/query-core" | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
minor bump on purpose?
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.
query-db-collection got bumped accidentally a while to 1.0 so it's fine here