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.


Hi Harshad,

Thanks for the question.

We’ve recently moved the documentation for this over here:

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:

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(
			RootCAs:    pool,
			ServerName: strings.Split(grpcEndpoint, ":")[0],



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 {

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


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>';
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(
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 extends dgraph-js client. You can use

const clientStub = new dgraph.clientStubFromSlashGraphQLEndpoint(
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.