CabalDAO
CabalDAO•6mo ago

just import it and call it?

just import it and call it?
7 Replies
nipunn
nipunn•6mo ago
I think easiest way is to factor the logic into a helper method and have both call it.
james
james•6mo ago
you can also just call it, but note that if you have any args type checking on the second query those types won't be enforced at runtime. they only get enforced when called by the convex runtime perhaps we should add a ctx.runQuery from queries (like in actions) but we haven't done so since it's generally a better software development practice to use helper methods, i.e., just a regular function call
nipunn
nipunn•6mo ago
yep this works
export const first = query(async (ctx) => {
let x = await second(ctx, {});
console.log(x);
return x;
});
export const second = query(async () => {
return "a";
});
export const first = query(async (ctx) => {
let x = await second(ctx, {});
console.log(x);
return x;
});
export const second = query(async () => {
return "a";
});
but I like this way better
export const first = query(async (ctx) => {
let x = await secondImpl(ctx);
console.log(x);
return x;
});
export const second = query(async (ctx) => {
return secondImpl(ctx);
});

const secondImpl = async (ctx: QueryCtx) => {
return "a";
};
export const first = query(async (ctx) => {
let x = await secondImpl(ctx);
console.log(x);
return x;
});
export const second = query(async (ctx) => {
return secondImpl(ctx);
});

const secondImpl = async (ctx: QueryCtx) => {
return "a";
};
But whatever you like!
james
james•6mo ago
second way! 😉
CabalDAO
CabalDAOOP•6mo ago
im not sure i get why second is better but ok 🙂 thank you
james
james•6mo ago
second is better just because it's less ambiguous about what it means e.g., if there was any arg validation in the first one then it would run on the sub-function, plus the sub-function doesn't run as a separate transaction, just within the context of the first transaction. but functionally they are both the same, apart from looking less confusing
CabalDAO
CabalDAOOP•6mo ago
ah i understand now. Thank you!

Did you find this page helpful?