Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server Action only works when queryFn isn't a reference #6264

Closed
jschuur opened this issue Oct 30, 2023 · 4 comments
Closed

Server Action only works when queryFn isn't a reference #6264

jschuur opened this issue Oct 30, 2023 · 4 comments
Labels
has workaround wontfix This will not be worked on

Comments

@jschuur
Copy link

jschuur commented Oct 30, 2023

Describe the bug

In a Next.js 14 app directory project with React Query 5 inside a client component, if I use reference (queryFn: getProjects) for the query function instead of an inline function that calls a function, I get this error:

Only plain objects, and a few built-ins, can be passed to Server Actions. Classes or null prototypes are not supported.

Doing it this way works:

useQuery({
  queryKey: ['projects'],
  queryFn: () => getProjects(),
});

Is this a bug?

I understand what a plain object is, and getProjects does return a promise to a serialisable object.

Your minimal, reproducible example

https://codesandbox.io/p/sandbox/gracious-golick-fq3ss9

Steps to reproduce

My useQuery call is abstracted away inside a custom hook that starts with 'use client';:

'use client';

import { useQuery } from '@tanstack/react-query';

import { getProjects } from '@/db/queries';

export default function useProjects() {
  return useQuery({
    queryKey: ['projects'],
    // works if inline function not if 'queryFn: getProjects'
    queryFn: () => getProjects(),
  });
}

This hook is used in a client component ('use client';). The error also happens if I use useQuery directly in the client component and skip the custom hook.

getProjects comes from a file prefixed with 'use server'; (the query is run by Drizzle on Turso):

'use server';

import 'server-only';

import { db } from '@/db/';
import { projects } from '@/db/schema';

export const getProjects = async () => db.select().from(projects);

I use the HydrationBoundary pattern, but even if I remove that from page.tsx and just create a client side query client, I get the same error too, unless I use an inline query function.

Expected behavior

useQuery accepts both a function reference and an inline function for queryFn.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • OS: macOS
  • Browser: Chrome
  • Version: 118.0.5993.117

Tanstack Query adapter

react-query

TanStack Query version

v5.4.3

TypeScript version

v5.2.2

Additional context

The error only kicks in after a few seconds, so it looks like React Query is doing it's default attempt at trying a few times and backing off in between attempts.

@weisisheng
Copy link
Contributor

weisisheng commented Oct 30, 2023

Seeing the same and your fix is also not working for me. Oddly, I am still getting stale data from yesterday although console.log showing correct pull from database.

tanstack/react-query 5.0.5, next.js 13.5.6

*edit: ignore my second statement, for some reason, google chrome was doing some very aggressive caching. will follow this item progress.

@TkDodo
Copy link
Collaborator

TkDodo commented Oct 30, 2023

The QueryFunction itself gets a QueryFunctionContext passed, which is an object that contains a bunch of things. By passing the function as a reference, you're basically doing:

queryFn: (...args) => getProjects(args)

TypeScript is fine with this even though getProjects doesn't take any args.

If you use that syntax, you're getting the same error. I've narrowed it down to one of our arguments being an AbortSignal, which is not a plain object so it likely cannot be passed to a server action. So, this notation won't work.

@TkDodo TkDodo closed this as not planned Won't fix, can't repro, duplicate, stale Oct 30, 2023
@TkDodo TkDodo added wontfix This will not be worked on has workaround labels Oct 30, 2023
@reinaldo-vombo
Copy link

Dude you just save my life

@davidchalifoux
Copy link

Thanks for figuring this out, it was driving me crazy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has workaround wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

5 participants