Adding new edges using Golang

I need to add new edges during streaming process. what I need to do is that inter an edge if there is not an edge between two nodes. If there is already then do something else. If the nodes does not exist, then these nodes should be created as well.

I am trying to following the example codes here But I am quite new to Golang. How to represent an edge in Golang ? I understand that an edge can be represented in RDF form in a file. But I did not want to use file as intermediate step.

In this example Person type is defined. I guess following codes are used to create Person and other attribute nodes. How edges are created? I guess Friends should be an edge predicate, right ? How does this piece of codes work ?

p := Person{
	Name:    "Alice",
	Age:     26,
	Married: true,
	Location: loc{
		Type:   "Point",
		Coords: []float64{1.1, 2},
	Dob: &dob,
	Raw: []byte("raw_bytes"),
	Friends: []Person{{
		Name: "Bob",
		Age:  24,
	}, {
		Name: "Charlie",
		Age:  29,
	School: []School{{
		Name: "Crown Public School",

mu := &api.Mutation{
	CommitNow: true,


In particular, how to define an edge in the type struct?
For example here
type DirectedEdge struct {
Entity uint64
Attr string
Value byte
ValueType uint32
ValueId uint64
Label string
Lang string
Op DirectedEdge_Op // Set or Delete
Facets []*facetsp.Facet

What should Op and Facets should be ?

Hey @farscape2012

A struct in Go client is marshalled to JSON format. This struct would be marshalled to something like.

{"name":"Alice","age":26,"dob":"1980-01-01T23:00:00Z","married":true,"raw_bytes":"cmF3X2J5dGVz","friend":[{"name":"Bob","age":24},{"name":"Charlie","age":29}],"school":[{"name":"Crown Public School"}]}

After this, the rules mentioned at apply.

Since the root node, doesn’t have a uid, a new node would be created with the properties name, married etc. Two new nodes would also be created for Bob and Charlie and connected to Alice using the friend edge.

_:alice <name> "Alice" .
_:alice <friend> _:bob .
_:bob <name> "Bob" .

You would have to query for the edge and if it doesn’t exist then you create one. You don’t need to think in terms of DirectedEdge as that is an internal data structure that we use.

Thx @pawan
What if I want to insert a new edge between an existing node and a new node? Can I create an instance of Person struct like following ? Does it automatically check whether the node with uid 0x100 and the new node (Bob) exist and insert an edge between them after creating Bot node?

type Person struct {
    Uid      string     `json:"uid,omitempty"`
    Name     string     `json:"name,omitempty"`
    Age      int        `json:"age,omitempty"`
    Dob      *time.Time `json:"dob,omitempty"`
    Married  bool       `json:"married,omitempty"`
    Raw      []byte     `json:"raw_bytes",omitempty`
    Friends  []Person   `json:"friend,omitempty"`
    Location loc        `json:"loc,omitempty"`
    School   []School   `json:"school,omitempty"`

_**p := Person{**_
_**        UID: "0X100"**_
_**	Friends: []Person{{**_
_**		Name: "Bob",**_
_**		Age:  24,**_
_**	}},**_
  pb, err := json.Marshal(person)
  mu.SetJson = pb
  assigned, err := dg.NewTxn().Mutate(ctx, mu)

If I define Friends edge as below, does it mean Person can have only (max) one edge ?

Friends: Person

What you are doing here is correct. Dgraph would create a new node for Bob and connect it to the node with uid 100.

p := Person{
         UID: "0X100",
         Friends: []Person{{
		Name: "Bob",
		Age:  24,

translates to

<100> <friend> _:b .
_:b <name> "Bob" .
_:b <age> "24" .

No, the Go struct representation is not related to the dgraph schema. In Dgraph a uid edge can always be connected to multiple uids. Though, we have an open issue to only allow only one uid.

1 Like

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