I am writing a Golang application using Dgraph for persisting objects. From the documentation, I can infer that a new UID and hence a new node is created everytime I mutate an object/run the code.
Is there a way to update the same node data instead for creating a new node?
@iluminae I am using uid which is generated by dgraph itself. When I run the code, there is no error but the record in my dgraph does not get any updated.
Here is my code, hope you can help.
func (dg *Dgraph) UpdateOrderState(ctx context.Context, jwt string, id string, state string, chargeId string) ([]byte, error) {
o := &models.Order{Uid: id, State: state, StripeChargeId: chargeId}
md := metadata.New(map[string]string{"Content-Type": "application/grpc", "Authorization": jwt, "auth-token": jwt})
ctx = metadata.NewOutgoingContext(ctx, md)
ojson, err := json.Marshal(o)
if err != nil {
log.Printf("Could not marshal order - %s", err)
return []byte{}, err // invalid order
}
mu := &api.Mutation{
SetJson: ojson,
CommitNow: true,
}
txn := dg.client.NewTxn()
res, err := txn.Mutate(ctx, mu)
if err != nil {
log.Printf("error updating order - %s", err)
return []byte{}, err // skip if broken at this stage
}
// defer txn.Discard(ctx)
log.Printf("ORDER UPDATED: %s", res.GetJson())
return res.GetJson(), nil
}
Ah the json mutation format - I assume something is wrong with the rendered json after the marshal. Read here about using existing uids in the json mutation format.
Also I would highly suggest using the protobuf nquad api (Set on the Mutation) to know exactly what you are doing in a type-safe way, but good luck either way.
can you share a literal input to Mutate that is not working rather than the code? Work backwards from just sending the literal string {"uid":"0x1","field":"value1"} and testing with that?