【问题标题】:Dql mutation add duplicate recordsdql突变添加重复记录
【发布时间】:2021-11-25 01:31:39
【问题描述】:

我想做什么

防止dql突变添加重复记录

我做了什么

我添加了一个 graphql 架构:

type Product {
    id: ID!
    name: String! @id @dgraph(pred: "Product.name")
    slug: String! @id @dgraph(pred: "Product.slug")
    image: String @dgraph(pred: "Product.image")
    created_at: DateTime! @dgraph(pred: "Product.created_at")
    updated_at: DateTime! @dgraph(pred: "Product.updated_at")
}

上面的 graphql 架构已经生成了下面的 DQL 架构:

<Product.created_at>: datetime .
<Product.image>: string .
<Product.name>: string @index(hash) @upsert .
<Product.slug>: string @index(hash) @upsert .
<Product.updated_at>: datetime .
<dgraph.drop.op>: string .
<dgraph.graphql.p_query>: string @index(sha256) .
<dgraph.graphql.schema>: string .
<dgraph.graphql.xid>: string @index(exact) @upsert .
type <Product> {
    Product.name
    Product.slug
    Product.image
    Product.created_at
    Product.updated_at
}
type <dgraph.graphql> {
    dgraph.graphql.schema
    dgraph.graphql.xid
}
type <dgraph.graphql.persisted_query> {
    dgraph.graphql.p_query
}

我使用:https://github.com/dgraph-io/dgo#running-a-mutation 运行突变以添加一些数据。 但它不尊重 @id 添加到架构中的某些字段,如“slug”和“name”。

使用 graphql 突变,这是有效的,并通过返回错误来尊重唯一性:"message": "couldn't rewrite mutation addProduct because failed to rewrite mutation payload because id aaaa already exists for field name inside type Product"

dgraph version v21.03.2

【问题讨论】:

    标签: dgraph


    【解决方案1】:

    在 Dql 中,您必须使用 Upsert Block 自行处理。

    或者,如果您不想要 dql 的强大功能,您可以使用自动处理这些东西的 graphql。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-20
      • 2020-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多