Skip to content
Closed
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
8 changes: 8 additions & 0 deletions .changeset/v6-infinite-fetch-behavior.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@tanstack/query-core': major
'@tanstack/react-query': major
'@tanstack/preact-query': major
'@tanstack/vue-query': major
---

Replace `queryClient.infiniteQuery(options)` with `infiniteQuery(queryClient, options)`, imported from the adapter package or `@tanstack/query-core`. The Vue export accepts reactive options. Infinite queries keep the same cache entries and can still refetch after hydration before an observer mounts. Bundles that use only ordinary queries can omit the pagination implementation.
18 changes: 8 additions & 10 deletions docs/framework/react/guides/prefetching.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,16 @@ Pass `getNextPageParam` when you prefetch an infinite query. On an empty cache,
[//]: # 'ExamplePrefetchInfiniteQuery'

```tsx
import { noop } from '@tanstack/react-query'
import { infiniteQuery, noop } from '@tanstack/react-query'

const prefetchProjects = async () => {
await queryClient
.infiniteQuery({
queryKey: ['projects'],
queryFn: fetchProjects,
initialPageParam: 0,
getNextPageParam: (lastPage, pages) => lastPage.nextCursor,
pages: 3, // prefetch the first 3 pages
})
.catch(noop)
await infiniteQuery(queryClient, {
queryKey: ['projects'],
queryFn: fetchProjects,
initialPageParam: 0,
getNextPageParam: (lastPage) => lastPage.nextCursor,
pages: 3, // prefetch the first 3 pages
}).catch(noop)
}
```

Expand Down
2 changes: 1 addition & 1 deletion docs/framework/vue/guides/prefetching.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ id: prefetching
title: Prefetching
---

If you're lucky enough, you may know enough about what your users will do to be able to prefetch the data they need before it's needed. If this is the case, use `queryClient.query` or `queryClient.infiniteQuery` to warm the cache ahead of time:
If you're lucky enough, you may know enough about what your users will do to be able to prefetch the data they need before it's needed. If this is the case, use `queryClient.query` or `infiniteQuery(queryClient, options)` to warm the cache ahead of time:

[//]: # 'ExamplePrefetching'

Expand Down
4 changes: 2 additions & 2 deletions docs/framework/vue/guides/ssr.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default defineNuxtPlugin((nuxt) => {

Now you are ready to prefetch some data in your pages with `onServerPrefetch`.

- Prefetch all the queries that you need with `queryClient.query`, `queryClient.infiniteQuery`, or `suspense`
- Prefetch all the queries that you need with `queryClient.query`, `infiniteQuery(queryClient, options)`, or `suspense`

```ts
export default defineComponent({
Expand Down Expand Up @@ -112,7 +112,7 @@ Now you are ready to prefetch some data in your pages with `onServerPrefetch`.

- Use `useContext` to get nuxt context
- Use `useQueryClient` to get server-side instance of `queryClient`
- Prefetch all the queries that you need with `queryClient.query`, `queryClient.infiniteQuery`, or `suspense`
- Prefetch all the queries that you need with `queryClient.query`, `infiniteQuery(queryClient, options)`, or `suspense`
- Dehydrate `queryClient` to the `nuxtContext`

```vue
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { QueryClient, dataTagSymbol, skipToken } from '@tanstack/query-core'
import {
QueryClient,
dataTagSymbol,
infiniteQuery,
skipToken,
} from '@tanstack/query-core'
import { createKey } from '@tanstack/query-test-utils'
import { assertType, describe, expectTypeOf, it } from 'vitest'
import { infiniteQueryOptions } from '../infinite-query-options'
Expand Down Expand Up @@ -73,7 +78,7 @@ describe('infiniteQueryOptions', () => {
initialPageParam: 1,
})

const data = await new QueryClient().infiniteQuery(options)
const data = await infiniteQuery(new QueryClient(), options)

expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
})
Expand All @@ -87,7 +92,7 @@ describe('infiniteQueryOptions', () => {
select: (data) => data.pages,
})

const data = await new QueryClient().infiniteQuery(options)
const data = await infiniteQuery(new QueryClient(), options)

expectTypeOf(data).toEqualTypeOf<Array<string>>()
})
Expand All @@ -101,7 +106,7 @@ describe('infiniteQueryOptions', () => {
enabled: false,
})

const data = await new QueryClient().infiniteQuery(options)
const data = await infiniteQuery(new QueryClient(), options)

expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
})
Expand All @@ -114,7 +119,7 @@ describe('infiniteQueryOptions', () => {
initialPageParam: 1,
})

const data = await new QueryClient().infiniteQuery(options)
const data = await infiniteQuery(new QueryClient(), options)

expectTypeOf(data).toEqualTypeOf<InfiniteData<unknown, number>>()
})
Expand Down
6 changes: 3 additions & 3 deletions packages/angular-query/src/infinite-query-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export type DefinedInitialDataInfiniteOptions<
/**
* You can generally pass everything to `infiniteQueryOptions` that you can also pass to
* `injectInfiniteQuery`. These options can be shared across functions and imperative APIs such as
* `queryClient.infiniteQuery`. `options.queryKey` is required and is the query key to generate options
* `infiniteQuery(queryClient, options)`. `options.queryKey` is required and is the query key to generate options
* for.
*
* This overload is selected when `initialData` is known to be defined.
Expand Down Expand Up @@ -200,7 +200,7 @@ export function infiniteQueryOptions<
/**
* You can generally pass everything to `infiniteQueryOptions` that you can also pass to
* `injectInfiniteQuery`. These options can be shared across functions and imperative APIs such as
* `queryClient.infiniteQuery`. `options.queryKey` is required and is the query key to generate options
* `infiniteQuery(queryClient, options)`. `options.queryKey` is required and is the query key to generate options
* for.
* @param options - The {@link UnusedSkipTokenInfiniteOptions} to use β€” everything you can pass to
* `injectInfiniteQuery`.
Expand Down Expand Up @@ -271,7 +271,7 @@ export function infiniteQueryOptions<
/**
* You can generally pass everything to `infiniteQueryOptions` that you can also pass to
* `injectInfiniteQuery`. These options can be shared across functions and imperative APIs such as
* `queryClient.infiniteQuery`. `options.queryKey` is required and is the query key to generate options
* `infiniteQuery(queryClient, options)`. `options.queryKey` is required and is the query key to generate options
* for.
* @param options - The {@link UndefinedInitialDataInfiniteOptions} to use β€” everything you can pass to
* `injectInfiniteQuery`.
Expand Down
4 changes: 2 additions & 2 deletions packages/angular-query/src/inject-infinite-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import type {
* actions, or add conditions like `hasNextPage() && !isFetching()`.
* @see https://tanstack.com/query/latest/docs/framework/angular/guides/infinite-queries
* @see {@link infiniteQueryOptions} to share these options between `injectInfiniteQuery` and imperative APIs
* like `queryClient.infiniteQuery`.
* like `infiniteQuery(queryClient, options)`.
* @example
* ```angular-ts
* @Component({
Expand Down Expand Up @@ -107,7 +107,7 @@ export function injectInfiniteQuery<
* shown below.
* @see https://tanstack.com/query/latest/docs/framework/angular/guides/infinite-queries
* @see {@link infiniteQueryOptions} to share these options between `injectInfiniteQuery` and imperative APIs
* like `queryClient.infiniteQuery`.
* like `infiniteQuery(queryClient, options)`.
* @example
* Fetching the next page from a button click:
* ```angular-ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
QueryClient,
dataTagErrorSymbol,
dataTagSymbol,
infiniteQuery,
skipToken,
} from '@tanstack/query-core'
import { createKey } from '@tanstack/query-test-utils'
Expand Down Expand Up @@ -85,7 +86,7 @@ describe('infiniteQueryOptions', () => {
initialPageParam: 1,
})

const data = await new QueryClient().infiniteQuery(options)
const data = await infiniteQuery(new QueryClient(), options)
expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
})

Expand All @@ -98,7 +99,7 @@ describe('infiniteQueryOptions', () => {
select: (data) => data.pages,
})

const data = await new QueryClient().infiniteQuery(options)
const data = await infiniteQuery(new QueryClient(), options)
expectTypeOf(data).toEqualTypeOf<Array<string>>()
})

Expand All @@ -111,7 +112,7 @@ describe('infiniteQueryOptions', () => {
enabled: false,
})

const data = await new QueryClient().infiniteQuery(options)
const data = await infiniteQuery(new QueryClient(), options)
expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
})

Expand All @@ -123,7 +124,7 @@ describe('infiniteQueryOptions', () => {
initialPageParam: 1,
})

const data = await new QueryClient().infiniteQuery(options)
const data = await infiniteQuery(new QueryClient(), options)
expectTypeOf(data).toEqualTypeOf<InfiniteData<unknown, number>>()
})

Expand Down
21 changes: 12 additions & 9 deletions packages/lit-query/src/__tests__/ssr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

import { render } from '@lit-labs/ssr'
import { collectResult } from '@lit-labs/ssr/lib/render-result.js'
import { QueryCache, QueryClient, noop } from '@tanstack/query-core'
import {
QueryCache,
QueryClient,
infiniteQuery,
noop,
} from '@tanstack/query-core'
import { createKey, sleep } from '@tanstack/query-test-utils'
import { LitElement, html } from 'lit'
import { html as staticHtml, unsafeStatic } from 'lit/static-html.js'
Expand Down Expand Up @@ -143,14 +148,12 @@ describe('Server Side Rendering', () => {
}
}

queryClient
.infiniteQuery({
queryKey: key,
queryFn,
getNextPageParam: () => undefined,
initialPageParam: 0,
})
.catch(noop)
infiniteQuery(queryClient, {
queryKey: key,
queryFn,
getNextPageParam: () => undefined,
initialPageParam: 0,
}).catch(noop)
await vi.advanceTimersByTimeAsync(10)

const markup = await renderToString(Page)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { QueryClient, dataTagSymbol, skipToken } from '@tanstack/query-core'
import {
QueryClient,
dataTagSymbol,
infiniteQuery,
skipToken,
} from '@tanstack/query-core'
import { createKey } from '@tanstack/query-test-utils'
import { assertType, describe, expectTypeOf, it } from 'vitest'
import { infiniteQueryOptions } from '../infiniteQueryOptions'
Expand Down Expand Up @@ -84,7 +89,7 @@ describe('infiniteQueryOptions', () => {
initialPageParam: 1,
})

const data = await new QueryClient().infiniteQuery(options)
const data = await infiniteQuery(new QueryClient(), options)

expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
})
Expand All @@ -98,7 +103,7 @@ describe('infiniteQueryOptions', () => {
select: (data) => data.pages,
})

const data = await new QueryClient().infiniteQuery(options)
const data = await infiniteQuery(new QueryClient(), options)

expectTypeOf(data).toEqualTypeOf<Array<string>>()
})
Expand All @@ -112,7 +117,7 @@ describe('infiniteQueryOptions', () => {
enabled: false,
})

const data = await new QueryClient().infiniteQuery(options)
const data = await infiniteQuery(new QueryClient(), options)

expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
})
Expand All @@ -125,7 +130,7 @@ describe('infiniteQueryOptions', () => {
initialPageParam: 1,
})

const data = await new QueryClient().infiniteQuery(options)
const data = await infiniteQuery(new QueryClient(), options)

expectTypeOf(data).toEqualTypeOf<InfiniteData<unknown, number>>()
})
Expand Down
15 changes: 7 additions & 8 deletions packages/preact-query/src/__tests__/ssr.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { infiniteQuery } from '@tanstack/query-core'
import { createKey, sleep } from '@tanstack/query-test-utils'
import { renderToString } from 'preact-render-to-string'
import { useState } from 'preact/hooks'
Expand Down Expand Up @@ -188,14 +189,12 @@ describe('Server Side Rendering', () => {
)
}

void queryClient
.infiniteQuery({
queryKey: key,
queryFn,
getNextPageParam: () => undefined,
initialPageParam: 0,
})
.catch(noop)
void infiniteQuery(queryClient, {
queryKey: key,
queryFn,
getNextPageParam: () => undefined,
initialPageParam: 0,
}).catch(noop)
await vi.advanceTimersByTimeAsync(10)

const markup = renderToString(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { QueryClient } from '@tanstack/query-core'
import {
QueryClient,
infiniteQuery as fetchInfiniteQuery,
} from '@tanstack/query-core'
import { createKey } from '@tanstack/query-test-utils'
import { describe, expectTypeOf, it } from 'vitest'
import { useInfiniteQuery } from '../useInfiniteQuery'
Expand All @@ -19,7 +22,7 @@ describe('pageParam', () => {

it('should define type of param passed to queryFunctionContext with initialPageParam for infiniteQuery', () => {
const queryClient = new QueryClient()
queryClient.infiniteQuery({
fetchInfiniteQuery(queryClient, {
queryKey: ['key'],
queryFn: ({ pageParam }) => {
expectTypeOf(pageParam).toEqualTypeOf<number>()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { infiniteQuery } from '@tanstack/query-core'
import { createKey, sleep } from '@tanstack/query-test-utils'
import { fireEvent } from '@testing-library/preact'
import { Suspense } from 'preact/compat'
Expand Down Expand Up @@ -111,7 +112,7 @@ describe('usePrefetchInfiniteQuery', () => {
allPages.length < data.length ? allPages.length : undefined,
}

void queryClient.infiniteQuery({ ...queryOpts, pages: 3 }).catch(noop)
void infiniteQuery(queryClient, { ...queryOpts, pages: 3 }).catch(noop)
await vi.advanceTimersByTimeAsync(30)
queryOpts.queryFn.mockClear()

Expand Down
6 changes: 3 additions & 3 deletions packages/preact-query/src/infiniteQueryOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export type DefinedInitialDataInfiniteOptions<

/**
* You can generally pass everything to `infiniteQueryOptions` that you can also pass to `useInfiniteQuery`.
* These options can be shared across hooks and imperative APIs such as `queryClient.infiniteQuery`.
* These options can be shared across hooks and imperative APIs such as `infiniteQuery(queryClient, options)`.
* `options.queryKey` is required and is the query key to generate options for.
*
* This overload is selected when `initialData` is known to be defined.
Expand Down Expand Up @@ -188,7 +188,7 @@ export function infiniteQueryOptions<

/**
* You can generally pass everything to `infiniteQueryOptions` that you can also pass to `useInfiniteQuery`.
* These options can be shared across hooks and imperative APIs such as `queryClient.infiniteQuery`.
* These options can be shared across hooks and imperative APIs such as `infiniteQuery(queryClient, options)`.
* `options.queryKey` is required and is the query key to generate options for.
* @param options - The {@link UnusedSkipTokenInfiniteOptions} to use β€” everything you can pass to `useInfiniteQuery`.
* @returns The same options object, typed so that `queryKey` carries the inferred data type.
Expand Down Expand Up @@ -247,7 +247,7 @@ export function infiniteQueryOptions<

/**
* You can generally pass everything to `infiniteQueryOptions` that you can also pass to `useInfiniteQuery`.
* These options can be shared across hooks and imperative APIs such as `queryClient.infiniteQuery`.
* These options can be shared across hooks and imperative APIs such as `infiniteQuery(queryClient, options)`.
* `options.queryKey` is required and is the query key to generate options for.
* @param options - The {@link UndefinedInitialDataInfiniteOptions} to use β€” everything you can pass to `useInfiniteQuery`.
* @returns The same options object, typed so that `queryKey` carries the inferred data type.
Expand Down
2 changes: 1 addition & 1 deletion packages/preact-query/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export type UsePrefetchQueryOptions<
}

/**
* The options accepted by `usePrefetchInfiniteQuery` β€” everything you can pass to `queryClient.infiniteQuery`,
* The options accepted by `usePrefetchInfiniteQuery` β€” everything you can pass to `infiniteQuery(queryClient, options)`,
* except `queryFn` is required unless a default query function has been defined.
* @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.
* @template TError - The type of errors your `queryFn` may throw.
Expand Down
Loading
Loading