dgraph-rs icon indicating copy to clipboard operation
dgraph-rs copied to clipboard

Upsert

Open snaumov opened this issue 4 years ago • 4 comments

Hi, thanks for the library!

Is there a way to run an Upsert type of query?

snaumov avatar Apr 11 '20 01:04 snaumov

Hey,

if you run Dgraph 1.1+ you can use Txn::do_request to perform request consisting of one query and one or more mutations, which should be identical to upsert {} block.

I'll document this and add some examples to the repository.

ondrowan avatar Apr 12 '20 18:04 ondrowan

@ondrowan thanks for your reply

When I try to run this piece of code

     let q = r#"
        upsert {
            query {
              v as var(func: type(Message))
            }
          
            mutation {
              delete {
                uid(v) <message_id> * .
                uid(v) <text> * .
                uid(v) <date> * .
                uid(v) <reply_to> * .
                uid(v) <uid> * .
              }
            }
          }
        "#.to_string();

        let mut request = Request {
            query: q.into(),
            ..Default::default()
        };

        let mut txn = self.db.new_readonly_txn();

        txn.do_request(&mut request)?;

        txn.commit()?;

I'm getting this response back:


Dgraph Grpc error: RpcFailure(RpcStatus { status: Unknown, details: Some("while lexing upsert {\n            query {\n              v as var(func: type(Message))\n            }\n          \n            mutation {\n              delete {\n                uid(v) <message_id> * .\n                uid(v) <text> * .\n                uid(v) <date> * .\n                uid(v) <reply_to> * .\n                uid(v) <uid> * .\n              }\n            }\n          } at line 1 column 0: Invalid operation type: upsert") })

(excuse bad formatting)

So it's complaining about upsert operation being invalid. As long as my q query is not malformed (which I'm not 100% positive about), probably issue might be with Grpc missing an operation?

snaumov avatar Apr 16 '20 14:04 snaumov

@snaumov I don't have my development env up and running now, so I might be mistaken, but I think you don't need to use upsert block when using do_request and it converts the whole request into upsert one.

ondrowan avatar Apr 18 '20 18:04 ondrowan

I checked it further and what I mentioned seems to be the case. You'll just have to manually set both query and mutations parts of Request and it'll end up converting it into upsert block internally. I find Dgraph docs to be confusing when it comes to upserts because the examples use HTTP client, but gRPC ones use different way of achieving the same result. Either way, once I have some time and energy, I'll add examples with this since it's confusing.

ondrowan avatar Apr 18 '20 18:04 ondrowan