Created
October 7, 2019 04:25
-
-
Save MichelDiz/7014c4af96016d0569019fad235bbde3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"encoding/json" | |
"fmt" | |
"log" | |
"github.com/dgraph-io/dgo/v2" | |
"github.com/dgraph-io/dgo/v2/protos/api" | |
"google.golang.org/grpc" | |
) | |
type Node struct { | |
UID string `json:"uid"` | |
Type string `json:"dgraph.type"` | |
Name string `json:"name"` | |
Child *Node `json:"child"` | |
} | |
type CancelFunc func() | |
func main() { | |
dg, cancel := getDgraphClient() | |
defer cancel() | |
op := &api.Operation{} | |
op.Schema = ` | |
type Node { | |
name: string | |
child: uid | |
} | |
name: string . | |
child: uid . | |
` | |
ctx := context.Background() | |
if err := dg.Alter(ctx, op); err != nil { | |
log.Fatal(err) | |
} | |
p := Node{ | |
UID: "_:parent", | |
Type: "Node", | |
Name: "parent", | |
Child: &Node{ | |
UID: "_:child", | |
Type: "Node", | |
Name: "child", | |
}, | |
} | |
mu := &api.Mutation{ | |
CommitNow: true, | |
} | |
pb, err := json.Marshal(p) | |
if err != nil { | |
log.Fatal(err) | |
} | |
mu.SetJson = pb | |
assigned, err := dg.NewTxn().Mutate(ctx, mu) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Get Uids | |
parentUID := assigned.Uids["parent"] | |
var q = fmt.Sprintf(`query { | |
data(func: uid(%s)) { | |
uid #parentUID | |
name | |
child { | |
uid | |
name | |
} | |
} | |
}`, parentUID) | |
respf, err := dg.NewTxn().Query(ctx, q) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println() | |
fmt.Println(string(respf.Json)) | |
mu2 := &api.Mutation{ | |
CommitNow: true, | |
} | |
q1 := ` | |
{ | |
me(func: eq(email, "[email protected]")) { | |
v as uid | |
} | |
}` | |
update := Node{ | |
UID: parentUID, | |
Type: "Node", | |
Child: &Node{ | |
UID: "_:child", | |
Type: "Node", | |
Name: "child replacement", | |
}, | |
} | |
pb2, err := json.Marshal(update) | |
if err != nil { | |
log.Fatal(err) | |
} | |
mu2.SetJson = pb2 | |
assigned2, err := dg.NewTxn().Mutate(ctx, mu2) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Get Uids | |
UID := assigned2.Uids["parent"] | |
var q2 = fmt.Sprintf(`query { | |
data(func: uid(%s)) { | |
uid | |
name | |
child { | |
uid | |
name | |
} | |
} | |
}`, UID) | |
respf2, err := dg.NewTxn().Query(ctx, q2) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println() | |
fmt.Println(string(respf2.Json)) | |
} | |
func getDgraphClient() (*dgo.Dgraph, CancelFunc) { | |
conn, err := grpc.Dial("localhost:9080", grpc.WithInsecure()) | |
if err != nil { | |
log.Fatal("While trying to dial gRPC") | |
} | |
dc := api.NewDgraphClient(conn) | |
dg := dgo.NewDgraphClient(dc) | |
return dg, func() { | |
if err := conn.Close(); err != nil { | |
log.Printf("Error while closing connection:%v", err) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment