-
Notifications
You must be signed in to change notification settings - Fork 119
Expose the full response from queryFn on utils.queryData #809
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
base: main
Are you sure you want to change the base?
Expose the full response from queryFn on utils.queryData #809
Conversation
Comprehensive investigation of Discord user's feature request to support returning extra pagination info (like total_count) from queryFn in queryCollectionOptions. Key findings: - The full query response IS cached in TanStack Query already - The select function extracts just the array portion - But metadata is not accessible through collection.utils API - Current workaround requires direct queryClient.getQueryData() access Recommended solution: Add queryData property to QueryCollectionUtils to expose the full query response in a reactive, type-safe way. This would enable clean TanStack Table integration with pagination metadata without hacky workarounds like duplicating total_count into every item.
…y response
Implements support for accessing pagination metadata and other API response
info from queryCollectionOptions. Resolves Discord feature request from
Amir Hoseinian about returning extra pagination info.
Changes:
- Add queryData property to QueryCollectionUtils interface
- Store full query response in internal state when query succeeds
- Expose queryData via utils getter for reactive access
- Add comprehensive test coverage (4 new tests)
- Add detailed documentation with examples
The queryData property provides access to the full queryFn response,
including metadata that was previously inaccessible after using select()
to extract the array portion. This enables clean TanStack Table integration
with pagination info and other API metadata.
Example usage:
```typescript
const collection = createCollection(
queryCollectionOptions({
queryFn: async () => ({
data: await api.getContacts(),
total: pagination.total_count
}),
select: (response) => response.data,
// ...
})
)
const totalCount = collection.utils.queryData?.total
```
Benefits:
- Type-safe metadata access
- Reactive updates on refetch
- No need to duplicate metadata into items
- Cleaner API than accessing queryClient directly
- Perfect for server-side pagination with TanStack Table
Tests: All 145 tests passing
Coverage: 86.5% statements, 84.52% branches
🦋 Changeset detectedLatest commit: fb31e81 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 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 |
More templates
@tanstack/angular-db
@tanstack/db
@tanstack/db-ivm
@tanstack/electric-db-collection
@tanstack/offline-transactions
@tanstack/powersync-db-collection
@tanstack/query-db-collection
@tanstack/react-db
@tanstack/rxdb-db-collection
@tanstack/solid-db
@tanstack/svelte-db
@tanstack/trailbase-db-collection
@tanstack/vue-db
commit: |
|
Size Change: 0 B Total Size: 86 kB ℹ️ View Unchanged
|
|
Size Change: 0 B Total Size: 3.34 kB ℹ️ View Unchanged
|
| TKey extends string | number = string | number, | ||
| TInsertInput extends object = TItem, | ||
| TError = unknown, | ||
| TQueryData = any, |
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.
Should this be unknown rather than any?
| lastError: any | ||
| errorCount: number | ||
| lastErrorUpdatedAt: number | ||
| queryData: any |
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.
Shouldn't this be TQueryData instead of any?
| lastError: undefined as any, | ||
| errorCount: 0, | ||
| lastErrorUpdatedAt: 0, | ||
| queryData: undefined as any, |
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.
Do we really need to type it as any?
samwillis
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.
@KyleAMathews note that this exposes the data from the last query function. So when used in on-demand it will be the last response for the most recent loadSubset call. I think we need to consider if this is the right API with the 1:n query:collection design.
| - `isFetching`: Whether the query is currently fetching | ||
| - `isRefetching`: Whether the query is refetching in the background | ||
| - `isLoading`: Whether the query is loading for the first time | ||
| - `dataUpdatedAt`: Timestamp of the last successful data update | ||
| - `fetchStatus`: Current fetch status (`'fetching'`, `'paused'`, or `'idle'`) |
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.
With us having 1:n collection to query the isXX return true when any of the queries are in that state, dataUpdatedAt is the most recent query update time, and fetchStatus is an array of statuses.
Hmm yeah you're right. We need to all download all these ideas about query collection & step back and have a rethink. |
No description provided.