Dgo grpc client mutation example

I have the following code I’m trying to get working,

func (dg *Dgraph) CreateOrder(ctx context.Context, jwt string, jsondata []byte) ([]byte, error) {
	md := metadata.New(map[string]string{"Content-Type": "application/grpc", "Authorization": jwt, "auth-token": jwt})
	ctx = metadata.NewOutgoingContext(ctx, md)

	mu := &api.Mutation{
		SetJson:   jsondata,
		CommitNow: true,
	}

	txn := dg.client.NewTxn()

	res, err := txn.Mutate(ctx, mu)
	if err != nil {
		log.Printf("error creating order - %s", err)
		return []byte{}, err // skip if broken at this stage
	}

	// defer txn.Discard(ctx)
	log.Printf("ORDER CREATED: %s", res)
	return res.GetJson(), nil
}

I’m confident the dg.client is valid as I can successfully list orders.

If I call this function (through HTTP) with a valid JWT and invalid JSON, I’d expect to see some error with an indication of how it failed, no err is triggered and the output shows the following

2021/07/04 11:29:55 ORDER CREATED: txn:<start_ts:21111393 commit_ts:21111395 preds:"1-1f-country" preds:"1-1f-name" preds:"1-1f-product" preds:"1-1f-subject" hash:"86667f2c6988dea8dc2378a4f160d1f4789e247d0151778a52870b776f858c09" > latency:<parsing_ns:147705 processing_ns:49458303 assign_timestamp_ns:972110 total_ns:50818161 > metrics:<num_uids:<key:"_total" value:7 > num_uids:<key:"mutation_cost" value:7 > > uids:<key:"dg.4195284563.295" value:"0x4c6cd146" > uids:<key:"dg.4195284563.296" value:"0x4c6cd145" > hdrs:<key:"access-control-allow-credentials" value:<value:"true" > > hdrs:<key:"access-control-allow-methods" value:<value:"GET,POST,OPTIONS" > > hdrs:<key:"access-control-allow-origin" value:<value:"https://cloud.dgraph.io" > > hdrs:<key:"content-type" value:<value:"application/grpc" > > hdrs:<key:"date" value:<value:"Sun, 04 Jul 2021 11:30:00 GMT" > > hdrs:<key:"dgraph-toucheduids" value:<value:"7" > > hdrs:<key:"server" value:<value:"Caddy" > >

I can’t really tell what is happening, did I not send some CORS headers, is the Auth wrong, did the invalid JSON data cause an error? I don’t know, please help

T

Hi @taemon, we unable to understand the issue. Can you clarify?

Aman: According to logs the mutation is a success

Cheers.

I am trying to follow the dgo mutate example below from dgo/examples_test.go at master · dgraph-io/dgo · GitHub

	mu := &api.Mutation{
		CommitNow: true,
	}
	pb, err := json.Marshal(p)
	if err != nil {
		log.Fatal(err)
	}

	mu.SetJson = pb
	response, err := dg.NewTxn().Mutate(ctx, mu)
	if err != nil {
		log.Fatal(err)
	}

If I’m trying to create a new order, do I just send it a new Order structure in the format the schema requires?

I’m just trying to get a basic dgo grpc call to create a new order, seems like a simple usecase.

The Log message ‘ORDER CREATED’ is just my own log message showing that no error occurred but I would have expected either an authentication failure or a bad data error but got neither.

1 Like

Any good examples more than the one listed?

Hi @taemon , the mutation success is based on the response data.

txn:<start_ts:21111393 commit_ts:21111395 preds:"1-1f-country" preds:"1-1f-name" preds:"1-1f-product" preds:"1-1f-subject" hash:"86667f2c6988dea8dc2378a4f160d1f4789e247d0151778a52870b776f858c09" > latency:<parsing_ns:147705 processing_ns:49458303 assign_timestamp_ns:972110 total_ns:50818161 > metrics:<num_uids:<key:"_total" value:7 > num_uids:<key:"mutation_cost" value:7 > > uids:<key:"dg.4195284563.295" value:"0x4c6cd146" > uids:<key:"dg.4195284563.296" value:"0x4c6cd145" > hdrs:<key:"access-control-allow-credentials" value:<value:"true" > > hdrs:<key:"access-control-allow-methods" value:<value:"GET,POST,OPTIONS" > > hdrs:<key:"access-control-allow-origin" value:<value:"https://cloud.dgraph.io" > > hdrs:<key:"content-type" value:<value:"application/grpc" > > hdrs:<key:"date" value:<value:"Sun, 04 Jul 2021 11:30:00 GMT" > > hdrs:<key:"dgraph-toucheduids" value:<value:"7" > > hdrs:<key:"server" value:<value:"Caddy" > >

This explains that a mutation with start_ts 21111393 and commit_ts 21111395 is executed successfully with preds defined in the response. You can also see the latencies during the execution of the mutation.

Yes you need to send the order structure in the format of the schema. If the schema mode is flexible than if there is a predicate in the object not defined in the schema it will be created by dgraph with respective data type.

Any reason why you would expect an error? Is there any auth rules that should have triggered and cause the mutation to fail.

1 Like

Ahh I think I understand better. I was sending an invalid Order type, but I guess it just saved it as non-schema data or something.