Created
August 28, 2019 21:02
-
-
Save bgv/1c924c239f378a6ef6f13e9336937b89 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" | |
) | |
type Friend struct { | |
Uid string `json:"uid,omitempty"` | |
Name string `json:"name,omitempty"` | |
Age int `json:"age,omitempty"` | |
Married bool `json:"married,omitempty"` | |
DgraphType string `json:"dgraph.type,omitempty"` | |
} | |
type Person struct { | |
Uid string `json:"uid,omitempty"` | |
Name string `json:"name,omitempty"` | |
Age int `json:"age,omitempty"` | |
Married bool `json:"married,omitempty"` | |
Friends []*Friend `json:"friend,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 := Person{ | |
Uid: "_:alice", | |
Name: "Alice", | |
Age: 26, | |
Married: true, | |
DgraphType: "Person", | |
Friends: []*Friend{&Friend{ | |
Uid: "_:bob", | |
Name: "Bob", | |
Age: 24, | |
DgraphType: "Friend", | |
}, &Friend{ | |
Uid: "_:charlie", | |
Name: "Charlie", | |
Age: 29, | |
DgraphType: "Friend", | |
}}, | |
} | |
op := &api.Operation{} | |
op.Schema = ` | |
age: int . | |
friend: [uid] @reverse . | |
married: bool . | |
type Person { | |
name: string | |
age: int | |
married: bool | |
friend: [Friend] | |
} | |
type Friend { | |
name: string | |
age: int | |
married: bool | |
} | |
` | |
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["bob"] | |
// Forward query. | |
var q = fmt.Sprintf(`{ | |
expander(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(`{ | |
expander(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