[英]Query relationships within Apollo/Client
I am trying to query my categories within my product.我正在尝试查询我的产品中的类别。 I created a table in which I want to see the categories of my product table.我创建了一个表,我想在其中查看我的产品表的类别。 I am using Prisma as my typeORM and Apollo as a way to query my Postgres database.我使用 Prisma 作为我的 typeORM,使用 Apollo 作为查询我的 Postgres 数据库的方式。 Please see the attached photo of my table.请参阅我的桌子所附的照片。 I have categories that are within my Product model.我的产品 model 中有类别。
Here is my query function using my UseQuery Hook from Apollo/Client.这是我使用来自 Apollo/Client 的 UseQuery Hook 的查询 function。
const AllProductsQuery = gql`
query {
products {
id
name
description
img
price
ingredients
categories {
id
name
}
}
}
`
What I thought this would do was since I am querying the categories within the products I would be able to hit that information.我认为这会做的是,因为我正在查询产品中的类别,所以我能够找到该信息。 However, it isn't even console logging that data.但是,它甚至没有控制台记录该数据。
This is what I have这就是我所拥有的
<Table striped bordered hover >
<thead>
<tr>
<th>Product</th>
<th>Category</th>
<th>Price</th>
</tr>
</thead>
{data && data.products.map((product: Product, categories: Category) => {
return (
<tbody>
<tr>
<td>{product.name}</td>
<td>{product.categories.name}</td>
<td>{product.price}</td>
</tr>
</tbody>
)
})}
</Table>
However this doesn't even show the categories information within my products within the console log.但是,这甚至没有在控制台日志中显示我的产品中的类别信息。 The Product.name and Product.price work perfectly however, I try to run the same thing with product.categories.name doesn't work. Product.name 和 Product.price 完美地工作但是,我尝试使用 product.categories.name 运行相同的东西不起作用。 Any information would be great!任何信息都会很棒!
In this code that you sent you're renaming product
to Product
in the data.products.map
.在您发送的这段代码中,您将data.products.map
中的product
重命名为Product
。
If you received categories from server, as I asked you in the comment, try this code如果你从服务器收到类别,正如我在评论中问你的那样,试试这个代码
<Table striped bordered hover>
<thead>
<tr>
<th>Product</th>
<th>Category</th>
<th>Price</th>
</tr>
</thead>
{data &&
data.products.map((product) => {
return (
<tbody>
<tr>
<td>{product.name}</td>
<td>{product.categories.name}</td>
<td>{product.price}</td>
</tr>
</tbody>
);
})}
</Table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.