Skip to Content
Effetch Query
Queries

Infinite queries

Page through a list, and load more pages on demand.

An infinite query loads a list one page at a time, for "load more" buttons and infinite scroll. Its value is every page loaded so far, with the param each was fetched with, as an InfiniteQuery.InfiniteData: { pages, pageParams }. After this page, you can page through an endpoint or any Effect, and show the pages in a component.

The examples import api.ts and todos.ts from Getting started.

From an endpoint. api.infiniteQuery pages through an Effetch endpoint. pageParam names the query field each page's param is sent in:

activity.ts
import * as  from "effect/Schema";

import {  } from "@monarcode/effetch";

import {  } from "./todos.js";

const  = .({
  : .(
    .({ : ., : . }),
  ),
  : .(.),
});

export const  = .(
  .("GET /activity", { :  }),
  {
    // Sent as ?cursor=…, and left out of the key.
    : "cursor",
    : null as string | null,
    // null or undefined: there's no next page.
    : () => .,
  },
);
// activity({ query: { team: "web" } }) loads its first page from
// GET /activity?team=web, and the next from ?team=web&cursor=…

The page field is set on every page's request, over any value the call gives for it, and left out of the key, so every page belongs to one query. The param must be a value a query field takes, such as a string or a number. If the endpoint has a query schema, pageParam names one of its optional fields whose type takes the param, so a call can leave it out.

Without endpoints. InfiniteQuery.make defines one infinite query, and InfiniteQuery.family one per argument. Their run fetches one page from its param; a family's takes the member's argument first. This one loads todos by offset, in both directions:

import * as  from "effect/Schema";

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

import { ,  } from "./api.js";

const  = .({
  : .(),
  : .,
  : .,
  : .,
});

export const  = .({
  : ["todos", "pages"],
  : 0,
  // The member's argument, then the page's param.
  : (: number, ) =>
    .(() =>
      .("/todos", { : { ,  }, :  }),
    ),
  : () =>
    . + . < . ? . + . : ,
  // Lets the list load pages before the first, and reload dropped ones.
  : () =>
    . > 0 ? .(0, . - .) : ,
  : 10,
  // Only for server rendering: how each page crosses to the browser.
  : { :  },
});
// todoPages(20) is the infinite query keyed ["todos", "pages", 20]

nextPageParam and previousPageParam are InfiniteQuery.PageParamFunctions: they get a page, every page, that page's param and every param, and return undefined or null when there's no page in that direction. An endpoint with an output makes infinite queries that cross to the browser with no schema to write, as api.query's do; with make and family, give schema a page schema, and an error schema to send failures too. The page params go as they are, so they must be JSON values (strings, numbers, booleans, null) unless schema.pageParam gives their schema.

How pages load. The first fetch loads one page, from initialPageParam. fetchNextPage (from useInfiniteQuery, or InfiniteQuery.fetchNextPage in Effect code) adds the next one, and fetchPreviousPage adds the one before the first:

  • On the last page, it returns the pages without fetching. Without previousPageParam, there's never a previous page.
  • A page that fails keeps the loaded pages: the result is a Failure whose previousSuccess still holds them.
  • Past maxPages, a page is dropped from the far end: the first when a next page loads, the last when a previous page does.
  • While the next page is loading, another fetchNextPage joins that fetch. While any other fetch of the query is in progress, it waits for it, then fetches its page.
  • A write made while a page loads stays: the page is added to the data shown when it lands.
  • A refetch (after an invalidation, on focus, or when a component starts using the query while it's stale) fetches as many pages as are loaded, one after the other, each from the page before it, so a list that changed underneath doesn't show duplicates or gaps.

In Effect code, InfiniteQuery.fetchNextPage and InfiniteQuery.fetchPreviousPage return the updated { pages, pageParams }, and fail with the query's error if the page fails:

import * as  from "effect/Effect";

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

import {  } from "./activity.js";

export const  = .(
  .(({ : { : "web" } })),
  () => ..(() => .),
);

An infinite query is a query: invalidation, the cache functions and the client's defaults work on it as on any other. To change its pages in an optimistic update, Query.update the whole { pages, pageParams } value; on a family, Query.update writes every cached member. Entities find a record in every loaded page.

An infinite query takes the following options. The paging ones, from initialPageParam to maxPages, are its InfiniteQuery.PageOptions:

OptionWhat it does
pageParamThe query field each page's param is sent in (Effetch only).
initialPageParamThe first page's param.
nextPageParam(lastPage, pages, lastPageParam, pageParams) => param: the next page's param, or undefined / null for none.
previousPageParamThe same, from the first page, for fetchPreviousPage. Without it, there's no previous page.
maxPagesThe most pages to keep. Past it, a page is dropped from the far end; give previousPageParam too, so it can load again.
schema{ page, pageParam?, error? }: how the pages cross to the browser in server rendering (make, family).
Query optionsAs in Query options, such as staleTime and refetchOnFocus. retry applies to each page.

Infinite queries don't take placeholder or live.

Load more

useInfiniteQuery reads an infinite query, with controls for the next and previous pages. useSuspenseInfiniteQuery suspends only until the first page:

import {  } from "@monarcode/effetch-query/react";

import {  } from "./activity.js";

export function () {
  const { , , ,  } =
    (({ : { : "web" } }));

  return (
    <>
      <>
        {..
          .(() => .)
          .(() => (
            < ={.}>{.}</>
          ))}
      </>
      { ? (
        <
          ={}
          ={() => void ()}
        >
          { ? "Loading…" : "Load more"}
        </>
      ) : null}
    </>
  );
}

The loaded pages stay on screen while the next one loads. fetchNextPage never rejects: a page that fails shows in result. In React Native, call it from FlatList's onEndReached.

Both hooks return result beside the following page controls:

ControlWhat it is
fetchNextPageFetches the page after the last one. Resolves when it settles, and never rejects.
fetchPreviousPageFetches the page before the first one, in the same way.
hasNextPageWhether nextPageParam gives a param for the shown pages.
hasPreviousPageWhether previousPageParam gives a param for the shown pages.
isFetchingNextPageWhether a next page is being fetched, as opposed to a refetch of every page.
isFetchingPreviousPageWhether a previous page is being fetched.

While a page loads, result stays a waiting Success, so isFetchingNextPage is how you tell a page fetch from a background refetch of every page.

Without Suspense. useInfiniteQuery's result is an AsyncResult, as useQuery returns, and it fetches when useQuery does. Pass null to read nothing. It takes useQuery's keepPrevious and paused options (Pause screens you can't see); a paused reader's page functions still fetch pages when called.

With Suspense. A page that fails after the first isn't thrown, since there are pages to show. Only a failure with no pages before it reaches the error boundary, as a QueryFailure (Suspense and error boundaries).

On this page