# How to obtain success message from conditional upsert using Dgraph JS gRPC client?

**URL:** <https://discuss.dgraph.io/t/how-to-obtain-success-message-from-conditional-upsert-using-dgraph-js-grpc-client/6427>\
**Category:** Users\
**Tags:** mutation\
**Created:** [April 24, 2020, 6:41am UTC](https://discuss.dgraph.io/t/how-to-obtain-success-message-from-conditional-upsert-using-dgraph-js-grpc-client/6427 "2020-04-24T06:41:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![M.Lau](https://avatars.discourse-cdn.com/v4/letter/m/db5fbb/32.png) [@M.Lau](https://discuss.dgraph.io/u/M.Lau)\
**Post date:** [April 24, 2020, 6:41am UTC](https://discuss.dgraph.io/t/how-to-obtain-success-message-from-conditional-upsert-using-dgraph-js-grpc-client/6427/1 "2020-04-24T06:41:10Z")

</div>

Hi, after completed a conditional upsert using the Dgraph JS gRPC client, is it possible to get a response on whether the mutation was committed? There are 2 possible outcomes: 1. conditions not fulfilled so no commit. 2. conditions are fulfilled so commit the transaction. I am trying to distinguish these 2 outcomes. I tried getJson(), getMetrics(), …etc but I was not able to figure out how to check the status of the transaction after it was completed by the Dgraph db.

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [April 24, 2020, 4:15pm UTC](https://discuss.dgraph.io/t/how-to-obtain-success-message-from-conditional-upsert-using-dgraph-js-grpc-client/6427/2 "2020-04-24T16:15:51Z")

</div>

Please, share your query so I can check it and show what is missing.

---

<div class="post-metadata">

**Author:** ![M.Lau](https://avatars.discourse-cdn.com/v4/letter/m/db5fbb/32.png) [@M.Lau](https://discuss.dgraph.io/u/M.Lau)\
**Post date:** [April 27, 2020, 7:40am UTC](https://discuss.dgraph.io/t/how-to-obtain-success-message-from-conditional-upsert-using-dgraph-js-grpc-client/6427/3 "2020-04-27T07:40:39Z")

</div>

Here is my query:

```auto
upsert{
query {
    tree as var( func: uid(0xabc11) )
    {
      defects:~parent_tree @filter(type(Defect))
      {
          d as uid
      }              
    }

    var(func: uid($uid))
    {
      child_tree: ~parent_tree @filter(type(Tree))
      {
          ct as uid
      }              
    }
  }

mutate @if( eq( len(d), 0) AND eq( len(ct), 0) ){
     delete {
         uid(tree) * *.
     }
}
}

```

I like to get a confirmation message that the tree is delete from the client. However I am not able to do so. For example, when it fails the conditions and do not delete, the message i receive is still “success”.

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [April 27, 2020, 5:46pm UTC](https://discuss.dgraph.io/t/how-to-obtain-success-message-from-conditional-upsert-using-dgraph-js-grpc-client/6427/4 "2020-04-27T17:46:16Z")

</div>

Thanks for sharing it. I gonna answer in parts.

Take this query as an example

The main point to get the right responses from Dgraph, is to never use “var” block if you wanna see what you are doing.

```auto
upsert {
  query {
    q(func: eq(email, "user@company1.io")) {
      v as uid
    }
  }

  mutation {
    set {
      uid(v) <email> "user@company1.io" .
      uid(v) <age> "28" .
    }
  }
}

```

The first time you run it you will get this answer.

```auto
{
  "data": {
    "code": "Success",
    "message": "Done",
    "queries": {
      "q": []
    },
    "uids": {
      "uid(v)": "0x4e22"
    }
  }
}

```

As you can see, the “uids” field indicates that we have created a new node and it returns us a UID in the “uid (v)” key.

If you run the same query again, it gonna return this

```auto
{
  "data": {
    "code": "Success",
    "message": "Done",
    "queries": {
      "q": [
        {
          "uid": "0x4e22"
        }
      ]
    },
    "uids": {}
  }
}

```

As you can see, the “uids” field is empty and now we have a response in the “queries” field. This indicates that a new node was not created. “0x4e22” is the node that has the email “`user@company1.io`”.

For now, that’s the behavior you have to pay attention to.

See this issue to understand what happened in that context.  
[https://github.com/dgraph-io/dgraph/issues/4048](https://github.com/dgraph-io/dgraph/issues/4048)

Also, follow this

> <https://github.com/dgraph-io/dgraph/issues/4653>
>
> \## Experience Report
> 
> \### What you wanted to do
> 
> I would like to run an Upse…rt Block query and get the response to that operation right away.
> 
> \### What you actually did
> 
> Today https://github.com/dgraph-io/dgraph/pull/4210 covers a state before the mutation. However, this \`#4210\` feature does not seem to be useful except in cases for making some kind of comparisons.
> 
> \### Why that wasn't great, with examples
> 
> Following the same line of reasoning as https://github.com/dgraph-io/dgraph/issues/4048 obtaining a response after the execution of Upsert Query is essential for the developer to be aware of what is being performed in the procedure.
> 
> \### Any external references to support your case
> 
> GraphQL is an external example of this feature. GraphQL mutations can return (or not, it is optional) a new query with this mutation performed in mind.
> 
> \## Example
> 
> The result of the "Return Query" block must return values after the upsert query is executed. That is, return the result of the operation.
> 
> Format:
> 
> \`\`\`Graphql
> upsert {
> query \<query block\>
> \[fragment \<fragment block\>\]
> mutation \<mutation block 1\>
> \[mutation \<mutation block 2\>\]
> return query \<return query block\>
> \[return query \<return query block 2\>\]
> ...
> }
> \`\`\`
> 
> \`\`\`Graphql
> upsert {
> query {
> USER as var(func: eq(name, "abc")) {
> found as count(uid)
> }
> }
> mutation @if(eq(len(u), 0)) {
> set {
> uid(USER) \<name\> "abc" .
> uid(USER) \<dgraph.type\> "File" .
> }
> }
> return query {
> # The user can return as many blocks he needs.
> # This would be optional, If the user does not type "return query", then will return only the information we return todays.
> 
> #This first block is the one that will return the updated data
> me(func: uid(USER)) {
> uid
> expand(\_all\_)
> }
> 
> #This is a custom block - Everything here are just examples and would be optional.
> infoMe() { 
> mutation : (len(USER), 0) #This would return TRUE/FALSE if we support len() in the query body - it is just an idea
> total\_sum as sum: sum(val(found))
> mutation2 : math(total\_sum == 0 ) # This actually works in queries today. It will return TRUE.
> }
> 
> }
> }
> \`\`\`
> 
> \### Desired Result
> 
> \`\`\`JSON
> {
> "data": {
> "code": "Success",
> "message": "Done",
> "queries": {
> "var": \[\]
> },
> "uids": {},
> "return": {
> "me": \[
> {
> "name": "test",
> "email": "user22@company1.io",
> "age": 21
> }
> \],
> "infoMe": \[
> {
> "mutation": true,
> "total\_sum": 1,
> "\_mutation\_": true
> }
> \]
> }
> }
> }
> \`\`\`

---

<div class="post-metadata">

**Author:** ![Paras](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/paras/32/2768_2.png) [@Paras](https://discuss.dgraph.io/u/Paras)\
**Post date:** [April 27, 2020, 7:53pm UTC](https://discuss.dgraph.io/t/how-to-obtain-success-message-from-conditional-upsert-using-dgraph-js-grpc-client/6427/5 "2020-04-27T19:53:57Z")

</div>

I’d like to further add to clarify.

The conditional mutation may or may not have been executed depending on the “if” condition being satisfied. Both these are valid “successful” outcomes of the Upsert. Expecting an error when the condition is not met is incorrect assumption.

---

<div class="post-metadata">

**Author:** ![M.Lau](https://avatars.discourse-cdn.com/v4/letter/m/db5fbb/32.png) [@M.Lau](https://discuss.dgraph.io/u/M.Lau)\
**Post date:** [April 28, 2020, 5:37am UTC](https://discuss.dgraph.io/t/how-to-obtain-success-message-from-conditional-upsert-using-dgraph-js-grpc-client/6427/6 "2020-04-28T05:37:55Z")

</div>

Oic. Thanks for your reply.

---

<div class="post-metadata">

**Author:** ![M.Lau](https://avatars.discourse-cdn.com/v4/letter/m/db5fbb/32.png) [@M.Lau](https://discuss.dgraph.io/u/M.Lau)\
**Post date:** [April 28, 2020, 5:38am UTC](https://discuss.dgraph.io/t/how-to-obtain-success-message-from-conditional-upsert-using-dgraph-js-grpc-client/6427/7 "2020-04-28T05:38:22Z")

</div>

Ok. so it’s an open issue. Thanks for your help.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/2/2b38fdece2abe5814f2e51bee69d1e67fc304b0a.png) [@system](https://discuss.dgraph.io/u/system)\
**Post date:** [May 28, 2020, 5:38am UTC](https://discuss.dgraph.io/t/how-to-obtain-success-message-from-conditional-upsert-using-dgraph-js-grpc-client/6427/8 "2020-05-28T05:38:24Z")

</div>

Closed on May 28, 2020, 5:38am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
