A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.hypermode.com/dgraph/sdks/go below:

Go - Dgraph

 Requires at least Go 1.23
export GO111MODULE=on
go get -u -v github.com/dgraph-io/dgo/v240
func newClient() *dgo.Dgraph {
  // Dial a gRPC connection. The address to dial to can be configured when
  // setting up the dgraph cluster.
  d, err := grpc.Dial("localhost:9080", grpc.WithInsecure())
  if err != nil {
    log.Fatal(err)
  }

  return dgo.NewDgraphClient(
    api.NewDgraphClient(d),
  )
}
func newClient() *dgo.Dgraph {
  // Dial a gRPC connection. The address to dial to can be configured when
  // setting up the dgraph cluster.
  dialOpts := append([]grpc.DialOption{},
    grpc.WithInsecure(),
    grpc.WithDefaultCallOptions(grpc.UseCompressor(gzip.Name)))
  d, err := grpc.Dial("localhost:9080", dialOpts...)

  if err != nil {
    log.Fatal(err)
  }

  return dgo.NewDgraphClient(
    api.NewDgraphClient(d),
  )
}
conn, err := grpc.Dial("127.0.0.1:9080", grpc.WithInsecure())
if err != nil {
  glog.Error("While trying to dial gRPC, got error", err)
}
dc := dgo.NewDgraphClient(api.NewDgraphClient(conn))
ctx := context.Background()
// Login to namespace 123
if err := dc.LoginIntoNamespace(ctx, "groot", "password", 123); err != nil {
  glog.Error("Failed to login: ",err)
}
func setup(c *dgo.Dgraph) {
  // Install a schema into dgraph. Accounts have a `name` and a `balance`.
  err := c.Alter(context.Background(), &api.Operation{
    Schema: `
      name: string @index(term) .
      balance: int .
    `,
  })
}
  // Drop all data including schema from the dgraph instance. This is useful
  // for small examples such as this, since it puts dgraph into a clean
  // state.
  err := c.Alter(context.Background(), &api.Operation{DropOp: api.Operation_ALL})
  // Drop all data including schema from the dgraph instance. This is useful
  // for small examples such as this, since it puts dgraph into a clean
  // state.
  err := c.Alter(context.Background(), &api.Operation{DropOp: api.Operation_DATA})
func runTxn(c *dgo.Dgraph) {
  txn := c.NewTxn()
  defer txn.Discard(ctx)
  ...
}
  // Query the balance for Alice and Bob.
  const q = `
    {
      all(func: anyofterms(name, "Alice Bob")) {
        uid
        balance
      }
    }
  `
  resp, err := txn.Query(context.Background(), q)
  if err != nil {
    log.Fatal(err)
  }

  // After we get the balances, we have to decode them into structs so that
  // we can manipulate the data.
  var decode struct {
    All []struct {
      Uid     string
      Balance int
    }
  }
  if err := json.Unmarshal(resp.GetJson(), &decode); err != nil {
    log.Fatal(err)
  }
  // Query the balance for Alice and Bob.
  const q = `
    {
      all(func: anyofterms(name, "Alice Bob")) {
        name
        balance
      }
    }
  `
  resp, err := txn.QueryRDF(context.Background(), q)
  if err != nil {
    log.Fatal(err)
  }

  // <0x17> <name> "Alice" .
  // <0x17> <balance> 100 .
  fmt.Println(resp.Rdf)
  // Move $5 between the two accounts.
  decode.All[0].Bal += 5
  decode.All[1].Bal -= 5

  out, err := json.Marshal(decode.All)
  if err != nil {
    log.Fatal(err)
  }

  _, err := txn.Mutate(context.Background(), &api.Mutation{SetJson: out})
  // Finally, we can commit the transactions. An error will be returned if
  // other transactions running concurrently modify the same data that was
  // modified in this transaction. It is up to the library user to retry
  // transactions when they fail.

  err := txn.Commit(context.Background())
package dgo_test

import (
  "context"
  "encoding/json"
  "fmt"
  "log"
  "time"

  "github.com/dgraph-io/dgo/v200/protos/api"
)

type School struct {
  Name  string   `json:"name,omitempty"`
  DType []string `json:"dgraph.type,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"`
  Dob      *time.Time `json:"dob,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"`
  DType    []string   `json:"dgraph.type,omitempty"`
}

func Example_setObject() {
  dg, cancel := getDgraphClient()
  defer cancel()

  dob := time.Date(1980, 01, 01, 23, 0, 0, 0, time.UTC)
  // 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,
    DType:   []string{"Person"},
    Location: loc{
      Type:   "Point",
      Coords: []float64{1.1, 2},
    },
    Dob: &dob,
    Raw: []byte("raw_bytes"),
    Friends: []Person{{
      Name:  "Bob",
      Age:   24,
      DType: []string{"Person"},
    }, {
      Name:  "Charlie",
      Age:   29,
      DType: []string{"Person"},
    }},
    School: []School{{
      Name:  "Crown Public School",
      DType: []string{"Institution"},
    }},
  }

  op := &api.Operation{}
  op.Schema = `
    name: string @index(exact) .
    age: int .
    married: bool .
    loc: geo .
    dob: datetime .
    Friend: [uid] .
    type: string .
    coords: float .
    type Person {
      name: string
      age: int
      married: bool
      Friend: [Person]
      loc: Loc
    }
    type Institution {
      name: string
    }
    type Loc {
      type: string
      coords: float
    }
  `

  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
  response, err := dg.NewTxn().Mutate(ctx, mu)
  if err != nil {
    log.Fatal(err)
  }

  // Assigned uids for nodes which were created would be returned in the response.Uids map.
  variables := map[string]string{"$id1": response.Uids["alice"]}
  q := `query Me($id1: string){
    me(func: uid($id1)) {
      name
      dob
      age
      loc
      raw_bytes
      married
      dgraph.type
      friend @filter(eq(name, "Bob")){
        name
        age
        dgraph.type
      }
      school {
        name
        dgraph.type
      }
    }
  }`

  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)
  }

  out, _ := json.MarshalIndent(r, "", "\t")
  fmt.Printf("%s\n", out)
}
 Output: {
   "me": [
     {
       "name": "Alice",
       "age": 26,
       "dob": "1980-01-01T23:00:00Z",
       "married": true,
       "raw_bytes": "cmF3X2J5dGVz",
       "friend": [
         {
           "name": "Bob",
           "age": 24,
           "loc": {},
           "dgraph.type": [
             "Person"
           ]
         }
       ],
       "loc": {
         "type": "Point",
         "coordinates": [
           1.1,
           2
         ]
       },
       "school": [
         {
           "name": "Crown Public School",
           "dgraph.type": [
             "Institution"
           ]
         }
       ],
       "dgraph.type": [
         "Person"
       ]
     }
   ]
 }

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4