T
TanStack10mo ago
harsh-harlequin

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-harlequin
harsh-harlequinOP10mo 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

Did you find this page helpful?