RayyR
Convex Community2y ago
10 replies
Rayy

What is the best approach to call query when they depend on scheduled functions?

I am calling
useMutation
from client where it inserts data in the db for that particular chat Id, and then run certain scheduled functions based on the data, and I am returning the chat Id to the client when the data has been inserted to the db.

My application is working fine, but I feel like I am making unnecessary query requests, when all the scheduled functions have not yet been completed.

I am currently showing a loading state to the user till the embeddingId is undeifned, since the last scheduled function will populate the embeddingId.

export const useQueryChatProps = ({ chatId }: { chatId: Id<"chatbook"> }) => {
  const data = useQuery(api.chatbook.getEmbeddingId, {
    chatId,
  });

  const loading = data === undefined || data?.embeddingId === undefined;

  return {
    data,
    loading,
  };
};


Is this okay? Or is there a better way to achieve this?

Here is my code for inserting data to the db and calling the scheuled functions

   const user = await ctx.db
      .query("users")
      .filter((q) => q.eq(q.field("_id"), args.userId));
    if (!user) {
      throw new ConvexError("You need to login first.");
    }

    const url = args.storageId
      ? await ctx.storage.getUrl(args.storageId)
      : args.url;

    if (!url) {
      throw new ConvexError("No URL found.");
    }

    const chatId = await ctx.db.insert("chatbook", {
      userId: args.userId,
      url,
    });

      await ctx.scheduler.runAfter(0, internal.github.getFilesFromRepo, {
        repoUrl: url,
        filePath: extractPathFromUrl(url),
        chatId,
      });

    return chatId;


The scheduled function is dependant on other scheduled functions.
Was this page helpful?