Skip to content

Bug: useInfiniteQuery causes 400 Bad Request after .invalidate() due to auto-injected cursor: undefined #6862

Open

Description

@devlopermitesh

Provide environment information

nothing

Describe the bug

❗ Problem

Using trpc.studio.getMany.useSuspenseInfiniteQuery({ limit: 5 }) works correctly on first render, but after a mutation followed by utils.studio.getMany.invalidate(), tRPC triggers a refetch that causes a 400 Bad Request error.


🔍 Logs

GET /api/trpc/studio.getMany?batch=1&input=%7B%220%22%3A%7B%22limit%22%3A5%2C%22direction%22%3A%22forward%22%7D%7D 400

Decoded input:

{
  "0": {
    "limit": 5,
    "direction": "forward"
  }
}

Expected input:

{
  limit: number;
  direction?: "forward" | "backward";
  cursor?: {
    id: string;
    updateAt: Date;
  } | null;
}

✅ Steps to Reproduce

  1. Use useSuspenseInfiniteQuery({ limit: 5 }) on the client

  2. Set up schema like:

    .input(z.object({
      limit: z.number(),
      cursor: z.object({
        id: z.string().uuid(),
        updateAt: z.coerce.date()
      }).optional().nullable(),
      direction: z.enum(["forward", "backward"]).optional(),
    }))
  3. Trigger a mutation

  4. Call utils.studio.getMany.invalidate()

  5. Observe 400 error


✅ Expected Behavior

  • invalidate() should refetch the query without producing a Zod validation error.
  • cursor: undefined and direction: "forward" should be supported by default, as they are internally used by useInfiniteQuery().

🧠 Additional Notes

  • This issue occurs because React Query internally passes cursor: undefined even if the user doesn’t specify it.
  • Only .optional().nullable() allows this without throwing a Zod error.

🛠️ Workaround

Updating the schema like this resolves the issue:

cursor: z.object({
  id: z.string().uuid(),
  updateAt: z.coerce.date(),
}).optional().nullable()

Link to reproduction

https://github.com/devlopermitesh/Youtube

To reproduce

🐛 Bug

git clone '<repo>'

Then click on the Create button →
✅ I see:
POST /api/trpc/video.create?batch=1

But immediately after:
❌ I get an error:
GET /api/trpc/studio.getMany?batch=1&input=%7B%220%22%3A%7B%22limit%22%3A5%2C%22direction%22%3A%22forward%22%7D%7D → 400 Bad Request

Additional information

No response

👨‍👧‍👦 Contributing

  • 🙋‍♂️ Yes, I'd be down to file a PR fixing this bug!

Activity

  1. KATT commented on Nov 2, 2026

    @KATT
    Member

    🤖 In v12, infiniteOptions() takes input as a function of the page param, e.g. input: (cursor) => ({ ...yourInput, cursor }), so the cursor only goes where you put it and nothing injects cursor: undefined on a refetch after invalidate(). https://v12.trpc.io/v12/docs/integrations/tanstack-query#the-helpers. v12 isn't released yet and there's no ETA.

  2. added
    next-major/definite
    on Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    next-major/definite🐛 bug: unconfirmed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions