TanStack / TanStack/db

Errors don't propagate to new on-demand live queries after an earlier request succeeds

Open
#1,764 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
3.9k
Forks
266
Avg merge
1d 4h
Merged PRs (30d)
55

Description

  • [x ] I've validated the bug against the latest version of DB packages

Describe the bug
The initial error-propagation issue in #1260 (https://github.com/TanStack/db/issues/1260) was recently resolved. While testing that fix with syncMode: 'on-demand', I found a further problem with that implementation.
After one filtered request succeeds, subsequent live queries that request different subsets do not enter the error state when their requests fail. This is a common use case for on-demand collections—for example, fetching different date ranges or ID ranges.

I would expect an error from a later filtered request to propagate to that specific live query, while existing live queries with usable data remain ready.

Tested with

  • @tanstack/db: 0.8.3
  • @tanstack/query-core: 5.102.2
  • @tanstack/query-db-collection: 1.2.8

To Reproduce

  import { QueryClient } from '@tanstack/query-core';
  import { createCollection, createLiveQueryCollection, eq, parseLoadSubsetOptions } from '@tanstack/db';
  import { queryCollectionOptions } from '@tanstack/query-db-collection';

  const successfulTodoId = 'successful';
  const failingTodoId = 'failing';
  const requestedTodoIds = [];

  let resolveSuccessfulTodoRequest;
  const successfulTodoRequestFinished = new Promise((resolve) => {
      resolveSuccessfulTodoRequest = resolve;
  });

  const queryClient = new QueryClient({
      defaultOptions: {
          queries: {
              retry: false,
          },
      },
  });

  const todoCollection = createCollection(
      queryCollectionOptions({
          queryKey: ['todos'],
          queryClient,
          retry: false,
          syncMode: 'on-demand',
          queryFn: async (context) => {
              const requestedTodoId = parseLoadSubsetOptions(context.meta?.loadSubsetOptions).filters.find(
                  ({ operator }) => operator === 'eq',
              )?.value;

              requestedTodoIds.push(String(requestedTodoId));

              if (requestedTodoId === successfulTodoId) {
                  const result = [{ id: successfulTodoId, completed: false, text: 'Successful todo' }];

                  resolveSuccessfulTodoRequest();

                  return result;
              }

              if (requestedTodoId === failingTodoId) {
                  throw new Error('Failed to fetch filtered todos');
              }

              return [];
          },
          getKey: (todo) => todo.id,
      }),
  );

  const successfulTodoQuery = createLiveQueryCollection({
      query: (q) => q.from({ todo: todoCollection }).where(({ todo }) => eq(todo.id, successfulTodoId)),
      startSync: true,
  });

  // Wait until the first live query and its backend request have completed.
  await successfulTodoQuery.preload();
  await successfulTodoRequestFinished;

  if (JSON.stringify(requestedTodoIds) !== JSON.stringify([successfulTodoId])) {
      throw new Error(`The successful query did not finish before the failing query started:
      ${JSON.stringify(requestedTodoIds)}`);
  }

  const failingTodoQuery = createLiveQueryCollection({
      query: (q) => q.from({ todo: todoCollection }).where(({ todo }) => eq(todo.id, failingTodoId)),
      startSync: true,
  });

  await failingTodoQuery.preload().catch(() => undefined);

  console.log({
      requestedTodoIds,
      todoCollectionReady: todoCollection.isReady(),
      todoCollectionHasError: todoCollection.utils.isError,
      todoCollectionLastError: todoCollection.utils.lastError?.message,
      successfulTodoQueryStatus: successfulTodoQuery.status,
      failingTodoQueryStatus: failingTodoQuery.status,
  });

 

Expected behavior
Expected output:

{
      requestedTodoIds: ['successful', 'failing'],
      todoCollectionReady: true,
      todoCollectionHasError: true,
      todoCollectionLastError: 'Failed to fetch filtered todos',
      successfulTodoQueryStatus: 'ready',
      failingTodoQueryStatus: 'error',
  }

Actual output:

  {
      requestedTodoIds: ['successful', 'failing'],
      todoCollectionReady: true,
      todoCollectionHasError: true,
      todoCollectionLastError: 'Failed to fetch filtered todos',
      successfulTodoQueryStatus: 'ready',
      failingTodoQueryStatus: 'ready',
  }

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the on-demand behavior exposed through queryCollectionOptions and createLiveQueryCollection, using the reproduction and expected versus actual statuses in this issue. Verify how a later filtered request affects live-query error state, then confirm that the failing query reports error while the successful query remains ready.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.