恕我直言,您可以搜索“嵌套突变” - 此处未描述,您可以轻松找到示例/教程。
建议的数据库结构(n对n关系):
order{orderID,lines[{orderLineID}] } >
order_line{orderLineID, productID, anount, price} >
product {productID}
...在嵌套突变中创建(按相反顺序product>order_line>order)
产品不需要orderID
,但是当你要求它时[在产品解析器中]
query product(id) {
id
orderedRecently {
orderID
date
price
}
}
...你可以简单地从以下位置获取它(或者更多 - 数组)orderLines
and orders
表 [使用简单的 SQL 查询 - 其中price
将从中读取orderLines
]
orderedRecently
解析器可以获得产品id
来自父对象(通常是第一个参数)
当然,您可以(并且应该)将数据返回为order
and orderLine
类型(单独缓存,标准化):
query product($id: ID!) {
product(id: $id) {
id
orderedRecently {
id
date
orderLine {
id
amount
price
}
}
}
}
在哪里输入orderedRecently: [Order!]
- 数组可以为空,尚未重新排序
update
我稍微误解了你的要求(命名约定)......你已经有了正确的数据库结构。突变可以用复杂的数据/输入来“喂养”:
mutation {
createOrder(input: {
order: {
ordername: "My order"
products: [
{
quantity: 3
idrefproduct: 25
},
{
quantity: 5
idrefproduct: 28
}
]
}
}) {
order {
id
ordername
products {
id
idrefproduct
quantity
}
}
}
}
Your product
is my orderLine
, idrefproduct
is product
.
createOrder
创建/插入order
然后使用它的id
用于创建产品记录(order.id
, idrefproduct
and quantity
)。解析器只能返回订单id
或结构化数据(如上所述)。