0

我想做的事

防止 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
4

1 回答 1

0

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

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

于 2022-01-28T06:51:33.773 回答