gogrpcrpcdgraph-dgo

Connection error rpc with Golang and DGraph


i'm trying make a mutation inside a DGraph database, but when i run the code, it throws me the next error:

rpc error: code = Unavailable desc = connection close exit status 1

I'm using dGraph with docker in the port 8000, my code of golang here:

package main

import (
   "fmt"
   "context"
   "encoding/json"
   "log"
   dgo "github.com/dgraph-io/dgo"
   api "github.com/dgraph-io/dgo/protos/api"
   grpc "google.golang.org/grpc"
)

type Person struct {
   Name string `json:"name,omitempty"`
   Lastname string `json:"lastname,omitempty"`
}

func main() {
conn, err := grpc.Dial("localhost:8000", grpc.WithInsecure())
if err != nil {
  log.Fatal(err)
}
defer conn.Close()
dgraphClient := dgo.NewDgraphClient(api.NewDgraphClient(conn))
p := Person {
    Name: "Giovanni",
    Lastname: "Mosquera Diazgranados",
}
txn := dgraphClient.NewTxn()
ctx := context.Background()
defer txn.Discard(ctx)
pb, err := json.Marshal(p)
if err != nil {
    log.Fatal(err)
}
mu := &api.Mutation{
    SetJson: pb,
}
res, err := txn.Mutate(ctx, mu)
if err != nil {
    fmt.Println("Aqui toy")
    log.Fatal(err)
} else {
    fmt.Println(res)
}
}

How can i solve this error to connect with my DGraph and make a mutation?


Solution

  • Welcome to Stack Overflow!

    To get your code working locally with the docker "standalone" version of DGraph I had to change 2 things:

    import (
        dgo "github.com/dgraph-io/dgo/v2"
        api "github.com/dgraph-io/dgo/v2/protos/api"
    )