forked from dgraph-io/dgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples_test.go
974 lines (836 loc) · 19.2 KB
/
examples_test.go
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
/*
* Copyright (C) 2017 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dgo_test
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) {
conn, err := grpc.Dial("127.0.0.1:9180", grpc.WithInsecure())
if err != nil {
log.Fatal("While trying to dial gRPC")
}
dc := api.NewDgraphClient(conn)
return dgo.NewDgraphClient(dc), func() {
if err := conn.Close(); err != nil {
log.Printf("Error while closing connection:%v", err)
}
}
}
func ExampleDgraph_Alter_dropAll() {
dg, cancel := getDgraphClient()
defer cancel()
op := api.Operation{
DropAll: true,
}
ctx := context.Background()
if err := dg.Alter(ctx, &op); err != nil {
log.Fatal(err)
}
// Output:
}
func ExampleTxn_Query_variables() {
dg, cancel := getDgraphClient()
defer cancel()
type Person struct {
Uid string `json:"uid,omitempty"`
Name string `json:"name,omitempty"`
}
op := &api.Operation{}
op.Schema = `
name: string @index(exact) .
`
ctx := context.Background()
err := dg.Alter(ctx, op)
if err != nil {
log.Fatal(err)
}
p := Person{
Name: "Alice",
}
mu := &api.Mutation{
CommitNow: true,
}
pb, err := json.Marshal(p)
if err != nil {
log.Fatal(err)
}
mu.SetJson = pb
_, err = dg.NewTxn().Mutate(ctx, mu)
if err != nil {
log.Fatal(err)
}
variables := make(map[string]string)
variables["$a"] = "Alice"
q := `query Alice($a: string){
me(func: eq(name, $a)) {
name
}
}`
resp, err := dg.NewTxn().QueryWithVars(ctx, q, variables)
if err != nil {
log.Fatal(err)
}
type Root struct {
Me []Person `json:"me"`
}
var r Root
err = json.Unmarshal(resp.Json, &r)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(resp.Json))
// Output: {"me":[{"name":"Alice"}]}
}
func ExampleTxn_Mutate() {
type School struct {
Name string `json:"name,omitempty"`
}
type loc struct {
Type string `json:"type,omitempty"`
Coords []float64 `json:"coordinates,omitempty"`
}
// If omitempty is not set, then edges with empty values (0 for int/float, "" for string, false
// for bool) would be created for values not specified explicitly.
type Person struct {
Uid string `json:"uid,omitempty"`
Name string `json:"name,omitempty"`
Age int `json:"age,omitempty"`
Married bool `json:"married,omitempty"`
Raw []byte `json:"raw_bytes,omitempty"`
Friends []Person `json:"friend,omitempty"`
Location loc `json:"loc,omitempty"`
School []School `json:"school,omitempty"`
}
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
// dont have a Uid).
p := Person{
Name: "Alice",
Age: 26,
Married: true,
Location: loc{
Type: "Point",
Coords: []float64{1.1, 2},
},
Raw: []byte("raw_bytes"),
Friends: []Person{{
Name: "Bob",
Age: 24,
}, {
Name: "Charlie",
Age: 29,
}},
School: []School{{
Name: "Crown Public School",
}},
}
op := &api.Operation{}
op.Schema = `
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)
}
// 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)) {
name
age
loc
raw_bytes
married
friend @filter(eq(name, "Bob")) {
name
age
}
school {
name
}
}
}`
variables := make(map[string]string)
variables["$id"] = puid
resp, err := dg.NewTxn().QueryWithVars(ctx, q, variables)
if err != nil {
log.Fatal(err)
}
type Root struct {
Me []Person `json:"me"`
}
var r Root
err = json.Unmarshal(resp.Json, &r)
if err != nil {
log.Fatal(err)
}
// R.Me would be same as the person that we set above.
// fmt.Printf("Me: %+v\n", r.Me)
fmt.Println(string(resp.Json))
// Output: {"me":[{"name":"Alice","age":26,"loc":{"type":"Point","coordinates":[1.1,2]},"raw_bytes":"cmF3X2J5dGVz","married":true,"friend":[{"name":"Bob","age":24}],"school":[{"name":"Crown Public School"}]}]}
}
func ExampleTxn_Mutate_bytes() {
dg, cancel := getDgraphClient()
defer cancel()
type Person struct {
Uid string `json:"uid,omitempty"`
Name string `json:"name,omitempty"`
Bytes []byte `json:"bytes,omitempty"`
}
op := &api.Operation{}
op.Schema = `
name: string @index(exact) .
`
ctx := context.Background()
err := dg.Alter(ctx, op)
if err != nil {
log.Fatal(err)
}
p := Person{
Name: "Alice-new",
Bytes: []byte("raw_bytes"),
}
mu := &api.Mutation{
CommitNow: true,
}
pb, err := json.Marshal(p)
if err != nil {
log.Fatal(err)
}
mu.SetJson = pb
_, err = dg.NewTxn().Mutate(ctx, mu)
if err != nil {
log.Fatal(err)
}
q := `{
q(func: eq(name, "Alice-new")) {
name
bytes
}
}`
resp, err := dg.NewTxn().Query(ctx, q)
if err != nil {
log.Fatal(err)
}
type Root struct {
Me []Person `json:"q"`
}
var r Root
err = json.Unmarshal(resp.Json, &r)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Me: %+v\n", r.Me)
// Output: Me: [{Uid: Name:Alice-new Bytes:[114 97 119 95 98 121 116 101 115]}]
}
func ExampleTxn_Query_unmarshal() {
type School struct {
Name string `json:"name,omitempty"`
}
type Person struct {
Uid string `json:"uid,omitempty"`
Name string `json:"name,omitempty"`
Age int `json:"age,omitempty"`
Married bool `json:"married,omitempty"`
Raw []byte `json:"raw_bytes,omitempty"`
Friends []Person `json:"friend,omitempty"`
School []School `json:"school,omitempty"`
}
dg, cancel := getDgraphClient()
defer cancel()
op := &api.Operation{}
op.Schema = `
age: int .
married: bool .
`
ctx := context.Background()
err := dg.Alter(ctx, op)
if err != nil {
log.Fatal(err)
}
p := Person{
Name: "Bob",
Age: 24,
}
txn := dg.NewTxn()
pb, err := json.Marshal(p)
if err != nil {
log.Fatal(err)
}
mu := &api.Mutation{
CommitNow: true,
SetJson: pb,
}
assigned, err := txn.Mutate(ctx, mu)
if err != nil {
log.Fatal(err)
}
bob := assigned.Uids["blank-0"]
// 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 and Charlie and school are created (since they dont
// have a Uid). Alice is also connected via the friend edge to an existing node Bob.
p = Person{
Name: "Alice",
Age: 26,
Married: true,
Raw: []byte("raw_bytes"),
Friends: []Person{{
Uid: bob,
}, {
Name: "Charlie",
Age: 29,
}},
School: []School{{
Name: "Crown Public School",
}},
}
txn = dg.NewTxn()
mu = &api.Mutation{}
pb, err = json.Marshal(p)
if err != nil {
log.Fatal(err)
}
mu.SetJson = pb
mu.CommitNow = true
assigned, err = txn.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"]
variables := make(map[string]string)
variables["$id"] = puid
const q = `query Me($id: string){
me(func: uid($id)) {
name
age
loc
raw_bytes
married
friend @filter(eq(name, "Bob")) {
name
age
}
school {
name
}
}
}`
resp, err := dg.NewTxn().QueryWithVars(ctx, q, variables)
if err != nil {
log.Fatal(err)
}
type Root struct {
Me []Person `json:"me"`
}
var r Root
err = json.Unmarshal(resp.Json, &r)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(resp.Json))
// Output: {"me":[{"name":"Alice","age":26,"raw_bytes":"cmF3X2J5dGVz","married":true,"friend":[{"name":"Bob","age":24}],"school":[{"name":"Crown Public School"}]}]}
}
func ExampleTxn_Mutate_facets() {
dg, cancel := getDgraphClient()
defer cancel()
// Doing a dropAll isn't required by the user. We do it here so that we can verify that the
// example runs as expected.
op := api.Operation{
DropAll: true,
}
ctx := context.Background()
if err := dg.Alter(ctx, &op); err != nil {
log.Fatal(err)
}
op = api.Operation{}
op.Schema = `
name: string @index(exact) .
`
err := dg.Alter(ctx, &op)
if err != nil {
log.Fatal(err)
}
// This example shows example for SetObject using facets.
type School struct {
Name string `json:"name,omitempty"`
Since time.Time `json:"school|since,omitempty"`
}
type Person struct {
Name string `json:"name,omitempty"`
NameOrigin string `json:"name|origin,omitempty"`
Friends []Person `json:"friend,omitempty"`
// These are facets on the friend edge.
Since time.Time `json:"friend|since,omitempty"`
Family string `json:"friend|family,omitempty"`
Age float64 `json:"friend|age,omitempty"`
Close bool `json:"friend|close,omitempty"`
School []School `json:"school,omitempty"`
}
ti := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
p := Person{
Name: "Alice",
NameOrigin: "Indonesia",
Friends: []Person{
Person{
Name: "Bob",
Since: ti,
Family: "yes",
Age: 13,
Close: true,
},
Person{
Name: "Charlie",
Family: "maybe",
Age: 16,
},
},
School: []School{School{
Name: "Wellington School",
Since: ti,
}},
}
mu := &api.Mutation{}
pb, err := json.Marshal(p)
if err != nil {
log.Fatal(err)
}
mu.SetJson = pb
mu.CommitNow = true
assigned, err := dg.NewTxn().Mutate(ctx, mu)
if err != nil {
log.Fatal(err)
}
auid := assigned.Uids["blank-0"]
variables := make(map[string]string)
variables["$id"] = auid
const q = `query Me($id: string){
me(func: uid($id)) {
name @facets
friend @filter(eq(name, "Bob")) @facets {
name
}
school @facets {
name
}
}
}`
resp, err := dg.NewTxn().QueryWithVars(ctx, q, variables)
if err != nil {
log.Fatal(err)
}
type Root struct {
Me []Person `json:"me"`
}
var r Root
err = json.Unmarshal(resp.Json, &r)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Me: %+v\n", r.Me)
// Output: Me: [{Name:Alice NameOrigin:Indonesia Friends:[{Name:Bob NameOrigin: Friends:[] Since:2009-11-10 23:00:00 +0000 UTC Family:yes Age:13 Close:true School:[]}] Since:0001-01-01 00:00:00 +0000 UTC Family: Age:0 Close:false School:[{Name:Wellington School Since:2009-11-10 23:00:00 +0000 UTC}]}]
}
func ExampleTxn_Mutate_list() {
dg, cancel := getDgraphClient()
defer cancel() // This example shows example for SetObject for predicates with list type.
type Person struct {
Uid string `json:"uid"`
Address []string `json:"address"`
PhoneNumber []int64 `json:"phone_number"`
}
p := Person{
Address: []string{"Redfern", "Riley Street"},
PhoneNumber: []int64{9876, 123},
}
op := &api.Operation{}
op.Schema = `
address: [string] .
phone_number: [int] .
`
ctx := context.Background()
err := dg.Alter(ctx, op)
if err != nil {
log.Fatal(err)
}
mu := &api.Mutation{}
pb, err := json.Marshal(p)
if err != nil {
log.Fatal(err)
}
mu.SetJson = pb
mu.CommitNow = true
assigned, err := dg.NewTxn().Mutate(ctx, mu)
if err != nil {
log.Fatal(err)
}
variables := map[string]string{"$id": assigned.Uids["blank-0"]}
const q = `
query Me($id: string){
me(func: uid($id)) {
address
phone_number
}
}
`
resp, err := dg.NewTxn().QueryWithVars(ctx, q, variables)
if err != nil {
log.Fatal(err)
}
type Root struct {
Me []Person `json:"me"`
}
var r Root
err = json.Unmarshal(resp.Json, &r)
if err != nil {
log.Fatal(err)
}
// List items aren't guaranteed to be in the same order.
fmt.Println(string(resp.Json))
// {"me":[{"address":["Riley Street","Redfern"],"phone_number":[9876,123]}]}
}
func ExampleDeleteEdges() {
dg, cancel := getDgraphClient()
defer cancel()
op := &api.Operation{}
op.Schema = `
age: int .
married: bool .
name: string @lang .
location: string .
`
ctx := context.Background()
err := dg.Alter(ctx, op)
if err != nil {
log.Fatal(err)
}
type School struct {
Uid string `json:"uid"`
Name string `json:"name@en,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 []Person `json:"friends,omitempty"`
Location string `json:"location,omitempty"`
Schools []*School `json:"schools,omitempty"`
}
// Lets add some data first.
p := Person{
Name: "Alice",
Age: 26,
Married: true,
Location: "Riley Street",
Friends: []Person{{
Name: "Bob",
Age: 24,
}, {
Name: "Charlie",
Age: 29,
}},
Schools: []*School{&School{
Name: "Crown Public School",
}},
}
mu := &api.Mutation{}
pb, err := json.Marshal(p)
if err != nil {
log.Fatal(err)
}
mu.SetJson = pb
mu.CommitNow = true
mu.IgnoreIndexConflict = true
assigned, err := dg.NewTxn().Mutate(ctx, mu)
if err != nil {
log.Fatal(err)
}
alice := assigned.Uids["blank-0"]
variables := make(map[string]string)
variables["$alice"] = alice
const q = `query Me($alice: string){
me(func: uid($alice)) {
name
age
loc
married
friends {
name
age
}
schools {
name@en
}
}
}`
resp, err := dg.NewTxn().QueryWithVars(ctx, q, variables)
if err != nil {
log.Fatal(err)
}
// Now lets delete the friend and location edge from Alice
mu = &api.Mutation{}
dgo.DeleteEdges(mu, alice, "friends", "loc")
mu.CommitNow = true
_, err = dg.NewTxn().Mutate(ctx, mu)
if err != nil {
log.Fatal(err)
}
resp, err = dg.NewTxn().QueryWithVars(ctx, q, variables)
if err != nil {
log.Fatal(err)
}
type Root struct {
Me []Person `json:"me"`
}
var r Root
err = json.Unmarshal(resp.Json, &r)
fmt.Println(string(resp.Json))
// Output: {"me":[{"name":"Alice","age":26,"married":true,"schools":[{"name@en":"Crown Public School"}]}]}
}
func ExampleTxn_Mutate_deleteNode() {
dg, cancel := getDgraphClient()
defer cancel()
// In this test we check S * * deletion.
type Person struct {
Uid string `json:"uid,omitempty"`
Name string `json:"name,omitempty"`
Age int `json:"age,omitempty"`
Married bool `json:"married,omitempty"`
Friends []*Person `json:"friend,omitempty"`
}
p := Person{
Name: "Alice",
Age: 26,
Married: true,
Friends: []*Person{&Person{
Name: "Bob",
Age: 24,
}, &Person{
Name: "Charlie",
Age: 29,
}},
}
op := &api.Operation{}
op.Schema = `
age: int .
married: bool .
`
ctx := context.Background()
err := dg.Alter(ctx, op)
if 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)
}
alice := assigned.Uids["blank-0"]
bob := assigned.Uids["blank-1"]
charlie := assigned.Uids["blank-2"]
variables := make(map[string]string)
variables["$alice"] = alice
variables["$bob"] = bob
variables["$charlie"] = charlie
const q = `query Me($alice: string, $bob: string, $charlie: string){
me(func: uid($alice)) {
name
age
married
friend {
uid
name
age
}
}
me2(func: uid($bob)) {
name
age
}
me3(func: uid($charlie)) {
name
age
}
}`
resp, err := dg.NewTxn().QueryWithVars(ctx, q, variables)
if err != nil {
log.Fatal(err)
}
type Root struct {
Me []Person `json:"me"`
Me2 []Person `json:"me2"`
Me3 []Person `json:"me3"`
}
var r Root
err = json.Unmarshal(resp.Json, &r)
// Now lets try to delete Alice. This won't delete Bob and Charlie but just remove the
// connection between Alice and them.
// The JSON for deleting a node should be of the form {"uid": "0x123"}. If you wanted to
// delete multiple nodes you could supply an array of objects like [{"uid": "0x321"}, {"uid":
// "0x123"}] to DeleteJson.
d := map[string]string{"uid": alice}
pb, err = json.Marshal(d)
if err != nil {
log.Fatal(err)
}
mu = &api.Mutation{
CommitNow: true,
DeleteJson: pb,
}
_, err = dg.NewTxn().Mutate(ctx, mu)
if err != nil {
log.Fatal(err)
}
resp, err = dg.NewTxn().QueryWithVars(ctx, q, variables)
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(resp.Json, &r)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Resp after deleting node: %+v\n", string(resp.Json))
// Output: Resp after deleting node: {"me":[],"me2":[{"name":"Bob","age":24}],"me3":[{"name":"Charlie","age":29}]}
}
func ExampleTxn_Mutate_deletePredicate() {
dg, cancel := getDgraphClient()
defer cancel()
type Person struct {
Uid string `json:"uid,omitempty"`
Name string `json:"name,omitempty"`
Age int `json:"age,omitempty"`
Married bool `json:"married,omitempty"`
Friends []Person `json:"friend,omitempty"`
}
p := Person{
Name: "Alice",
Age: 26,
Married: true,
Friends: []Person{Person{
Name: "Bob",
Age: 24,
}, Person{
Name: "Charlie",
Age: 29,
}},
}
op := &api.Operation{}
op.Schema = `
age: int .
married: bool .
`
ctx := context.Background()
err := dg.Alter(ctx, op)
if 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)
}
alice := assigned.Uids["blank-0"]
variables := make(map[string]string)
variables["$id"] = alice
const q = `query Me($id: string){
me(func: uid($id)) {
name
age
married
friend {
uid
name
age
}
}
}`
resp, err := dg.NewTxn().QueryWithVars(ctx, q, variables)
if err != nil {
log.Fatal(err)
}
type Root struct {
Me []Person `json:"me"`
}
var r Root
err = json.Unmarshal(resp.Json, &r)
if err != nil {
log.Fatal(err)
}
op = &api.Operation{
DropAttr: "friend",
}
err = dg.Alter(ctx, op)
if err != nil {
log.Fatal(err)
}
op.DropAttr = "married"
err = dg.Alter(ctx, op)
if err != nil {
log.Fatal(err)
}
// Also lets run the query again to verify that predicate data was deleted.
resp, err = dg.NewTxn().QueryWithVars(ctx, q, variables)
if err != nil {
log.Fatal(err)
}
r = Root{}
err = json.Unmarshal(resp.Json, &r)
if err != nil {
log.Fatal(err)
}
// Alice should have no friends and only two attributes now.
fmt.Printf("Response after deletion: %+v\n", r)
// Output: Response after deletion: {Me:[{Uid: Name:Alice Age:26 Married:false Friends:[]}]}
}