如何使用自定义属性描述多对多 GrhapQl 关系的模式?
大家好!我有下表:
+-------------+--------------+----------------------------+
| Product |
+-------------+--------------+----------------------------+
| id | int(11) | PRIMARY KEY AUTO_INCREMENT |
| name | varchar(255) | |
+-------------+--------------+----------------------------+
+-------------+--------------+----------------------------+
| Nutrient |
+-------------+--------------+----------------------------+
| id | int(11) | PRIMARY KEY AUTO_INCREMENT |
| name | varchar(255) | |
| unit | varchar(255) | |
| alias | varchar(255) | |
+-------------+--------------+----------------------------+
+-------------+--------------+----------------------------+
| Product_To_Nutrient |
+-------------+--------------+----------------------------+
| nutrientId | int(11) | PRIMARY KEY FOREIGN KEY |
| productId | int(11) | PRIMARY KEY FOREIGN KEY |
| amount | int(11) | |
+-------------+--------------+----------------------------+
为了获取营养,我使用数据加载器。
如何描述我的架构,以便我可以运行以下查询并获取每种营养素的 AMOUNT?或者也许我需要重建我的数据库,如果是这样,哪种架构是最佳的?预先感谢您的建议:)
query Product {
getProducts {
id,
name,
nutrients: {
name,
unit,
alias,
AMOUNT
}
}
我的堆栈:TypeOrm、TypeGraphQL、MicroOrm、NodeJs。
Product_To_Nutrient
表重命名为 ingredients
(只是为了方便)unit
属性从 nutritient
移至您的 ingredient
,因为了解 quantity
中的 units
很重要。 (在当前情况下,如果您需要更新 unit
,您还必须使用该 ingredients
更新所有 nutritient
。使用 GraphQL 进行查询,您必须通过
ingredients
query Product {
getProducts {
id,
name,
ingredients: {
nutrient: {
id,
name,
alias
},
quantity,
unit
}
}
}
要加载
ingredient
,查询产品时,可以使其成为“热切”关系。
使用
Dataloader
加载链接到 nutrients
的所有 ingredient
。