W
Wasp-lang•3mo ago
anand248

When we query any entity with child entities included, how to get types defined for those in client

Wasp version - latest 0.14 e.g. query getStandard { fn: import { getStandard } from "@src/standards/queries", entities: [Order] } My query is actuall able to return child entitiy [orderRows] inside Order, how can I specify that in .wasp file ?
4 Replies
kapa.ai
kapa.ai•3mo ago
Hi @anand248. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- In Wasp, you can specify the entities used in your query in the .wasp file. However, the current version of Wasp (0.14) does not support specifying nested entities directly in the .wasp file. The entities field in the query declaration is used to specify the top-level entities that the query interacts with. Here is an example of how you can declare a query in a .wasp file:
query getStandard {
fn: import { getStandard } from "@src/standards/queries",
entities: [Order]
}
query getStandard {
fn: import { getStandard } from "@src/standards/queries",
entities: [Order]
}
In your Node.js query implementation, you can include child entities in the result by using Prisma's include feature. However, the types for these included entities are not automatically generated by Wasp. You would need to manually define these types in your TypeScript code. For full-stack type safety, Wasp is working on improvements as mentioned in this Github issue (https://github.com/wasp-lang/wasp/issues/910). Also, there is a proposal to expose Prisma types as Wasp imports as mentioned in this Github issue (https://github.com/wasp-lang/wasp/issues/901). These improvements might help in the future with the type safety of nested entities. For now, you might need to manually ensure that the types used in your client-side code match the structure of the data returned by your queries. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @anand248, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
miho
miho•3mo ago
Could you please share your TS code for this piece 😄 Read this section in the docs: https://wasp-lang.dev/docs/data-model/operations/queries#implementing-queries-in-node Pay attention to the "Inferring the return type" piece. Using the satisfies to type your queries will make sure that the client gets the correct types.
Queries | Wasp
We'll explain what Queries are and how to use them. If you're looking for a detailed API specification, skip ahead to the API Reference.
StackTrace
StackTrace•2mo ago
Hi - so to elaborate on this answer are we saying that its not able to inherit the type from the model of the parent with the child? Ill need to extend the type on the client side to interpret
miho
miho•2mo ago
Did you see this section in the docs:
If don't want to explicitly type the Query's return value, the satisfies keyword tells TypeScript to infer it automatically:
const getFoo = (async (_args, context) => {
const foos = await context.entities.Foo.findMany()
return {
foos,
message: 'Here are some foos!',
queriedAt: new Date(),
}
}) satisfies GetFoo
const getFoo = (async (_args, context) => {
const foos = await context.entities.Foo.findMany()
return {
foos,
message: 'Here are some foos!',
queriedAt: new Date(),
}
}) satisfies GetFoo
Want results from more Discord servers?
Add your server