github.com/unigraph-dev/dgraph@v1.1.1-0.20200923154953-8b52b426f765/contrib/integration/mutates/main.go (about)

     1  /*
     2   * Copyright 2017-2018 Dgraph Labs, Inc. and Contributors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package main
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"flag"
    23  	"fmt"
    24  	"log"
    25  
    26  	"github.com/dgraph-io/dgo"
    27  	"github.com/dgraph-io/dgo/protos/api"
    28  	"github.com/dgraph-io/dgo/x"
    29  	"google.golang.org/grpc"
    30  )
    31  
    32  var alpha = flag.String("alpha", "localhost:9080", "Dgraph alpha addr")
    33  var insert = flag.Bool("add", false, "Insert")
    34  
    35  func main() {
    36  	flag.Parse()
    37  
    38  	// Setup dgraph client
    39  	ctx := context.Background()
    40  	conn, err := grpc.Dial(*alpha, grpc.WithInsecure())
    41  	if err != nil {
    42  		log.Fatal(err)
    43  	}
    44  	pc := api.NewDgraphClient(conn)
    45  	c := dgo.NewDgraphClient(pc)
    46  
    47  	// Ingest
    48  	if *insert {
    49  		testInsert3Quads(ctx, c)
    50  	} else {
    51  		testQuery3Quads(ctx, c)
    52  	}
    53  }
    54  
    55  func testInsert3Quads(ctx context.Context, c *dgo.Dgraph) {
    56  	// Set schema
    57  	op := &api.Operation{}
    58  	op.Schema = `name: string @index(fulltext) .`
    59  	x.Check(c.Alter(ctx, op))
    60  
    61  	txn := c.NewTxn()
    62  
    63  	mu := &api.Mutation{}
    64  	quad := &api.NQuad{
    65  		Subject:     "200",
    66  		Predicate:   "name",
    67  		ObjectValue: &api.Value{Val: &api.Value_StrVal{StrVal: "ok 200"}},
    68  	}
    69  	mu.Set = []*api.NQuad{quad}
    70  	_, err := txn.Mutate(ctx, mu)
    71  	if err != nil {
    72  		log.Fatalf("Error while running mutation: %v\n", err)
    73  	}
    74  
    75  	mu = &api.Mutation{}
    76  	quad = &api.NQuad{
    77  		Subject:     "300",
    78  		Predicate:   "name",
    79  		ObjectValue: &api.Value{Val: &api.Value_StrVal{StrVal: "ok 300"}},
    80  	}
    81  	mu.Set = []*api.NQuad{quad}
    82  	// mu.SetNquads = []byte(`<300> <name> "ok 300" .`)
    83  	_, err = txn.Mutate(ctx, mu)
    84  	if err != nil {
    85  		log.Fatalf("Error while running mutation: %v\n", err)
    86  	}
    87  
    88  	mu = &api.Mutation{}
    89  	quad = &api.NQuad{
    90  		Subject:     "400",
    91  		Predicate:   "name",
    92  		ObjectValue: &api.Value{Val: &api.Value_StrVal{StrVal: "ok 400"}},
    93  	}
    94  	mu.Set = []*api.NQuad{quad}
    95  	// mu.SetNquads = []byte(`<400> <name> "ok 400" .`)
    96  	_, err = txn.Mutate(ctx, mu)
    97  	if err != nil {
    98  		log.Fatalf("Error while running mutation: %v\n", err)
    99  	}
   100  
   101  	x.Check(txn.Commit(ctx))
   102  	fmt.Println("Commit OK")
   103  }
   104  
   105  func testQuery3Quads(ctx context.Context, c *dgo.Dgraph) {
   106  	txn := c.NewTxn()
   107  	q := fmt.Sprint(`{ me(func: uid(200, 300, 400)) { name }}`)
   108  	resp, err := txn.Query(ctx, q)
   109  	if err != nil {
   110  		log.Fatalf("Error while running query: %v\n", err)
   111  	}
   112  	fmt.Printf("Response JSON: %q\n", resp.Json)
   113  	x.AssertTrue(bytes.Equal(resp.Json, []byte(
   114  		"{\"me\":[{\"name\":\"ok 200\"},{\"name\":\"ok 300\"},{\"name\":\"ok 400\"}]}")))
   115  	x.AssertTrue(resp.Txn.StartTs > 0)
   116  	x.Check(txn.Commit(ctx))
   117  }