Connection error rpc with Golang and DGraph

Viewed 278

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?

2 Answers

Welcome to Stack Overflow!

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

  • use port 9080. The container exposes 3 ports: 8000, 8080, 9080. Using 8080 or 8000 I get the same error you mentioned.
  • use the v2 imports. Not sure which version of DGraph server you are running, so you might not need to do this. But in case you have a new server you need these imports:
import (
    dgo "github.com/dgraph-io/dgo/v2"
    api "github.com/dgraph-io/dgo/v2/protos/api"
)

Port 8000 is for the ratel-ui which comes with dgraph. To make mutations using the dgraph go client you will want to connect to the exposed grpc-alpha port, this is typically on 9080.

Related