Reputation: 99
I'm experimenting with dgraph and so far my biggest struggle is to create an edge between two objects without prior knowledge of their uids (for bulk loading). Example - let's have two types - parent and child, the only difference is that child is always a leaf node, so the schema may be something like
<name>: string .
<age>: int .
<children>: [uid] .
type Parent {
name
age
children
}
type Child {
name
age
}
Now I would like to insert three nodes - two parents and one child - and create an edge between them, all that using one query, without prior querying uid. I imagine something like this:
{
"set": [
{
"name": "Kunhuta",
"age": 38,
"dgraph.type": "Parent"
},
{
"name": "Vendelin",
"age": 41,
"dgraph.type": "Parent"
},
{
"name": "Ferko",
"age": 14,
"dgraph.type": "Child",
"Parent": [
{
"name": "Kunhuta"
},
{
"name": "Vendelin"
}
]
}
]
}
(Suppose names are unique type identifiers)
Is it possible to somehow do so in dgraph?
Upvotes: 0
Views: 677
Reputation: 2133
So if you are adding new nodes to the graph and wish to connect them to each other with new edges you can do this with blank nodes.
From the dgraph docs: https://dgraph.io/docs/v1.0.8/mutations/
Blank nodes in mutations, written _:identifier, identify nodes within a mutation
Here's your example with blank nodes added:
{
"set": [
{
"uid": "_:kunhuta",
"name": "Kunhuta",
"age": 38,
"dgraph.type": "Parent"
},
{
"uid": "_:vendelin",
"name": "Vendelin",
"age": 41,
"dgraph.type": "Parent"
},
{
"uid": "_:ferko",
"name": "Ferko",
"age": 14,
"dgraph.type": "Child",
"Parent": [
{
"uid": "_:kunhuta",
"name": "Kunhuta"
},
{
"uid": "_:vendelin",
"name": "Vendelin"
}
]
}
]
}
Things get a bit more difficult when you want to connect a new node to an existing one in the graph. For example say you committed the above mutation then you wanted to add a new child to "Kunhuta". You would need to query the existing uid
of "Kunhuta" and use that uid on a new set mutation to add your child.
Also I would use some form of unique identifier outside of names to help query for the uid
. If you ended up adding another "Kunhuta" in your graph how would you know which is the real parent? For this reason dgraph recommends adding an external identifier (usually a uuid) and calling it xid
in your schema.
Upvotes: 0