Okay everyone … so I am still VERY new to using Dgraph. But, I wanted to be able to turn a python object …
class Region(Node):
area: int
population: int
name: str
borders: List[Region]
into a schema …
Region: bool @index(bool) .
_type: string .
area: int .
borders: uid .
name: string .
population: int .
Hmm … while I am at it, maybe we should also generate the mutations:
from pydiggy import generate_mutation, Facets
por = Region(uid=0x11, name="Portugal")
spa = Region(uid=0x12, name="Spain")
gas = Region(name="Gascony")
mar = Region(name="Marseilles")
por.borders = [spa]
spa.borders = [por, gas, mar]
gas.borders = [Facets(spa, foo='bar', hello='world'), mar]
mar.borders = [spa, gas]
por.stage()
spa.stage()
gas.stage()
mar.stage()
print(generate_mutation())
Prints out
{
set {
<0x11> <Region> "true" .
<0x11> <_type> "Region" .
<0x11> <name> "Portugal" .
<0x11> <borders> <0x12> .
<0x12> <Region> "true" .
<0x12> <_type> "Region" .
<0x12> <name> "Spain" .
<0x12> <borders> <0x11> .
<0x12> <borders> _:unsaved.0 .
<0x12> <borders> _:unsaved.1 .
_:unsaved.0 <Region> "true" .
_:unsaved.0 <_type> "Region" .
_:unsaved.0 <name> "Gascony" .
_:unsaved.0 <borders> <0x12> (foo="bar", hello="world") .
_:unsaved.0 <borders> _:unsaved.1 .
_:unsaved.1 <Region> "true" .
_:unsaved.1 <_type> "Region" .
_:unsaved.1 <name> "Marseilles" .
_:unsaved.1 <borders> <0x12> .
_:unsaved.1 <borders> _:unsaved.0 .
}
}
Maybe also go the other way. A Dgraph result into python objects.
>>> data = hydrate(retrieved_data)
{'allRegions': [<Region:17>, <Region:18>, <Region:19>, <Region:20>]}
Now, this is in no way production worthy. More a proof of concept at this stage. And, since I am still new to Dgraph, I am not even sure if I am following best practices yet. Still plenty of work to go, but any thoughts on what I should or should not be doing would be welcome.