I am trying to connect DGraph using Go client on a Windows machine.
I have started by DGraph Client by using the following docker command.
docker run --rm -it -p 8080:8080 -p 9080:9080 -p 8000:8000 -v ~/dgraph:/dgraph dgraph/standalone:v20.03.0
I can able to access dgraph UI at HTTP://localhost:8000. I was able to Alter Schema, Set data, and Query it back using dgraph UI.
I have the following implementation in Go to connect DGraph.
import (
"context"
"fmt"
"log"
"github.com/dgraph-io/dgo/v2"
"github.com/dgraph-io/dgo/v2/protos/api"
"google.golang.org/grpc"
)
// UserGroup type
type UserGroup struct {
UID string `json:"uid,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Level int `json:"level,omitempty"`
Enabled bool `json:"enabled,omitempty"`
}
// User type
type User struct {
UID string `json:"uid,omitempty"`
UserID string `json:"userId,omitempty"`
Email string `json:"email,omitempty"`
Enabled bool `json:"married,omitempty"`
UserRoles []UserGroup `json:"user.userGroups,omitempty"`
DType []string `json:"dgraph.type,omitempty"`
}
// CancelFunc type
type CancelFunc func()
// Client of dGraph
func Client() (*dgo.Dgraph, CancelFunc) {
conn, err := grpc.Dial("localhost:9080", grpc.WithInsecure())
if err != nil {
log.Fatal("While trying to dial gRPC")
}
dc := api.NewDgraphClient(conn)
dg := dgo.NewDgraphClient(dc)
return dg, func() {
if err := conn.Close(); err != nil {
log.Printf("Error while closing connection:%v", err)
}
}
}
// GetData func
func GetData() {
dg, cancel := Client()
defer cancel()
ctx := context.Background()
q := `{
q(func: eq(dgraph.type, "User")) {
dgraph.type
uid
userId
email
}
}`
resp, err := dg.NewTxn().Query(ctx, q)
if err != nil {
fmt.Println("Query error")
log.Fatal(err)
}
fmt.Println(resp)
}
But I am getting the following error.
error: code = Unavailable desc = all SubConns are in TransientFailure, latest connection error: connection closed
Not sure what exactly I am missing here. Any help will be appreciated.