dgraph-rs
dgraph-rs copied to clipboard
Upsert
Hi, thanks for the library!
Is there a way to run an Upsert
type of query?
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 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 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.
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.