【问题标题】:GraphQL - How retrieve id of previous mutation, during query of multiple mutationsGraphQL - 如何在查询多个突变期间检索先前突变的 id
【发布时间】:2020-07-30 19:00:09
【问题描述】:

我想在同一个查询中运行多个突变。

在下面的示例中,我创建了一个订单,然后我创建了一个产品记录,与之前创建的有关。

我必须有 2 个突变。

首先,我插入一个订单。在输出中,我检索了 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 输出中检索 idorder ? ????

有可能吗?

如果我忘记了一些信息,请不要犹豫。

提前致谢。

编辑

  • 使用 PostGraphile,插件“postgraphile-plugin-nested-mutations”或“自定义突变”(带有 PL PGSQL 函数)
  • 如果没有 PostGraphile,解析器(如 @xadm 的示例)允许这种特殊的嵌套突变。

【问题讨论】:

  • 可能正在寻找“嵌套突变”
  • 我看到了“嵌套突变”,但是我没有找到我的用例。我想真正检索按顺序生成的 id,因为我需要它来运行在同一查询中的产品和其他表中执行插入。您是否已经在“嵌套突变”中使用了变量?

标签: graphql postgraphile


【解决方案1】:

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

建议的数据库结构(n-to-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 查询-其中price 将从orderLines 读取]

orderedRecently 解析器可以从父对象(通常是第一个参数)获取产品id

当然,您可以(并且应该)将数据返回为 orderorderLine 类型(单独缓存,标准化):

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

其中类型 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是我的orderLineidrefproductproduct

createOrder 创建/插入order,然后使用其id 创建产品记录(order.ididrefproductquantity)。解析器只能返回订单id 或结构化数据(如上)。

【讨论】:

  • 感谢您的帮助,您花时间陪我,很好。但是,我不明白很多想法。我的例子就是一个例子。我想检索“订单 ID”,因为之后,我将在多个表(表 A、B、C、...)中的多次插入中使用它。在您的解释中,您写了“查询”,但我们谈论的是突变,我迷路了。
  • 网络上的示例与我的用例不匹配,恕我直言。我经常看到“我插入文章,然后我在里面插入子对象标签文章。这很好!”
  • 正如我所写的......这不是关于嵌套突变的解释......是的,这很好,因为标签不需要article_id......您的产品不需要order_id,同样......重新设计表格/关系...或显示结构/关系
  • 感谢您的耐心等待。我不是graphQL专家。我现在要编辑我的第一篇文章,包括表格和关系。
  • 您好@xadm,感谢您的帮助,这是真的,我的示例并不明确。我读了你的更新回复。我更明白了。我会研究一下,明天我会给出反馈。再次感谢。周日愉快。 gql mutation { createOrder(input: { order: { ordername: "My order" products: [ { ... }, ], cultA: [ { ... }, ] } }) { order { id ordername products { id idrefproduct quantity } } } }
猜你喜欢
  • 2017-11-10
  • 2017-08-24
  • 2018-09-17
  • 2019-05-24
  • 2016-01-14
  • 2021-09-09
  • 2022-01-13
  • 2022-11-25
  • 1970-01-01
相关资源
最近更新 更多