leoschettini
leoschettini
WWasp-lang
Created by leoschettini on 3/27/2024 in #đŸ™‹questions
Calling queries without useQuery hook
Hi, I'm trying to call a query directly, from the client, without using the hook, but couldn't figure out how to do so. I declared the query in the main.wasp file:
query getUserWritingAssistants {
fn: import { GetUserWritingAssistants } from "@src/server/queries.js",
entities: [WritingAssistant]
}
query getUserWritingAssistants {
fn: import { GetUserWritingAssistants } from "@src/server/queries.js",
entities: [WritingAssistant]
}
and have the function implemented in queries.ts too:
export const getUserWritingAssistants: GetUserWritingAssistants<void, WritingAssistant[]> = async (_args, context) => {
if (!context.user) {
throw new HttpError(401)
}

return context.entities.WritingAssistant.findMany({
where: { author: { id: context.user.id } }
})
}
export const getUserWritingAssistants: GetUserWritingAssistants<void, WritingAssistant[]> = async (_args, context) => {
if (!context.user) {
throw new HttpError(401)
}

return context.entities.WritingAssistant.findMany({
where: { author: { id: context.user.id } }
})
}
Based on https://github.com/wasp-lang/wasp/issues/1909, I'm trying to call it like this:
import { getUserWritingAssistants } from 'wasp/client/operations';

const { data: creatorAssistants } = await getUserWritingAssistants(getUserWritingAssistants.queryCacheKey, { user });
import { getUserWritingAssistants } from 'wasp/client/operations';

const { data: creatorAssistants } = await getUserWritingAssistants(getUserWritingAssistants.queryCacheKey, { user });
That gives me this error: Property 'queryCacheKey' does not exist on type '(queryCacheKey: string[], args: any) => Promise<any>'. Plus, based on this error, the signature of getUserWritingAssistants will not allow me to pass the user like that. So, questions are: How to call the query directly, and how can I build the context to pass to the query? For some context: I'm implementing some post-login logic, and am setting some cookies to determine what I need to execute after login/signup. Based on these cookies, I dynamically execute different functions, some of them that need to query additional data to work properly. I could use the useQuery hook, but rather keep the logic abstracted from the actual component. Right now, my component is simply getting the AuthUser and passing it to another function that does the querying.
19 replies