-
-
Save MichelDiz/c1d30240607e743a157985acfc6804fd to your computer and use it in GitHub Desktop.
dgraph-1.1.0-rc3-test.go
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" | |
) | |
// Article wwww | |
type Article struct { | |
Uid string `json:"uid,omitempty"` | |
Title string `json:"title,omitempty"` | |
Points int `json:"points,omitempty"` | |
DgraphType string `json:"dgraph.type,omitempty"` | |
} | |
// Author wwww | |
type Author struct { | |
Uid string `json:"uid,omitempty"` | |
Name string `json:"name,omitempty"` | |
Age int `json:"age,omitempty"` | |
Nationality string `json:"nationality,omitempty"` | |
Articles []Article `json:"articles,omitempty"` | |
DgraphType string `json:"dgraph.type,omitempty"` | |
} | |
type CancelFunc func() | |
func main() { | |
dg, cancel := getDgraphClient() | |
defer cancel() | |
// While setting an object if a struct has a Uid then its properties in the graph are updated | |
// else a new node is created. | |
// In the example below new nodes for Alice, Bob and Charlie and school are created (since they | |
// don't have a Uid). | |
p := Author{ | |
Uid: "_:alice", | |
Name: "Alice", | |
Age: 26, | |
DgraphType: "Author", | |
Articles: []Article{ | |
Article{ | |
Uid: "_:article1", | |
Title: "West", | |
Points: 3, | |
DgraphType: "Article", | |
}, | |
Article{ | |
Uid: "_:article2", | |
Title: "East", | |
Points: 7, | |
DgraphType: "Article", | |
}, | |
}, | |
} | |
op := &api.Operation{} | |
op.Schema = ` | |
age: int . | |
articles: [uid] @reverse . | |
name: string . | |
nationality: string . | |
title: string . | |
type Author { | |
name: string | |
age: int | |
nationality: string | |
articles: [Article] | |
} | |
type Article { | |
title: string | |
points: int | |
articles: [Author] | |
} | |
` | |
ctx := context.Background() | |
if err := dg.Alter(ctx, op); err != nil { | |
log.Fatal(err) | |
} | |
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 | |
alice := assigned.Uids["alice"] | |
bob := assigned.Uids["article1"] | |
// Forward query. | |
var q = fmt.Sprintf(`{ | |
expForward(func: uid(%s)) { | |
uid | |
expand(_all_) { | |
uid | |
expand(_all_) | |
} | |
} | |
}`, alice) | |
respf, err := dg.NewTxn().Query(ctx, q) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println() | |
fmt.Println(string(respf.Json)) | |
// Reverse query | |
var qr = fmt.Sprintf(`{ | |
expReverse(func: uid(%s)) { | |
uid | |
expand(_all_) { | |
uid | |
expand(_all_) | |
} | |
} | |
}`, bob) | |
respr, err := dg.NewTxn().Query(ctx, qr) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println() | |
fmt.Println(string(respr.Json)) | |
// // Query schema | |
// const sch = `schema{}` | |
// resp, err := dg.NewTxn().Query(ctx, sch) | |
// if err != nil { | |
// log.Fatal(err) | |
// } | |
// fmt.Println() | |
// fmt.Println(string(resp.Json)) | |
} | |
func getDgraphClient() (*dgo.Dgraph, CancelFunc) { | |
conn, err := grpc.Dial("127.0.0.1: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