useQuery with server function?
Hello - I ran into something I didn't know how to handle. I am trying to make a query - but I am passing the
db in the query options. However, this gets ran in the client so - pg can't be called there. What is the proper way to handle this in Tanstack Start with useQuery or queryClient perhaps?
// some file
import { db } from "~/server/db";
export const MESSAGE_LIST_QUERY_KEY = ["messages"];
const sessionHistoryQueryOptions = () =>
queryOptions({
queryKey: MESSAGE_LIST_QUERY_KEY,
queryFn: () => getAllSessionHistory(db),
});
export default sessionHistoryQueryOptions;
// in another file
// does not work. pg can't be called in the client
const { data: messages } = useQuery(sessionHistoryQueryOptions());1 Reply
harsh-harlequinOP•10mo ago
Ok I fixed it by creating a server function than used
useServerFn to wrap the function pass to queryFn. I wonder if there is a shortcut, i.e: a flag that calls queryFn in the server directly