TanStackT
TanStack15mo ago
29 replies
cold-orange

Not sure if this belongs in #start or #query, but type inference when using server functions

Hello, not sure where this belongs, but I'm using Start and Query and my type inference seems to be wrong when my objects contain dates. See images and code related.

import { queryOptions } from "@tanstack/react-query";
import { redirect } from "@tanstack/react-router";
import { createServerFn, json } from "@tanstack/start";
import { asc, eq } from 'drizzle-orm';
import { getEvent } from "vinxi/http";
import { db } from "~/server/db";
import { transaction } from "~/server/db/schema";


const fetchUserTransactions = createServerFn('GET', async (_, ctx) => {
    const event = getEvent();
    const auth = event.context.auth;

    if (!auth.isAuthenticated) {
        throw redirect({
            to: '/signin',
            code: 400
        })
    }

    const transactions = await db.select().from(transaction).where(eq(transaction.userId, auth.user?.id)).orderBy(asc(transaction.date))
    
    
    return json(transactions);
})

export const transactionQueries = {
    getUserTransactions: () => queryOptions({
        queryKey: ['transactions', 'all'],
        queryFn: () => fetchUserTransactions(),
    }),
} as const;
image.png
image.png
Was this page helpful?