#Cannot solve "Convex functions should not directly call other Convex functions"

1 messages · Page 1 of 1 (latest)

dusk crane
#

I searched, asked AI and tried to create helper functions but I can't get rid of this warn, it seems to be happening in my Promise.all() because I get a lot of logs.
My helper functions:

// convex/models/routes.ts

import { geospatial } from "@/convex";
import { internal } from "@/convex/_generated/api";
import { Id } from "@/convex/_generated/dataModel";
import { ActionCtx, MutationCtx } from "@/convex/_generated/server";
import schema from "@/convex/schema";
import { Infer } from "convex/values";

export async function insertRoute(
  ctx: MutationCtx,
  data: Infer<typeof schema.tables.routes.validator>,
): Promise<Id<"routes">> {
  return await ctx.db.insert("routes", { ...data });
}

export async function insertGeospatialPoint(
  ctx: MutationCtx,
  routeId: Id<"routes">,
  point: {
    latitude: number;
    longitude: number;
  },
) {
  return await geospatial.insert(
    ctx,
    routeId,
    {
      latitude: point.latitude,
      longitude: point.longitude,
    },
    {},
  );
}

// NEW FUNCTION
export async function insertManyGeospatialPoints(
  ctx: ActionCtx,
  routeId: Id<"routes">,
  points: {
    latitude: number;
    longitude: number;
  }[],
) {
  const promises = points.map((point) =>
    ctx.runMutation(internal.routes.insertGeospatialPointMutation, {
      routeId,
      point,
    }),
  );
  return await Promise.all(promises);
}

Rest of code is in the next comment because of Discord limit.

spiral sageBOT
#

Thanks for posting in #1088161997662724167.
Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets.

    - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.)
    - Use [search.convex.dev](https://search.convex.dev) to search Docs, Stack, and Discord all at once.
    - Additionally, you can post your questions in the Convex Community's #1228095053885476985 channel to receive a response from AI.
    - Avoid tagging staff unless specifically instructed.

    Thank you!
dusk crane
#
// convex/routes.ts

import { geospatial } from "@/convex";
import { internal } from "@/convex/_generated/api";
import { Id } from "@/convex/_generated/dataModel";
import { action, internalMutation, query } from "@/convex/_generated/server";
import { coordinateSchema, routeSchema } from "@/convex/schema";
import { v } from "convex/values";
import * as Routes from "./model/routes";

export const insertRouteMutation = internalMutation({
  args: {
    ...routeSchema,
  },
  handler: async (ctx, data) => Routes.insertRoute(ctx, data),
});

export const insertGeospatialPointMutation = internalMutation({
  args: {
    routeId: v.id("routes"),
    point: coordinateSchema,
  },
  handler: async (ctx, { routeId, point }) =>
    Routes.insertGeospatialPoint(ctx, routeId, point),
});

export const createRouteWithPointsAction = action({
  args: {
    ...routeSchema,
    points: v.array(coordinateSchema),
  },
  handler: async (ctx, { points, ...route }) => {
    console.log(points.length);
    const routeId: Id<"routes"> = await ctx.runMutation(
      internal.routes.insertRouteMutation,
      route,
    );

    // UPDATED LINE
    await Routes.insertManyGeospatialPoints(ctx, routeId, points);

    return routeId;
  },
});

waxen cradle
#

hmm i don't see anything wrong in this code. can you share the code for import { geospatial } from "@/convex"; ?

dusk crane
waxen cradle
#

makes sense. and the code is export const geospatial = new GeospatialIndex(...) or something?

dusk crane
#

yes, here

// convex/index.ts
import { Id } from "@/convex/_generated/dataModel";
import { GeospatialIndex } from "@convex-dev/geospatial";
import { components } from "./_generated/api";

export const geospatial = new GeospatialIndex<Id<"routes">>(
  components.geospatial,
);

tepid talon
#

What version of that component are you using?

dusk crane
#

since it's still in beta, could be a minor issue from the lib?

waxen cradle
#

could be, but i don't see an issue with the lib though. is this happening in prod or convex-test?

#

oh i see you're calling internal.routes.insertGeospatialPoint but according to your filepaths it would be internal.index.insertGeospatialPoint. can you share the contents of convex/routes.ts?

dusk crane
tepid talon
#

You have code that calls internal.routes.insertGeospatialPointMutation, that's a function presumably defined in convex/routes.ts? that's code we can't see and we're wondering if has this pattern in it

dusk crane
tepid talon
#

Did the "Convex functions should not directly call other Convex functions" error have any other details?

#

to make sure we're on the same page, what we're trying to do is fine where a convex query, mutation, or action is being called directly instead of using helper functions

#

so just looking at the code could be enough, if we can find the right code to look at

dusk crane
#

also, these warns only happens on my local terminal (when running npx convex dev), there are no warns in my project logs

tepid talon
#

Oh isn't it just this?

await Routes.insertManyGeospatialPoints(ctx, routeId, points);
#

oh that's what Lee asked abotu alreacyx

tepid talon
#

can you show that code?

dusk crane
#

am I doing it wrong?

tepid talon
#

huh yeah it looks ok, it's not a Convex Function, it's a helper

dusk crane
#

seems like any interaction with geospatial will log this warning, so if you're saying the code looks ok I could just ignore it for now

tepid talon
#

Sounds good

#

thanks for reporting @dusk crane, we'll let you know when there's a new version of the geospatial component that fixes this