2

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 Answers2

1

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"
)
TehSphinX
  • 6,536
  • 1
  • 24
  • 34
  • Thanks for the Welcome, i apreciate it – Giovanni Mosquera Aug 31 '20 at 15:16
  • I tried changing to "github.com/dgraph-io/dgo/v2", but Go throws the next error: cannot find package "github.com/dgraph-io/dgo/v2" in any of: /usr/local/go/src/github.com/dgraph-io/dgo/v2 (from $GOROOT) /home/giovanni/go/src/github.com/dgraph-io/dgo/v2 (from $GOPATH)\nHow can i solve it? – Giovanni Mosquera Aug 31 '20 at 15:17
  • 2
    Did you `go get` the new import path "github.com/dgraph-io/dgo/v2"? – TehSphinX Aug 31 '20 at 15:34
  • Yeah, when i try "go get github.com/dgraph-io/dgo/v2", it throws the error: cannot find package "github.com/dgraph-io/dgo/v2" in any of: /usr/local/go/src/github.com/dgraph-io/dgo/v2 (from $GOROOT) /home/giovanni/go/src/github.com/dgraph-io/dgo/v2 (from $GOPATH) – Giovanni Mosquera Aug 31 '20 at 15:42
  • I only can import the "github.com/dgraph-io/dgo" without the "v2" – Giovanni Mosquera Aug 31 '20 at 15:43
  • 1
    `go get github.com/dgraph-io/dgo/v2` works perfectly for me. If you have go modules initialized you can also try a `go mod tidy` to get the dependencies. – TehSphinX Aug 31 '20 at 15:46
  • 1
    But in any case: this has now become a dependency problem, not a dgraph/dgo problem any more. – TehSphinX Aug 31 '20 at 15:47
  • I tried go mod tidy and the console say this: go: cannot find main module; see 'go help modules'... it means problems with my version of go or something? – Giovanni Mosquera Aug 31 '20 at 15:48
  • 1
    Is there a go.mod file in the same folder as your project root? If not, go to that folder and call `go mod init` and give the module a name. The name is best domain based to be unique like `github.com/tehsphinx/somemodule`. – TehSphinX Aug 31 '20 at 15:52
  • 1
    Now, i tried run main.go again and the program throws: txn: latency: uids:. this response means all right? – Giovanni Mosquera Aug 31 '20 at 16:06
  • 1
    Yes, that is from `fmt.Println(res)`. It is the result of the mutation containing the uid and other information. – TehSphinX Aug 31 '20 at 16:07
  • Ohh! Right!! Finally!. Now... can you help me with one more thing?... How can i view the mutation of data in the DGraph? I'm triying with the query: "data(func:has(name))" but this dont return anything... – Giovanni Mosquera Aug 31 '20 at 16:10
  • 1
    Are you providing fields you want to see? like `name` and `lastname`. At least ask for uid. Example: `{ data(func: has(name)) { uid name } }` – TehSphinX Aug 31 '20 at 16:28
  • 1
    Doing the tour might help you. It will use a local dgraph instance to run against and fill it with data in the first steps. Then you can play with the data right from the tour: https://dgraph.io/tour/intro/1/ – TehSphinX Aug 31 '20 at 16:31
  • 1
    I did not think that someone would answer a question of that language and also about its connection in DGraph, you are the god of Go, really, thank you very much!The program works perfectly, i'm really grateful with you, honestly – Giovanni Mosquera Aug 31 '20 at 16:47
  • Thx :D Glad I could help! – TehSphinX Aug 31 '20 at 16:51
0

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.

Josh Dando
  • 1,647
  • 14
  • 9