簡體   English   中英

GraphQL - 如何在查詢多個突變期間檢索先前突變的 id

[英]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?

有可能的?

如果我忘記了一些信息,請不要猶豫。

提前致謝。

編輯

  • 使用 PostGraphile,插件“postgraphile-plugin-nested-mutations”或“自定義突變”(帶有 PL PGSQL 功能)
  • 在沒有 PostGraphile 的情況下,作為 @xadm 示例的解析器允許這種特定的嵌套突變。

恕我直言,您可以搜索“嵌套突變”-此處未描述,您可以輕松找到示例/教程。

建議的數據庫結構(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
  }
}

...您可以從orderLinesorders表中簡單地獲取它(或者相當多的數組)[使用簡單的 SQL 查詢 - 將從orderLines中讀取price ]

已訂購最近解析器可以從父orderedRecently獲取產品id (通常是第一個參數)

當然,您可以(並且應該)將數據作為orderorderLine類型返回(單獨緩存,標准化):

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.ididrefproductquantity )。 解析器只能返回訂單id或結構化數據(如上)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM