Yeah, so last night I thought of that, too and I ended up doing something like this for Delete
to maintain some sort of referential integrity (I couldn’t find any docs if DGraph provide support for that). In case it might help someone in the future here is a snippet of the code I use. The idea here is to avoid deleting R
if there are any O
s linked to it.
q := `
query Entity($xid: string) {
entity(func: eq(xid, $xid)) {
uid
}
}
`
resp, err := s.c.NewTxn().QueryWithVars(ctx, q, map[string]string{"$xid": uid.Value()})
if err != nil {
return mt.Errorf("query txn: %w", err)
}
var r struct {
Results []struct {
UID string `json:"uid,omitempty"`
} `json:"entity"`
}
if err = json.Unmarshal(resp.Json, &r); err != nil {
return fmt.Errorf("query result unmarshal: %w", err)
}
res := len(r.Results)
switch {
case res == 0:
return nil
case res > 1:
// NOTE: this should never happen
panic("ErrDuplicateNode")
}
node := map[string]string{"uid": r.Results[0].UID}
pb, err := json.Marshal(e)
if err != nil {
return fmt.Errorf("mutation marshal: %w", err)
}
mu := &dgapi.Mutation{
Cond: `@if(not type(Resource) OR eq(count(~resource), 0))`,
SetJson: pb,
}
...
...
}
The code for `Add`ition mutation looks similar-ish. These things are all doable indeed, but I'd love if they were simpler :-)
Also, the referential integrity support in DGraph would go a long way, too. Unless I missed it in any docs.