Add an edge by code


(于鹏飞) #1

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?


(Peter Stace) #2

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.


(Zhong Liang Ong) #3

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(
		"person.household",
		personHouseholdNode)

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.


(于鹏飞) #4

Is there a go api that create edge?


(于鹏飞) #5

I see, I can use SetNquads, thank you.


(于鹏飞) #6

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),
	})

(system) #7

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