Working grpc example with slash graphql

Can someone provide a working example of GRPC with Slash graphql using Go.

I understand there are some example but they all seem to use WithInsecure field and documentation was helpful with which method to use.

Thanks

Hi Harshad,

Thanks for the question.

We’ve recently moved the documentation for this over here: https://dgraph.io/docs/slash-graphql/advanced-queries/

This contains an example of connecting to Slash GraphQL over secure GRPC with python.

If you are looking for a golang example, then you can look at how we connect to Slash GraphQL from dgraph in order to run the bank test: dgraph/main.go at master · dgraph-io/dgraph · GitHub

Posting the relevant bits for posterity:

type authorizationCredentials struct {
	token string
}

func (a *authorizationCredentials) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
	return map[string]string{"Authorization": a.token}, nil
}

func (a *authorizationCredentials) RequireTransportSecurity() bool {
	return true
}

func grpcConnection(grpcEndpoint string, slashToken string) (*grpc.ClientConn, error) {
	pool, err := x509.SystemCertPool()
	if err != nil {
		return nil, err
	}
	return grpc.Dial(
		grpcEndpoint,
		grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{
			RootCAs:    pool,
			ServerName: strings.Split(grpcEndpoint, ":")[0],
		})),
		grpc.WithPerRPCCredentials(&authorizationCredentials{*slashToken}),
	)
}

Tejas

2 Likes

Hi @gja,

I was able to get something working . Thanks. However, I am getting the following error.

dc := api.NewDgraphClient(conn)
	dg := dgo.NewDgraphClient(dc)

	op := &api.Operation{}
	op.Schema = `
		name: string @index(exact) .
		age: int .
		married: bool .
		loc: geo .
		dob: datetime .
	`

	ctx := context.Background()
	err = dg.Alter(ctx, op)
	if err != nil {
		log.Fatal(err)
	}

	resp, err := dg.NewTxn().Query(ctx, `schema(pred: [name, age]) {type}`)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(string(resp.Json))

This is code i am trying to execute. The error message as below.

rpc error: code = Unknown desc = Alter is not allowed. Please switch your Slash GraphQL backend to flexible mode.

Can you DM me and @slotlocker2 your endpoint? We’ll make the appropriate changes to get this to work. The UI for getting this to work will go live in a few days

Not sure how to DM on this thing. I have send a question via slash.

Hey @harshadbhatia,

I have enabled the changes on your backend. Could you validate if it looks alright at your end?

1 Like

Yes thanks it is now working! Thanks :smiley:

Would anyone be able to provide the same for Node? (let me know if I should post this separately)

I’ve got this mostly working:

export const SLASH_ADDR = '<NAME>.grpc.us-west-2.aws.cloud.dgraph.io:443';
export const SLASH_SSL = grpc.credentials.createSsl();
export const SLASH_TOKEN = grpc.credentials.createFromMetadataGenerator(
  (_url, cb) => {
    const meta = new grpc.Metadata();
    meta.add('authorization', '<API-KEY>');
    cb(null, meta);
  }
);
export const SLASH_CREDENTIALS = grpc.credentials.combineChannelCredentials(
  SLASH_SSL,
  SLASH_TOKEN
);
const stub = new dgraph.DgraphClientStub(SLASH_ADDR, SLASH_CREDENTIALS);
// ...

But I receive an error on every call Error: 14 UNAVAILABLE: failed to connect to all addresses which I believe is related to the cert.

@jpstrikesback This PR Update gRPC library in dgraph-js to use gRPC-js by anurags92 · Pull Request #125 · dgraph-io/dgraph-js · GitHub extends dgraph-js client. You can use

const clientStub = new dgraph.clientStubFromSlashGraphQLEndpoint(
  graphQlEndpoint,
  apiKey
);
const dgraphClient = new dgraph.DgraphClient(clientStub);

to connect directly to dgraph. Keep in mind that if your cluster has been asleep for a while, it will fail for the first couple of minutes while the cluster re-starts.