This test reproduces the error I mentioned and the behaviour @Joschka described in his post:
package main
import (
"context"
"encoding/json"
"fmt"
"testing"
"github.com/dgraph-io/dgo/v2"
"github.com/dgraph-io/dgo/v2/protos/api"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
)
func Test_it_replaces_uid_predicates(t *testing.T) {
conn, err := grpc.Dial("localhost:9080", grpc.WithInsecure())
if err != nil {
t.Fatalf("conntect: %v", err)
}
dg := dgo.NewDgraphClient(api.NewDgraphClient(conn))
defer dg.Alter(context.Background(), &api.Operation{DropAll: true})
err = dg.Alter(context.Background(), &api.Operation{
Schema: `
type Node {
name: string
child: uid
}
name: string .
child: uid .
`,
})
require.NoError(t, err)
type Node struct {
UID string `json:"uid"`
Type string `json:"dgraph.type"`
Name string `json:"name"`
Child *Node `json:"child"`
}
in := Node{
UID: "_:parent",
Type: "Node",
Name: "parent",
Child: &Node{
UID: "_:child",
Type: "Node",
Name: "child",
},
}
js, err := json.Marshal(in)
require.NoError(t, err)
res, err := dg.NewTxn().Mutate(context.Background(), &api.Mutation{CommitNow: true, SetJson: js})
require.NoError(t, err)
parentUID := res.GetUids()["parent"]
update := Node{
UID: res.GetUids()["parent"],
Type: "Node",
Child: &Node{
UID: "_:child",
Type: "Node",
Name: "child replacement",
},
}
js, err = json.Marshal(update)
require.NoError(t, err)
res, err = dg.NewTxn().Mutate(context.Background(), &api.Mutation{CommitNow: true, SetJson: js})
assert.NoError(t, err)
q := `
query {
n(func: uid(%s)) {
uid
name
child {
uid
name
}
}
}
`
queryRes, err := dg.NewReadOnlyTxn().Query(context.Background(), fmt.Sprintf(q, parentUID))
require.NoError(t, err)
var actual map[string][]Node
err = json.Unmarshal(queryRes.GetJson(), &actual)
require.NoError(t, err)
require.Len(t, actual["n"], 1)
assert.Equal(t, "parent", actual["n"][0].Name)
assert.Equal(t, "child replacement", actual["n"][0].Child.Name)
}