TanStackT
TanStack2mo ago
4 replies
dangerous-fuchsia

Best practice for dependent queries relying on parent collection ID?

Hi everyone! 👋

I’m using TanStack DB (collections) in combination with TanStack Query in an Angular app.

I have a setup where a Products collection depends on the selected projectId. I’m using an Angular computed() signal to track the active projectId based on the Projects collection (as that has an isActive variable).

The Issue: When I create a new Project, TanStack DB updates the collection optimistically, so the projectId exists immediately in the client state. Because of the signal reactivity, my dependent "Products" query fires off instantly—at the same time as the "Create Project" mutation.

This creates a race condition:

POST /projects (Pending)

GET /projects/:id/products (Fires immediately because the signal saw the new ID)

The GET often hits the server before the POST finishes, resulting in a 404.

My Question: What is the best way to handle this dependency in the TanStack DB ecosystem?

Since the ID is valid (optimistically), I can't just check if (!id). Is there a standard way to check if a specific item in a Collection is still "optimistic" or "syncing" so I can disable the dependent products fetch until the server confirms the project exists?

Thanks!
Was this page helpful?