# InfiniteQuery (https://effetch-query.monarcode.dev/docs/reference/infinite-query)

`InfiniteQuery` defines queries that load a list one page at a time, and loads more pages. Import it from the core entry point:

```ts
import { InfiniteQuery } from "@monarcode/effetch-query";
```

Its exports follow, grouped by the guide that covers them, each with its
docs. Your editor shows the same docs, with an example, when you hover the
name.

## Infinite queries

Guide: [Infinite queries](https://effetch-query.monarcode.dev/docs/queries/infinite).

### InfiniteQuery.make

Defines an [`InfiniteQuery`](https://effetch-query.monarcode.dev/docs/reference/infinite-query#infinitequery) with no arguments. Nothing is fetched until it's used; then its first page, from `initialPageParam`.

### InfiniteQuery.family

Defines a [`Family`](https://effetch-query.monarcode.dev/docs/reference/infinite-query#family-type): one [`InfiniteQuery`](https://effetch-query.monarcode.dev/docs/reference/infinite-query#infinitequery) per argument, each with its own pages.

### InfiniteQuery.isInfiniteQuery

Checks whether a value is an [`InfiniteQuery`](https://effetch-query.monarcode.dev/docs/reference/infinite-query#infinitequery), as opposed to a plain query or another atom.

### InfiniteQuery.TypeId

The property that marks an [`InfiniteQuery`](https://effetch-query.monarcode.dev/docs/reference/infinite-query#infinitequery), so it can be told apart from other queries.

### InfiniteQuery.Family

Makes one [`InfiniteQuery`](https://effetch-query.monarcode.dev/docs/reference/infinite-query#infinitequery) per argument, as [`Query.family`](https://effetch-query.monarcode.dev/docs/reference/query#family) does: the same query for arguments that are `Equal.equals`, keyed by the family's key with the argument appended. It's a [`Query.Family`](https://effetch-query.monarcode.dev/docs/reference/query#family-type), so it's a target for [`Query.invalidate`](https://effetch-query.monarcode.dev/docs/reference/query#invalidate) (every query under its key), and [`Query.update`](https://effetch-query.monarcode.dev/docs/reference/query#update) writes every cached member.

| Prop  | Type  | Description                             |
| ----- | ----- | --------------------------------------- |
| `key` | `Key` | The key every member's key starts with. |

### InfiniteQuery.FamilyOptions

Options for [`family`](https://effetch-query.monarcode.dev/docs/reference/infinite-query#family).

| Prop                  | Type                               | Description                                                                                                                                                                                                                                                                                                             |
| --------------------- | ---------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `reactivityKeys?`     | `union`                            | The `Reactivity` keys that refetch this query when invalidated, for Effect code that publishes them (Effect's SQL clients do). Run `Reactivity.invalidate` through the client, as QueryClient.QueryClient.runPromise does, to reach it. Default: none.                                                                  |
| `entities?`           | `object`                           | How this query takes part in the client's entity sync (QueryClient.EntitiesOptions.sync). Default: it does, if it has a `schema`.                                                                                                                                                                                       |
| `key`                 | `Key`                              | The family's key. Each member appends its argument.                                                                                                                                                                                                                                                                     |
| `run`                 | `function`                         | Fetches one page of one member from its argument and the page's param. It runs with your services and the client, and its `Scope` closes when the page settles.                                                                                                                                                         |
| `schema?`             | `object`                           | How each member's pages cross from server to browser, as MakeOptions.schema.                                                                                                                                                                                                                                            |
| `staleTime?`          | `union`                            | How long a success counts as fresh. Default: 0, stale at once.                                                                                                                                                                                                                                                          |
| `retry?`              | `object`                           | Retries a failed fetch on this schedule, which gets each failure. Defects aren't retried. Default: no retries.                                                                                                                                                                                                          |
| `refetchOnMount?`     | `union`                            | If true, refetches a stale query when a component starts using it. Default: `true`.                                                                                                                                                                                                                                     |
| `retryOnMount?`       | `union`                            | If true, refetches a failed query when a component starts using it, however recent the failure. A suspense reader (React's `useSuspenseQuery`) of a failure with no earlier value throws it to an error boundary instead, so it never starts using the query to retry it (see `useResetQueryFailure`). Default: `true`. |
| `refetchOnFocus?`     | `union`                            | If true, refetches a stale query in use when the app regains focus (CommonOptions.signals). Default: `true`.                                                                                                                                                                                                            |
| `refetchOnReconnect?` | `union`                            | If true, refetches a stale query in use when the device reconnects (CommonOptions.signals). Default: `true`.                                                                                                                                                                                                            |
| `refetchInterval?`    | `union`                            | The interval at which a query in use refetches. A tick that finds a fetch in progress doesn't start another. Polling pauses while nothing uses the query and resumes when something starts using it. Default: no polling.                                                                                               |
| `gcTime?`             | `union`                            | How long a query nothing uses stays cached. Default: 5 minutes. Like `reactivityKeys` and `entities`, it has no client-wide default.                                                                                                                                                                                    |
| `initialPageParam`    | `P`                                | The first page's param.                                                                                                                                                                                                                                                                                                 |
| `nextPageParam`       | `PageParamFunction<A, NoInfer<P>>` | The param of the page after the last one, or `undefined` / `null` for none.                                                                                                                                                                                                                                             |
| `previousPageParam?`  | `PageParamFunction<A, NoInfer<P>>` | The param of the page before the first one, or `undefined` / `null` for none. Without it, there's no previous page.                                                                                                                                                                                                     |
| `maxPages?`           | `number`                           | The most pages to keep. Past it, a page is dropped from the far end: the first when a next page loads, the last when a previous page does. Give `previousPageParam` too, so the dropped pages can load again. Default: no limit.                                                                                        |

### InfiniteQuery.InfiniteData

What an [`InfiniteQuery`](https://effetch-query.monarcode.dev/docs/reference/infinite-query#infinitequery) holds: the pages loaded so far, in order, and the param each was fetched with.

| Prop         | Type    | Description                                              |
| ------------ | ------- | -------------------------------------------------------- |
| `pages`      | `array` | The pages, first to last.                                |
| `pageParams` | `array` | The param each page was fetched with, in the same order. |

### InfiniteQuery.InfiniteQuery

A query whose value is a list of pages, for "load more" lists and infinite scroll, as TanStack's `useInfiniteQuery`. Create one with [`make`](https://effetch-query.monarcode.dev/docs/reference/infinite-query#make), or one per argument with [`family`](https://effetch-query.monarcode.dev/docs/reference/infinite-query#family).

It **is** a [`Query.Query`](https://effetch-query.monarcode.dev/docs/reference/query#query) of [`InfiniteData`](https://effetch-query.monarcode.dev/docs/reference/infinite-query#infinitedata), so the whole cache API works on it: [`Query.invalidate`](https://effetch-query.monarcode.dev/docs/reference/query#invalidate) and [`Query.refetch`](https://effetch-query.monarcode.dev/docs/reference/query#refetch) fetch its pages again, [`Query.set`](https://effetch-query.monarcode.dev/docs/reference/query#set) and [`Query.update`](https://effetch-query.monarcode.dev/docs/reference/query#update) write them (optimistic writes too). [`fetchNextPage`](https://effetch-query.monarcode.dev/docs/reference/infinite-query#fetchnextpage) and [`fetchPreviousPage`](https://effetch-query.monarcode.dev/docs/reference/infinite-query#fetchpreviouspage) add a page at either end.

A refetch fetches as many pages as are loaded, one after the other, and shows them together: the first from its stored param, each later one from `nextPageParam` of the page before it, so cursors stay consistent when the list has changed. A page that fails keeps the loaded pages: the query becomes a failure whose `previousSuccess` holds them.

### InfiniteQuery.MakeOptions

Options for [`make`](https://effetch-query.monarcode.dev/docs/reference/infinite-query#make): a query's options and its page options. An infinite query takes no `placeholder` or `live`.

| Prop                  | Type                               | Description                                                                                                                                                                                                                                                                                                             |
| --------------------- | ---------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `key`                 | `Key`                              | The query's key.                                                                                                                                                                                                                                                                                                        |
| `run`                 | `function`                         | Fetches one page from its param. It runs with your services and the client, and its `Scope` closes when the page settles.                                                                                                                                                                                               |
| `schema?`             | `object`                           | How the pages cross from server to browser; without one, they fetch in the browser.                                                                                                                                                                                                                                     |
| `staleTime?`          | `union`                            | How long a success counts as fresh. Default: 0, stale at once.                                                                                                                                                                                                                                                          |
| `retry?`              | `object`                           | Retries a failed fetch on this schedule, which gets each failure. Defects aren't retried. Default: no retries.                                                                                                                                                                                                          |
| `refetchOnMount?`     | `union`                            | If true, refetches a stale query when a component starts using it. Default: `true`.                                                                                                                                                                                                                                     |
| `retryOnMount?`       | `union`                            | If true, refetches a failed query when a component starts using it, however recent the failure. A suspense reader (React's `useSuspenseQuery`) of a failure with no earlier value throws it to an error boundary instead, so it never starts using the query to retry it (see `useResetQueryFailure`). Default: `true`. |
| `refetchOnFocus?`     | `union`                            | If true, refetches a stale query in use when the app regains focus (CommonOptions.signals). Default: `true`.                                                                                                                                                                                                            |
| `refetchOnReconnect?` | `union`                            | If true, refetches a stale query in use when the device reconnects (CommonOptions.signals). Default: `true`.                                                                                                                                                                                                            |
| `refetchInterval?`    | `union`                            | The interval at which a query in use refetches. A tick that finds a fetch in progress doesn't start another. Polling pauses while nothing uses the query and resumes when something starts using it. Default: no polling.                                                                                               |
| `gcTime?`             | `union`                            | How long a query nothing uses stays cached. Default: 5 minutes. Like `reactivityKeys` and `entities`, it has no client-wide default.                                                                                                                                                                                    |
| `reactivityKeys?`     | `union`                            | The `Reactivity` keys that refetch this query when invalidated, for Effect code that publishes them (Effect's SQL clients do). Run `Reactivity.invalidate` through the client, as QueryClient.QueryClient.runPromise does, to reach it. Default: none.                                                                  |
| `entities?`           | `object`                           | How this query takes part in the client's entity sync (QueryClient.EntitiesOptions.sync). Default: it does, if it has a `schema`.                                                                                                                                                                                       |
| `initialPageParam`    | `P`                                | The first page's param.                                                                                                                                                                                                                                                                                                 |
| `nextPageParam`       | `PageParamFunction<A, NoInfer<P>>` | The param of the page after the last one, or `undefined` / `null` for none.                                                                                                                                                                                                                                             |
| `previousPageParam?`  | `PageParamFunction<A, NoInfer<P>>` | The param of the page before the first one, or `undefined` / `null` for none. Without it, there's no previous page.                                                                                                                                                                                                     |
| `maxPages?`           | `number`                           | The most pages to keep. Past it, a page is dropped from the far end: the first when a next page loads, the last when a previous page does. Give `previousPageParam` too, so the dropped pages can load again. Default: no limit.                                                                                        |

### InfiniteQuery.PageOptions

How an infinite query finds its pages, in [`MakeOptions`](https://effetch-query.monarcode.dev/docs/reference/infinite-query#makeoptions) and [`FamilyOptions`](https://effetch-query.monarcode.dev/docs/reference/infinite-query#familyoptions).

| Prop                 | Type                               | Description                                                                                                                                                                                                                      |
| -------------------- | ---------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `initialPageParam`   | `P`                                | The first page's param.                                                                                                                                                                                                          |
| `nextPageParam`      | `PageParamFunction<A, NoInfer<P>>` | The param of the page after the last one, or `undefined` / `null` for none.                                                                                                                                                      |
| `previousPageParam?` | `PageParamFunction<A, NoInfer<P>>` | The param of the page before the first one, or `undefined` / `null` for none. Without it, there's no previous page.                                                                                                              |
| `maxPages?`          | `number`                           | The most pages to keep. Past it, a page is dropped from the far end: the first when a next page loads, the last when a previous page does. Give `previousPageParam` too, so the dropped pages can load again. Default: no limit. |

### InfiniteQuery.PageParamFunction

Finds the param of the page after the last one (`nextPageParam`) or before the first (`previousPageParam`), from that page, every page, its param and every param. Return `undefined` or `null` when there's no such page, as in TanStack.

## Load more

Guide: [Load more](https://effetch-query.monarcode.dev/docs/queries/infinite#load-more).

### InfiniteQuery.fetchNextPage

Fetches the page after the last one and appends it, dropping the first page past `maxPages`. Returns the new [`InfiniteData`](https://effetch-query.monarcode.dev/docs/reference/infinite-query#infinitedata), and fails with the query's error if the page fails (the loaded pages stay).

- With no next page (`nextPageParam` gives `undefined` or `null`), it returns the data as it is, without fetching.
- Without data, it's the query's first fetch, and joins it while it runs. With no pages (a write left none), it fetches from the first page, as TanStack does.
- While a next page is being fetched, it joins that fetch; while any other fetch of the query runs, it waits for it, then fetches its page.
- A write made while the page loads stays: the page is added to the data shown when it lands.

### InfiniteQuery.fetchPreviousPage

Fetches the page before the first one and prepends it, dropping the last page past `maxPages`. Behaves as [`fetchNextPage`](https://effetch-query.monarcode.dev/docs/reference/infinite-query#fetchnextpage) does, in the other direction; without `previousPageParam` there's never a previous page.

## Send the cache

Guide: [Send the cache](https://effetch-query.monarcode.dev/docs/server-rendering).

### InfiniteQuery.Schema

How an infinite query's pages are sent from the server to the browser, as [`Query.Schema`](https://effetch-query.monarcode.dev/docs/reference/query#schema) for a query: Schemas for a page and, to send failures too, the error. The page params go as they are, so they must be JSON values (strings, numbers, booleans, `null`), unless `pageParam` names their schema; a query whose params don't fit isn't sent.

| Prop         | Type     | Description                                                  |
| ------------ | -------- | ------------------------------------------------------------ |
| `page`       | `object` | A page's schema.                                             |
| `pageParam?` | `object` | The page params' schema, for params that aren't JSON values. |
| `error?`     | `object` | The error's schema, needed only to send failures.            |