Skip to content
Open
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/solid-query-status-resource.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tanstack/solid-query": patch
---

Start Solid query resources when status fields are read so curried `queryOptions` fetch on mount without requiring `data` access.
67 changes: 67 additions & 0 deletions packages/solid-query/src/__tests__/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
QueryClient,
QueryClientProvider,
keepPreviousData,
queryOptions,
useQuery,
} from '..'
import { Blink, mockOnlineManagerIsOnline, setActTimeout } from './utils'
Expand Down Expand Up @@ -244,6 +245,72 @@ describe('useQuery', () => {
expect(rendered.getByText('test')).toBeInTheDocument()
})

it('should fetch when a curried queryOptions result only reads status fields', async () => {
const key = queryKey()
const queryFn = vi.fn((slug: string) => `test-${slug}`)
const fetchQueryOptions = (slug: string) =>
queryOptions({
queryKey: [key, slug],
queryFn: () => queryFn(slug),
})

function Page() {
const options = fetchQueryOptions('slug')
const state = useQuery(() => options)

return (
<Switch>
<Match when={state.isPending}>pending</Match>
<Match when={state.isError}>error</Match>
<Match when={state.isSuccess}>success</Match>
</Switch>
)
}

const rendered = render(() => (
<QueryClientProvider client={queryClient}>
<Page />
</QueryClientProvider>
))

expect(rendered.getByText('pending')).toBeInTheDocument()
await vi.advanceTimersByTimeAsync(10)
expect(queryFn).toHaveBeenCalledTimes(1)
expect(rendered.getByText('success')).toBeInTheDocument()
})

it('should fetch when a curried queryOptions result only reads resource fields', async () => {
const key = queryKey()
const queryFn = vi.fn((slug: string) => `test-${slug}`)
const fetchQueryOptions = (slug: string) =>
queryOptions({
queryKey: [key, slug],
queryFn: () => queryFn(slug),
})

function Page() {
const options = fetchQueryOptions('slug')
const state = useQuery(() => options)

void state.error
void state.failureReason
void state.refetch
void state.promise

return <div>mounted</div>
}

const rendered = render(() => (
<QueryClientProvider client={queryClient}>
<Page />
</QueryClientProvider>
))

expect(rendered.getByText('mounted')).toBeInTheDocument()
await vi.advanceTimersByTimeAsync(10)
expect(queryFn).toHaveBeenCalledTimes(1)
})

it('should return the correct states for a successful query', async () => {
const key = queryKey()
const states: Array<UseQueryResult<string>> = []
Expand Down
33 changes: 33 additions & 0 deletions packages/solid-query/src/useBaseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,34 @@ const hydratableObserverResult = <
return obj
}

const resourceTrackingProps = new Set<PropertyKey>([
'dataUpdatedAt',
'error',
'errorUpdatedAt',
'failureCount',
'failureReason',
'errorUpdateCount',
'isError',
'isFetched',
'isFetchedAfterMount',
'isFetching',
'isLoading',
'isPending',
'isLoadingError',
'isInitialLoading',
'isPaused',
'isPlaceholderData',
'isRefetchError',
'isRefetching',
'isStale',
'isSuccess',
'isEnabled',
'refetch',
'promise',
'status',
'fetchStatus',
])
Comment thread
coderabbitai[bot] marked this conversation as resolved.

// Base Query Function that is used to create the query.
export function useBaseQuery<
TQueryFnData,
Expand Down Expand Up @@ -381,6 +409,11 @@ export function useBaseQuery<
}
return queryResource()?.data
}
if (resourceTrackingProps.has(prop)) {
// Solid resources are lazy, so status-only consumers still need to read
// the resource once to start the observer subscription and fetch.
queryResource()
}
return Reflect.get(target, prop)
},
}
Expand Down