简体   繁体   English

GraphQL - 如何在查询多个突变期间检索先前突变的 id

[英]GraphQL - How retrieve id of previous mutation, during query of multiple mutations

i would like run multiple mutations in the same query.我想在同一个查询中运行多个突变。

In the example below, i create an order and after i create a product record, concerning previously created.在下面的示例中,我创建了一个订单,然后创建了一个产品记录,涉及先前创建的。

I must have 2 mutations.我必须有2个突变。

First, i insert an order.首先,我插入一个订单。 In output, i retrieve among others, idorder.在 output 中,我检索了 idorder 等。

Then, i insert an product.然后,我插入一个产品。 This product这个产品

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
    }
  }
}

Real example with SQL structure: 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

how can i retrieve idorder from output of createOrder above?如何从上面的 createOrder 的 output 中检索 idorder?

It is possible?有可能的?

If i forgot some informations, don't hesitate.如果我忘记了一些信息,请不要犹豫。

Thanks in advance.提前致谢。

EDIT :编辑

  • With PostGraphile, plugin "postgraphile-plugin-nested-mutations" or "custom mutations" (with PL PGSQL function)使用 PostGraphile,插件“postgraphile-plugin-nested-mutations”或“自定义突变”(带有 PL PGSQL 功能)
  • Without PostGraphile, a resolver as the example of @xadm permits this particular nested mutation.在没有 PostGraphile 的情况下,作为 @xadm 示例的解析器允许这种特定的嵌套突变。

IMHO you can search for "nested mutations" - not described here, you'll easily find examples/tutorials.恕我直言,您可以搜索“嵌套突变”-此处未描述,您可以轻松找到示例/教程。

Proposed DB structure (n-to-n relation):建议的数据库结构(n 对 n 关系):

order{orderID,lines[{orderLineID}] } > 
  order_line{orderLineID, productID, anount, price} > 
    product {productID}

... created in nested mutations (in reverse order product>order_line>order) ...在嵌套突变中创建(以相反的顺序 product>order_line>order)

Product don't need orderID , but when you ask for it [in product resolver]产品不需要orderID ,但是当您要求时 [在产品解析器中]

query product(id) {
  id
  orderedRecently {
    orderID
    date
    price
  }
}

... you can simply get it (or rather many - array) from orderLines and orders tables [using simple SQL query - where price will be read from orderLines ] ...您可以从orderLinesorders表中简单地获取它(或者相当多的数组)[使用简单的 SQL 查询 - 将从orderLines中读取price ]

orderedRecently resolver can get product id from parent object (usually 1st param)已订购最近解析器可以从父orderedRecently获取产品id (通常是第一个参数)

Of course you can (and should) return data as order and orderLine types (to be cached separately, normalized):当然,您可以(并且应该)将数据作为orderorderLine类型返回(单独缓存,标准化):

query product($id: ID!) {
  product(id: $id) {
    id
    orderedRecently {
      id
      date
      orderLine {
        id
        amount
        price
      }
    }
  }
}

where type orderedRecently: [Order!] - array can be empty, not eordered yet where type orderedRecently: [Order!] - 数组可以为空,尚未排序

update更新

I slightly misunderstood your requirements (naming convention)... you already have proper db structure.我稍微误解了你的要求(命名约定)......你已经有了正确的数据库结构。 Mutation can be 'feeded' with complex data/input:突变可以用复杂的数据/输入“喂养”:

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 .你的product是我的订单idrefproduct orderLine product

createOrder creates/inserts order and then use its id for creation of product records ( order.id , idrefproduct and quantity ). createOrder创建/插入order ,然后使用其id创建产品记录( order.ididrefproductquantity )。 Resolver can return only order id or structured data (as above).解析器只能返回订单id或结构化数据(如上)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM