selmeci / dgraph-tonic

Async/Sync gRPC client for Dgraph DB
MIT License
83 stars 8 forks source link

After mutate, how to get the new record or uid ? #17

Closed pandagood closed 4 years ago

pandagood commented 4 years ago

Hi I encountered some problems when using txn.mutate. This is my code:

let post = json!({
        "content":"I'm creating post...",
        "entities":{
            "mentions":[
                {"uid": "0x4e26"}
            ],
            "urls":[
                { "url": "https://github.com/chronotope/chrono", "size": 235334 }
            ]
        },
        "medias":{
            "video":{
                "media_type": "Video",
                "url": "https://pbs.twimg.com/media/EaoKttmXsAEQcK5",
            },
            "photos":[{
                "media_type": "Video",
                "url": "https://pbs.twimg.com/media/EaoKttmXsAEQcK5",
            }]
       }
});

let mut mu = Mutation::new();
mu.set_set_json(& post)?;
let mut txn = self.client.new_mutated_txn();
let response = txn.mutate(mu).await?;
txn.commit().await?;

println!("{:?}", response);
println!("{:?}", response.uids);

I want to get the new post after mutate, but the response did not return it.

So I can only get the uid of the new post, but I got the uids map:

{"dg.685636434.37": "0xc377", "dg.685636434.40": "0xc375", "dg.685636434.35": "0xc376", "dg.685636434.38": "0xc374", "dg.685636434.39": "0xc373", "dg.685636434.36": "0xc378"}

I don’t know which is the post uid at all, and I don’t know how to get it

Please help me, thanks.

selmeci commented 4 years ago

Hi,

we're going to add some examples to Readme asap.

selmeci commented 4 years ago

@zhihelive Can you please try this example before I will create example in readme?

let post = json!({
        "uid": "_:root"
        "content":"I'm creating post...",
        "entities":{
            "uid": "_:entities"
            "mentions":[
                {"uid": "0x4e26"}
            ],
            "urls":[
                { "url": "https://github.com/chronotope/chrono", "size": 235334 }
            ]
        },
        "medias":{
            "video":{
                "media_type": "Video",
                "url": "https://pbs.twimg.com/media/EaoKttmXsAEQcK5",
            },
            "photos":[{
                "media_type": "Video",
                "url": "https://pbs.twimg.com/media/EaoKttmXsAEQcK5",
            }]
       }
});

let mut mu = Mutation::new();
mu.set_set_json(& post)?;
let mut txn = self.client.new_mutated_txn();
let response = txn.mutate(mu).await?;
txn.commit().await?;

println!("{:?}", response);
println!("{:?}", response.uids);

Dgraph (not this client) assign to nodes without uid random names, like you get in your example.

{"dg.685636434.37": "0xc377", "dg.685636434.40": "0xc375", "dg.685636434.35": "0xc376", "dg.685636434.38": "0xc374", "dg.685636434.39": "0xc373", "dg.685636434.36": "0xc378"}

When you add uid: "_variable_name" to mutations nodes, you should see something like this:

{"root": "0xc377", "entities": "0xc375", "dg.685636434.35": "0xc376", "dg.685636434.38": "0xc374", "dg.685636434.39": "0xc373", "dg.685636434.36": "0xc378"}
selmeci commented 4 years ago

Dgraph documentation about uids: https://dgraph.io/docs/mutations/#setting-literal-values

pandagood commented 4 years ago

@selmeci I did what you said, this is feasible and solved my problem, thank you very much.