I was looking at this yesterday a bit and thinking I could maybe simplify it to something like this:
q := `
{
node(func: eq(xid, "` + uid.Value() + `")) {
u as uid
}
}
`
node := map[string]string{"uid": "uid(u)"}
// Only delete Resource if it has no inbound edges
cond := `@if(not type(Resource) OR eq(count(~resource), 0))`
pb, err := json.Marshal(node)
if err != nil {
return nil, fmt.Errorf("json marshal: %w", err)
}
mu := &dgapi.Mutation{
Cond: cond,
SetJson: pb,
}
req := &dgapi.Request{
Query: query,
Mutations: []*dgapi.Mutation{mu},
CommitNow: true,
}
_, err = s.c.NewTxn().Do(context.Background(), req)
if err != nil {
return fmt.Errorf("txn Delete: %w", err)
}
But when I try to do this I get the following error:
2021/02/20 11:08:55 txn Delete: rpc error: code = Unknown desc = Some variables are defined but not used
Defined:[__dgraph__0 u]
Used:[__dgraph__0]
This, I assume, means that u
does not seem to be used, however, it actually is?
node := map[string]string{"uid": "uid(u)"}
pb, err := json.Marshal(node)
if err != nil {
return nil, fmt.Errorf("json marshal: %w", err)
}
I’m not quite sure what the problem is here, given, for example, the following works like a charm:
query := `
{
resource(func: eq(xid, "` + uid.Value() + `")) {
u as uid
}
}
`
res := &Resource{
UID: "uid(u)",
}
pb, err := json.Marshal(res)
if err != nil {
return nil, fmt.Errorf("json marshal: %w", err)
}
mu := &dgapi.Mutation{
Cond: cond,
SetJson: pb,
}
req := &dgapi.Request{
Query: query,
Mutations: []*dgapi.Mutation{mu},
CommitNow: true,
}
if _, err := s.c.NewTxn().Do(ctx, req); err != nil {
return fmt.Errorf("txn Add: %w", err)
}
This puzzles me a bit, but I’m sure I’m missing something fundamental here.