Mordsith
Mordsith3mo ago

Custom query with default argument

I want to write a custom query that adds a default argument.
export const organizationAdminQuery = customQuery(
query,
customCtx(async (ctx) => {
const userId = await getAuthUserIdOrFail(ctx);
return { ...ctx, userId };
})
);

// These queries are only available to the organization admin
export const orgAdminQuery = organizationAdminQuery({
args: { organizationId: v.id("organization") },
handler: async (ctx, { organizationId }) => {
return getOrganizationAdmins(organizationId)
},
});
export const getOrganizationSponsorsQuery = organizationAdminQuery({
args: { organizationId: v.id("organization") },
handler: async (ctx, { organizationId }) => {
return getOrganizationAdmins(organizationId)
},
});
export const organizationAdminQuery = customQuery(
query,
customCtx(async (ctx) => {
const userId = await getAuthUserIdOrFail(ctx);
return { ...ctx, userId };
})
);

// These queries are only available to the organization admin
export const orgAdminQuery = organizationAdminQuery({
args: { organizationId: v.id("organization") },
handler: async (ctx, { organizationId }) => {
return getOrganizationAdmins(organizationId)
},
});
export const getOrganizationSponsorsQuery = organizationAdminQuery({
args: { organizationId: v.id("organization") },
handler: async (ctx, { organizationId }) => {
return getOrganizationAdmins(organizationId)
},
});
I would like to add an organizationId argument to this custom query so that every consumer of this custom query won't need an organizationId in the query args. I'm trying to achieve this to avoid the repeated organizationId in the args param
export const getOrganizationSponsorsQuery = organizationAdminQuery({
args: {},
handler: async (ctx, { organizationId }) => {
return getOrganizationAdmins(organizationId)
},
});
export const getOrganizationSponsorsQuery = organizationAdminQuery({
args: {},
handler: async (ctx, { organizationId }) => {
return getOrganizationAdmins(organizationId)
},
});
I got this working using another approach but I'll like to know if this can be done directly with customQuery
4 Replies
Convex Bot
Convex Bot3mo ago
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 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!
jamalsoueidan
jamalsoueidan3mo ago
const myQueryBuilder = customQuery(query, {
args: { sessionId: v.id("sessions") },
input: async (ctx, args) => { // << input instead of customCtx
const user = await getUserOrNull(ctx);
const session = await db.get(sessionId);
const db = wrapDatabaseReader({ user }, ctx.db, rlsRules);
return { ctx: { db, user, session }, args: {} };
},
});
const myQueryBuilder = customQuery(query, {
args: { sessionId: v.id("sessions") },
input: async (ctx, args) => { // << input instead of customCtx
const user = await getUserOrNull(ctx);
const session = await db.get(sessionId);
const db = wrapDatabaseReader({ user }, ctx.db, rlsRules);
return { ctx: { db, user, session }, args: {} };
},
});
erquhart
erquhart3mo ago
Customizing serverless functions without middleware
Re-use code and centralize request handler definitions with discoverability and type safety and without the indirection of middleware or nesting of wr...
Mordsith
MordsithOP3mo ago
Thank you @erquhart @jamalsoueidan

Did you find this page helpful?