Skip to content
Source
Sign inSign up
TanStack/query

Handle circular references in query keys #8625

Send to my agent
ClosedCollection50(GitHub profile, opens in a new tab) opened this issue 1 comment

Thank you for improving the asynchronous state management for TS/JS ecosystem with a greatest library.

I think it's good if the user becomes a more predictable queryKey.

I will write the relevant PR.

Describe the bug

Problem Statement

When using query keys containing circular references or complex objects (e.g., React elements in DEV), JSON.stringify in query key serialization triggers maximum call stack errors due to:

  1. Recursive object comparison in replacer functions
  2. Circular dependencies in nested objects

This results in unpredictable behavior and debugging difficulties for developers using non-serializable query keys.

When,

  const div = <div>asfd</div>;

  const {data} = useQuery({
    queryKey: [div],
    queryFn: // ...
  });
  const obj = {name: "root"};
  obj.self = obj;

  const {data} = useQuery({
    queryKey: [obj],
    queryFn: // ...
  });

Then,

image

Your minimal, reproducible example

https://codesandbox.io/p/sandbox/3rz288?file=%2Fsrc%2FApp.tsx

Steps to reproduce

  1. use useQuery
  2. add circular object to queryKey
  3. fetch
  4. error

Expected behavior

As a user, I wanted a more accurate error (Circular), or for the circular to be ignored.

However, a Maximum call stack error occurred, and a lot of time was spent on debugging.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

OS: macOS browser: Chrome

Tanstack Query adapter

react-query

TanStack Query version

5.66.0

TypeScript version

5.6.2

  1. TkDodo closed this

  2. TkDodo added labelswontfix

Sign in to join this conversation.