Let say I have a User with Posts, and I expose DELETE /posts/{postId}
as REST API.
Obviously, I don’t want people to delete other people’s post, so I check that the passed postId
belongs to the currently logged in user before telling Dgraph to delete the post. How do I do that in one transaction?
I tried the approach below because I thought the whole process would be aborted if POST_ID(123)
doesn’t belong to USER_ID(john)
, but apparently Dgraph doesn’t check for that.
{
"delete": [
{
"uid": "USER_ID(john)",
"post": [
{
"uid": "POST_ID(123)"
}
]
},
{
"uid": "POST_ID(123)"
}
]
}
MichelDiz
(Michel Diz)
July 28, 2019, 5:02pm
2
You open a transaction, do a query, do your checks in the application side and then use the same transaction to do a mutation based on your checks.
Dgraph never checks anything about that. Unless of course if you use upsert procedure.
BTW Soon you gonna be able to do it in a single operation, using upsert block.
opened 08:21PM - 22 Feb 19 UTC
closed 04:13PM - 08 Aug 19 UTC
kind/feature
This operation would allow a user to run a query, and use its results (via varia… bles) to execute the mutation. This would then allow upserts to happen, without doing multiple network calls to the DB.
```
txn {
query {
me(func: eq(email, "someone@gmail.com")) {
v as uid
}
}
mutation @if(eq(len(v), 0)) {
set {
<uid(v)> <name> "Some One" .
<uid(v)> <email> "someone@gmail.com" .
}
}
}
```
This txn would check for an account with `email=someone@gmail.com`, and only if it is present, would
it run the mutation.
This should return the result of query, normally. And also return the result of the mutation.
Additionally, we should be able to optionally do the mutation, iff the email is already present.
```
txn {
query {
me(func: eq(email, "someone@gmail.com")) {
v as uid
}
}
mutation @if(gt(len(v), 0)) {
set {
<uid(v)> <name> "Changed Name" .
}
}
}
```
This would introduce a new txn operator, and a new if directive applicable for mutations.
system
(system)
Closed
August 27, 2019, 5:02pm
3
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.