dgraph

Dql mutation add duplicate records


What I want to do

Prevent the dql mutation to add duplicates records

What I did

I add a graphql schema:

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

the above graphql schema has generated the bellow DQL schema:

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

I run a mutation to add some data using: https://github.com/dgraph-io/dgo#running-a-mutation. But it does not respect the @id added to the schema to some fields like "slug" and "name".

Using the graphql mutation this is working and respect the uniqueness by returning an error:"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

Solution

  • In Dql you have to handle this by your own using Upsert Block.

    Or if you don't want the power of dql you can use the graphql which handle this stuff automatically.