T
TanStack6mo ago
absent-sapphire

How to call a Convex mutation from createServerFn?

I'm lost on how I can use Convex from the server. Here is my attempt
const convex = new ConvexHttpClient(import.meta.env.VITE_CONVEX_URL);

// Define the input schema using Zod
const suggestMilestonesInputSchema = z.object({
userId: z.string(),
goal: z.any(), // Allow any type for goal for now
language: z.string(),
});

export const suggestMilestones = createServerFn({
method: "POST",
})
.validator((person: unknown) => {
return suggestMilestonesInputSchema.parse(person);
})
.handler(async (ctx) => {
// Call mutation inside the handler
await convex.mutation(api.user.increaseAISuggestions, { userId: userId as Id<"user"> });

});
const convex = new ConvexHttpClient(import.meta.env.VITE_CONVEX_URL);

// Define the input schema using Zod
const suggestMilestonesInputSchema = z.object({
userId: z.string(),
goal: z.any(), // Allow any type for goal for now
language: z.string(),
});

export const suggestMilestones = createServerFn({
method: "POST",
})
.validator((person: unknown) => {
return suggestMilestonesInputSchema.parse(person);
})
.handler(async (ctx) => {
// Call mutation inside the handler
await convex.mutation(api.user.increaseAISuggestions, { userId: userId as Id<"user"> });

});
Here is my mutation
// Update user email
export const updateUserEmail = mutation({
args: {
userId: v.id("user"),
email: v.string(),
},
handler: async (ctx, { userId, email }) => {
await ctx.db.patch(userId, {
email,
updatedAt: new Date().toISOString(),
});

return { success: true };
},
});
// Update user email
export const updateUserEmail = mutation({
args: {
userId: v.id("user"),
email: v.string(),
},
handler: async (ctx, { userId, email }) => {
await ctx.db.patch(userId, {
email,
updatedAt: new Date().toISOString(),
});

return { success: true };
},
});
4 Replies
fascinating-indigo
fascinating-indigo6mo ago
cc @ballingt
absent-sapphire
absent-sapphireOP6mo ago
If there is any example boilerplate on how to call convex from the createServerFn handler that would be amazing So I basically want to have some validations on the server side (does the user have enough credits) I can see there is:
import { fetchMutation, fetchQuery } from "convex/nextjs";
import { fetchMutation, fetchQuery } from "convex/nextjs";
Not sure if its also available for tanstack
wise-white
wise-white6mo ago
Yep works in tanstack if use specify the url as an option
absent-sapphire
absent-sapphireOP6mo ago
const user = await fetchQuery(api.user.findByID, {
id: userId as Id<"user">,
url: import.meta.env.VITE_CONVEX_URL,
});
const user = await fetchQuery(api.user.findByID, {
id: userId as Id<"user">,
url: import.meta.env.VITE_CONVEX_URL,
});
im getting
Object literal may only specify known properties, and 'url' does not exist in type '{ id: Id<"user">; }'.ts(2353)
Object literal may only specify known properties, and 'url' does not exist in type '{ id: Id<"user">; }'.ts(2353)
Whereas in the NextJSOptions it says:
/**
* Options to {@link preloadQuery}, {@link fetchQuery}, {@link fetchMutation} and {@link fetchAction}.
*/
export type NextjsOptions = {
/**
* The JWT-encoded OpenID Connect authentication token to use for the function call.
*/
token?: string;
/**
* The URL of the Convex deployment to use for the function call.
* Defaults to `process.env.NEXT_PUBLIC_CONVEX_URL`.
*/
url?: string;
/**
* Options to {@link preloadQuery}, {@link fetchQuery}, {@link fetchMutation} and {@link fetchAction}.
*/
export type NextjsOptions = {
/**
* The JWT-encoded OpenID Connect authentication token to use for the function call.
*/
token?: string;
/**
* The URL of the Convex deployment to use for the function call.
* Defaults to `process.env.NEXT_PUBLIC_CONVEX_URL`.
*/
url?: string;
Do you know how I can pass the
url
url
to the fetchQuery/fetchMutation? okay apparently this is the syntax
const user = await fetchQuery(
api.user.findByID,
{
id: userId as Id<"user">,
},
{
url: import.meta.env.VITE_CONVEX_URL,
},
);
const user = await fetchQuery(
api.user.findByID,
{
id: userId as Id<"user">,
},
{
url: import.meta.env.VITE_CONVEX_URL,
},
);

Did you find this page helpful?