Hello I’m just tarting to use dgraph and while trying to replicate something, i can’t make it work.
I will try to explain my problem below:
What I want to do
update parent node while inserting new child node
What I did
- Create schema like below :
type House{
name: String
people: [Person] @hasInverse(field: house)
}
type Person{
name: String
house: House
}
Using python client i added mutations:
- Adding house with people:
client_stub = pydgraph.DgraphClientStub('localhost:9080')
client = pydgraph.DgraphClient(client_stub)
txn = client.txn()
p = {
'House.name': 'House of Bob',
'House.people' : [ {'Person.name' : "Alice"} , {'Person.name' : "Joey"}]
}
mu = pydgraph.Mutation(set_json=json.dumps(p).encode('utf8'))
txn.mutate(mu)
txn.commit()
After indexing House.name i can use following query to see house + people relations:
{
q (func : eq(House.name, "House of Bob")) {
House.house_name
House.people
{
Person.name
}
}
}
i was able to see nodes with connections
- Adding person with house :
client_stub = pydgraph.DgraphClientStub('localhost:9080')
client = pydgraph.DgraphClient(client_stub)
txn = client.txn()
p = {
'Person.name' : 'John',
'Person.house' : {'House.name' : 'House of Bob'}
}
mu = pydgraph.Mutation(set_json=json.dumps(p).encode('utf8'))
txn.mutate(mu)
txn.commit()
Once i try to add person like this, brand new “House of Bob” appears and i end up having 2 “House of Bob”
1 house with 2 people and 1 house with 1 person
I think i should be inserting uid of {‘House.name’ : ‘House of Bob’} to ‘Person.house’ like
‘Person.house’ : “uid”
But im not sure how to do it after reading https://dgraph.io/docs/graphql/schema/graph-links/.
Thanks in advance!