# Add an edge by code

**URL:** <https://discuss.dgraph.io/t/add-an-edge-by-code/2090>\
**Category:** Users\
**Created:** [January 3, 2018, 7:14am UTC](https://discuss.dgraph.io/t/add-an-edge-by-code/2090 "2018-01-03T07:14:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![yupengfei](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/yupengfei/32/1095_2.png) [@yupengfei](https://discuss.dgraph.io/u/yupengfei)\
**Post date:** [January 3, 2018, 7:14am UTC](https://discuss.dgraph.io/t/add-an-edge-by-code/2090/1 "2018-01-03T07:14:32Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![peter](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/peter/32/574_2.png) [@peter](https://discuss.dgraph.io/u/peter)\
**Post date:** [January 3, 2018, 7:17am UTC](https://discuss.dgraph.io/t/add-an-edge-by-code/2090/2 "2018-01-03T07:17:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![zhongliangong](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/zhongliangong/32/1131_2.png) [@zhongliangong](https://discuss.dgraph.io/u/zhongliangong)\
**Post date:** [January 3, 2018, 8:28am UTC](https://discuss.dgraph.io/t/add-an-edge-by-code/2090/3 "2018-01-03T08:28:55Z")

</div>

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:

```auto
	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.

---

<div class="post-metadata">

**Author:** ![yupengfei](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/yupengfei/32/1095_2.png) [@yupengfei](https://discuss.dgraph.io/u/yupengfei)\
**Post date:** [January 3, 2018, 1:43pm UTC](https://discuss.dgraph.io/t/add-an-edge-by-code/2090/4 "2018-01-03T13:43:31Z")

</div>

Is there a go api that create edge?

---

<div class="post-metadata">

**Author:** ![yupengfei](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/yupengfei/32/1095_2.png) [@yupengfei](https://discuss.dgraph.io/u/yupengfei)\
**Post date:** [January 3, 2018, 2:32pm UTC](https://discuss.dgraph.io/t/add-an-edge-by-code/2090/5 "2018-01-03T14:32:56Z")

</div>

I see, I can use SetNquads, thank you.

---

<div class="post-metadata">

**Author:** ![yupengfei](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/yupengfei/32/1095_2.png) [@yupengfei](https://discuss.dgraph.io/u/yupengfei)\
**Post date:** [January 3, 2018, 2:33pm UTC](https://discuss.dgraph.io/t/add-an-edge-by-code/2090/6 "2018-01-03T14:33:35Z")

</div>

I think you can use SetNquads,

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

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

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/2/2b38fdece2abe5814f2e51bee69d1e67fc304b0a.png) [@system](https://discuss.dgraph.io/u/system)\
**Post date:** [February 2, 2018, 2:34pm UTC](https://discuss.dgraph.io/t/add-an-edge-by-code/2090/7 "2018-02-02T14:34:32Z")

</div>

Closed on February 2, 2018, 2:34pm UTC

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