M.Lau
(Marcus Lau)
May 4, 2020, 9:20am
1
How can I transfer edges from one predicate to another predicate using upsert?
for example
upsert{
query{
tree as q(uid(uid_a, uid_b))
{
parent_uid as parent_tree
}
}
mutation{
set{
uid(tree) <new_predicate> uid(parent_uid)
}
}
}
Schema for parent tree is
parent_tree [uid] @reverse
the desired effect is that for each node uid_a
take the parent_tree uids and transfer them into uid_a 's new_predicate
However, when I ran the above upsert, it actually takes both the parent_uid of uid_a and uid_b and insert them to both uid_a’s and uid_b’s new_predicate
so instead of
uid_a <new_predicate> [ only uid_a’s original parent_tree uid ]
I get
uid_a <new_predicaate> [ uid_a’s original parent_tree uid AND uid_b’s original_parent_tree uid ]
is there anyway to do for each node?
I don’t think thats possible. So I had created this issue
opened 02:38AM - 30 Apr 20 UTC
kind/feature
exp/expert
status/needs-attention
area/upsert
area/querylang/function
dgraph
## Experience Report
### What you wanted to do
I would like to use loops i… n queries and upsert blocks. In this case something similar to "foreach".
### What you actually did
There's nothing similar to this in Dgraph right now.
### Why that wasn't great, with examples
A loop like foreach would be very useful. There are situations that we want to iterate over objects in a query. Whether to display a particular form structure in the response or to create specific mutations in the upsert block. Which is not currently supported in Dgraph.
##### Syntax
I believe that Syntax from foreach would be very similar to the K-shortest path.
##### More examples
I think a "foreach" func would be the solution for almost all the things like "groupby value".
Ref of a DB doing something similar to it:
https://neo4j.com/docs/cypher-manual/current/clauses/foreach/
https://docs.mongodb.com/manual/reference/method/cursor.forEach/
A foreach loop could solve this issue #4779 in a blink of an eye (eliminating the misuse of an extra block). I gonna add a comment there with an example using "foreach".
> see https://github.com/dgraph-io/dgraph/issues/4779#issuecomment-621579502
In this link https://discuss.dgraph.io/t/foreach-func-in-graphql-loops-in-bulk-upsert/5533 I have other use cases. Most of them related to upsert block, issues with Facets bad responses (e.g: #4160), and so on.
>Pay attention that this link is a little old. I used different syntax just to illustrate.
> Reference for the group by issue: https://github.com/dgraph-io/dgraph/issues/4170
e.g: "for each group found in groupby create a new object to use in response".
```
foreach(in: PARAM1, title: PARAM2).
```
```
> For each UID in PARAM1, create a new object in the response with the title in PARAM2
```
```
{
var(func: has(kind)) @groupby(kind) {
T as count(uid)
}
foreach(func: foreach(in: T, title: kind)) {
name
age
total : val(T)
}
}
```
#### Desirable Result
```JSON
{
"data": {
"q": [
{
"dog": [
{
"total": 1
},
{
"uid": "0x1",
"name": "Bingo",
"age": "3"
}
]
},
{
"animal": [
{
"total": 2
},
{
"uid": "0x1",
"name": "Bingo",
"age": "3"
},
{
"uid": "0x3",
"name": "Angry Purr",
"age": "1"
}
]
},
{
"cat": [
{
"total": 1
},
{
"uid": "0x3",
"name": "Angry Purr",
"age": "1"
}
]
}
]
}
```
### Any external references to support your case
https://discuss.dgraph.io/t/foreach-func-in-graphql-loops-in-bulk-upsert/5533
Share there your thoughts and use cases.
Cheers.