Stevan
Stevan3y ago

How to call public query from action?

This seems like it should be super obvious, but I can't see to understand how to reference the public query within the runQuery method.
6 Replies
ballingt
ballingt3y ago
hm, what syntax are you trying to use? ctx.runQuery(api.file.myQuery) should do it
Stevan
StevanOP3y ago
Is that still valid if the action and query are in the same file?
jamwt
jamwt3y ago
Yep
Stevan
StevanOP3y ago
Okay I’ll try that. Thank you!
Cam
Cam2y ago
I noticed in the docs that internal queries are used in the examples, no security concerns with importing queries and calling them from an action like this?
import {api, internal} from "./_generated/api";

export const getWillyWeather = action({
args: {locationID: v.number()}, handler: async (ctx, args) => {
// Check if weather conditions exist, and are recent (no older than 20 minutes)
const weatherLocation = await ctx.runQuery(api.weatherQueries.queryLocationByID, {id: args.locationID});
const weatherConditions = await ctx.runQuery(api.weatherQueries.queryConditionsByLocationForeignKey, {locationID: weatherLocation._id});
...
},
});
import {api, internal} from "./_generated/api";

export const getWillyWeather = action({
args: {locationID: v.number()}, handler: async (ctx, args) => {
// Check if weather conditions exist, and are recent (no older than 20 minutes)
const weatherLocation = await ctx.runQuery(api.weatherQueries.queryLocationByID, {id: args.locationID});
const weatherConditions = await ctx.runQuery(api.weatherQueries.queryConditionsByLocationForeignKey, {locationID: weatherLocation._id});
...
},
});
lee
lee2y ago
@Cam (maybe this could be another support thread?) it doesn't look like you're using internal queries in that example. Public queries are available for anyone to call. If the queries don't check auth and expose secure information, then that would be a security concern

Did you find this page helpful?