Add an edge by code

I already put some items into dgraph, and I want to add edges later, for example, I want to recommend friend to a person after he upload his address list, and at that time, the person has been in graph for long. How can I archive this in one operation or need I do it in two query for the uid and one for the mutation?

Hi @yupengfei,

You will need to perform two operations, first query for the uid(s), and then perform the mutation. These two operations should be in the same transaction.

Replying here, because I felt it’s more relevant vs making a new topic.

Assuming both UIDs are present, is there a way to add an edge between 2 nodes in a similar manner to the v0.8 and earlier versions, where only the UIDs need to be known, vs having to fetch the entire structs of both nodes to populate the mu.SetJSON request?

Earlier example:

	personNode := g.DgraphClient.NodeUid( newPersonNodeUid )
	personHouseholdNode := g.DgraphClient.NodeUid( req.Person.Householduid )
	newEdgeCursor := personNode.ConnectTo(

In the new JSON-based mutations, I would need to fetch both the contents of the Person and Household Entities to populate the JSON request, which might take another query.

Is there a go api that create edge?

I see, I can use SetNquads, thank you.

I think you can use SetNquads,

	pb := fmt.Sprintf(`<%s> <%s> <%s> .`, uidRepos.UserIDs[0].UID, relation, uidRepos.UserIDs[1].UID)

	_, err = txn.Mutate(ctx, &api.Mutation{
		SetNquads: []byte(pb),

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.