[英]GraphQL - How retrieve id of previous mutation, during query of multiple mutations
我想在同一個查詢中運行多個突變。
在下面的示例中,我創建了一個訂單,然后創建了一個產品記錄,涉及先前創建的。
我必須有2個突變。
首先,我插入一個訂單。 在 output 中,我檢索了 idorder 等。
然后,我插入一個產品。 這個產品
mutation {
createOrder(input: {
order: {
ordername: "My order"
}
}) {
order {
idorder
ordername
}
},
createProduct(input: {
product: {
quantity: 3
idrefproduct: 25 # link to refProduct
idorder: XXXX # how can i retrieve idorder from output of createOrder above ? 🤔
}
}) {
product {
idproduct
}
}
}
SQL 結構的真實示例:
user(iduser, othersFields);
scenarios(idscenario, iduser, name, otherFields);
cultA(idcultA, idscenario, ...); // this table need of idscenario field
cultB(idcultB, idscenario, ...); // this table need of idscenario field
cultC(idcultC, idscenario, ...); // this table need of idscenario field
如何從上面的 createOrder 的 output 中檢索 idorder?
有可能的?
如果我忘記了一些信息,請不要猶豫。
提前致謝。
編輯:
恕我直言,您可以搜索“嵌套突變”-此處未描述,您可以輕松找到示例/教程。
建議的數據庫結構(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
和orders
表中簡單地獲取它(或者相當多的數組)[使用簡單的 SQL 查詢 - 將從orderLines
中讀取price
]
已訂購最近解析器可以從父orderedRecently
獲取產品id
(通常是第一個參數)
當然,您可以(並且應該)將數據作為order
和orderLine
類型返回(單獨緩存,標准化):
query product($id: ID!) {
product(id: $id) {
id
orderedRecently {
id
date
orderLine {
id
amount
price
}
}
}
}
where type orderedRecently: [Order!]
- 數組可以為空,尚未排序
我稍微誤解了你的要求(命名約定)......你已經有了正確的數據庫結構。 突變可以用復雜的數據/輸入“喂養”:
mutation {
createOrder(input: {
order: {
ordername: "My order"
products: [
{
quantity: 3
idrefproduct: 25
},
{
quantity: 5
idrefproduct: 28
}
]
}
}) {
order {
id
ordername
products {
id
idrefproduct
quantity
}
}
}
}
你的product
是我的訂單idrefproduct
orderLine
product
。
createOrder
創建/插入order
,然后使用其id
創建產品記錄( order.id
、 idrefproduct
和quantity
)。 解析器只能返回訂單id
或結構化數據(如上)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.