-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
201 lines (178 loc) · 3.93 KB
/
main.go
File metadata and controls
201 lines (178 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"time"
"github.com/dgraph-io/dgo"
"github.com/dgraph-io/dgo/protos/api"
"google.golang.org/grpc"
)
type CancelFunc func()
func getDgraphClient() (*dgo.Dgraph, CancelFunc) {
ip := ""
conn, err := grpc.Dial(ip, grpc.WithInsecure())
if err != nil {
log.Fatal("While trying to dial gRPC")
}
dc := api.NewDgraphClient(conn)
dg := dgo.NewDgraphClient(dc)
//ctx := context.Background()
return dg, func() {
if err := conn.Close(); err != nil {
log.Printf("Error while closing connection:%v", err)
}
}
}
type Starring struct {
Actor []Actor `json:"performance.actor,omitempty"`
Character []Character `json:"performance.character,omitempty"`
Film []Movie `json:"performance.film,omitempty"`
}
type Genre struct {
Uid string `json:"uid,omitempty"`
Name string `json:"name@en,omitempty"`
}
type Film struct {
Uid string `json:"uid,omitempty"`
Name string `json:"name@en,omitempty"`
}
type Actor struct {
Uid string `json:"uid,omitempty"`
Name string `json:"name@en,omitempty"`
Film []Movie `json:"performance.film,omitempty"`
}
type Director struct {
Uid string `json:"uid,omitempty"`
Name string `json:"name@en,omitempty"`
Film []Movie `json:"performance.film,omitempty"`
}
type Character struct {
Uid string `json:"uid,omitempty"`
Name string `json:"name@en,omitempty"`
}
type Movie struct {
Uid string `json:"uid,omitempty"`
Name string `json:"name@en,omitempty"`
NameDe string `json:"name@de,omitempty"`
NameTr string `json:"name@tr,omitempty"`
InitialReleaseDate time.Time `json:"initial_release_date,omitempty"`
Genre []Genre `json:"genre,omitempty"`
Starring []Starring `json:"starring,omitempty"`
Director []Director `json:"director.film,omitempty"`
Actor []Actor `json:"actor.film,omitempty"`
}
func main() {
t, _ := time.Parse(time.RFC3339, "1998-11-27T00:00:00Z")
dg, cancel := getDgraphClient()
defer cancel()
p := Movie{
Name: "Everything's Gonna Be Great",
NameDe: "Alles wird gut",
NameTr: "Herşey Çok Güzel Olacak",
Genre: []Genre{{
Name: "Comedy",
}, {
Name: "Drama",
}},
Starring: []Starring{
{
Character: []Character{{
Name: "Altan Camli",
}},
Actor: []Actor{{
Name: "Cem Yılmaz",
}},
},
{
Character: []Character{{
Name: "Nuri Camli",
}},
Actor: []Actor{{
Name: "Mazhar Alanson",
}},
},
{
Character: []Character{{
Name: "Ayla Camli",
}},
Actor: []Actor{{
Name: "Ceyda Düvenci",
}},
},
{
Character: []Character{{
Name: "Cevat Camli",
}},
Actor: []Actor{{
Name: "Selim Nasit",
}},
},
{
Character: []Character{{
Name: "Nusret",
}},
Actor: []Actor{{
Name: "Mustafa Uzunyilmaz",
}},
},
},
Director: []Director{
{
Name: "Ömer Vargi",
},
},
InitialReleaseDate: t,
}
ctx := context.Background()
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)
}
// Assigned uids for nodes which were created would be returned in the assigned.Uids map.
puid := assigned.Uids["blank-0"]
const q = `query Me($id: string){
me(func: uid($id)) {
uid
name@en
name@tr
initial_release_date
director{
name@en
}
starring{
performance.actor{
name@en
}
performance.character{
name@en
}
}
}
}`
variables := make(map[string]string)
variables["$id"] = puid
fmt.Println(puid)
resp, err := dg.NewTxn().QueryWithVars(ctx, q, variables)
if err != nil {
log.Fatal(err)
}
type Root struct {
Me []Movie `json:"me"`
}
var r Root
err = json.Unmarshal(resp.Json, &r)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(resp.Json))
}