I have data represented as a List(CSV),
I parse this list and insert the elements(different Kind) into dgraph,
then I want to query Elements with a relation to each other.
Let me try to explain, Bottles and Caps with
- brand: A, height: 10, bottleID: 1, type: bottle
- brand: B, height: 12, bottleID: 2, type: bottle
- brand: C, weight: 1, bottleID: 1, type: cap
I have following schema:
brand: string @index(hash).
bottleID: int @index(int) .
weight: int.
height: int.
cap: [uid] @ reverse.
type bottle {
brand
height
bottleID
cap
}
type cap {
brand
weight
bottleID
}
my elements are persisted as:
[{
"dgraph.type": "bottle",
"height":10,
"bottleID":1,
"brand": "A"
},
{
"dgraph.type": "bottle",
"height":12,
"bottleID":2,
"brand": "B"
},
{
"dgraph.type": "cap",
"weight":1,
"bottleID":1,
"brand": "C"
}]
what I want as query response for bottles:
[{
"dgraph.type" : "bottle",
"height":10,
"brand": "A",
"cap": {
"dgraph.type" : "cap",
"weight":1,
"brand": "C"
}
},
{
"dgraph.type" : "bottle",
"height":12,
"brand": "B"
}]
to achive this I query:
{
var (func: type("bottle")) {
A as bottleID
uid
},
caps(func: type("cap"), orderasc: bottleID)@filter(eq(bottleID,val(A))) {
B as bottleID
uid
},
bottles(func: type("bottle"), orderasc: bottleID) @filter(eq(bottleID,val(B))){
bottleID
uid
}
}
this returns me
"caps": [{
"bottleID": 1,
"uid": "0x25185"
}],
"bottles": [{
"bottleID": 1,
"uid": "0x25183"
}]
then I iterate over the to lists and add:
{"set":[ {
"uid": "0x25183",
"cap" : {
"uid": "0x25185"
}
}]}
for each pair.
then I can query:
{
bottles(func: type("bottle")) {
brand,
height,
cap {
brand
weight
}
}
}
then I get what I want:
"bottles": [
{
"brand": "A",
"height": 10,
"cap": {
"brand": "C",
"weight": 1
}
},{
"brand": "B",
"height": 12
}
]
How could I query this, without adding the relation for cap?