Correctly type entities with related fields
How can I make wasp correctly type the return type of a query when I include a related field?
In this case, I'm including the objectives property in my query:
Do i need to add the entity name to the wasp query declaration?
For reference, there's 🙋questionsHow to properly type Prisma query results with included relations in Wasp?
In this case, I'm including the objectives property in my query:
model Goal {
id String @id @default(cuid())
title String
description String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
clientId String
client Client @relation(fields: [clientId], references: [id], onDelete: Cascade)
objectives Objective[]
appointmentGoals AppointmentGoal[]
@@map("goals")
}model Goal {
id String @id @default(cuid())
title String
description String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
clientId String
client Client @relation(fields: [clientId], references: [id], onDelete: Cascade)
objectives Objective[]
appointmentGoals AppointmentGoal[]
@@map("goals")
}const getAllGoalsByClientSchema = z.object({
selectedClientId: z.string(),
})
type GetAllGoalsByClientInput = z.infer<typeof getAllGoalsByClientSchema>
export const getAllGoalsByClient: GetAllGoalsByClient<GetAllGoalsByClientInput, Goal[]> = async (rawArgs, context) => {
if (!context.user) {
throw new HttpError(401, 'Only authenticated users are allowed to perform this operation')
}
const { selectedClientId } = ensureArgsSchemaOrThrowHttpError(getAllGoalsByClientSchema, rawArgs)
return context.entities.Goal.findMany({
where: { clientId: selectedClientId },
include: { objectives: true },
})
}const getAllGoalsByClientSchema = z.object({
selectedClientId: z.string(),
})
type GetAllGoalsByClientInput = z.infer<typeof getAllGoalsByClientSchema>
export const getAllGoalsByClient: GetAllGoalsByClient<GetAllGoalsByClientInput, Goal[]> = async (rawArgs, context) => {
if (!context.user) {
throw new HttpError(401, 'Only authenticated users are allowed to perform this operation')
}
const { selectedClientId } = ensureArgsSchemaOrThrowHttpError(getAllGoalsByClientSchema, rawArgs)
return context.entities.Goal.findMany({
where: { clientId: selectedClientId },
include: { objectives: true },
})
}Do i need to add the entity name to the wasp query declaration?
app.query("getAllGoalsByClient", {
fn: {import: "getAllGoalsByClient", from: "@src/portal/operations/goal" },
entities: ["Goal", "Objective"],
});app.query("getAllGoalsByClient", {
fn: {import: "getAllGoalsByClient", from: "@src/portal/operations/goal" },
entities: ["Goal", "Objective"],
});For reference, there's 🙋questionsHow to properly type Prisma query results with included relations in Wasp?