Adding new edges without effecting existing edges

I am new to graph databases and dgraph. I want to do social networking here.
For now the goal is to have the nodes be users and the edges be follows.

An example:

Alice follows Bob, later that day Alice follows Charlie.

What I want to do

First add the edge from Alice to Bob.
Then add the edge from Alice to Charlie.

When i add the edge from Alice to Charlie, the edge between Alice and Bob dissappears and vice versa.

What I did

I have done the following mutations, all resulting in the same end result:

{
  set {
    <0xa> <follows> <0xb> .
  }
}
"{ 
     "set": [ 
        {
            "uid": "0xa", // Alice
            "follows": {
                ""uid"": "0xb" // Bob
            }
        }
    ]
}";

Right after that I did the exact same but replaced “0xb” with “0xc”

I could first query the object, then append the new follow to all existing follows, but if that list is let’s say 100,000 followers you might get performance issues due to all that json data being sent back and forth, I was wondering if I could do it some way so that the edges are added seperatly but don’t remove existing edges.

Dgraph metadata

dgraph version

v20.03.0

Looks like you have set the follows edge as one-to-one instead of one-to-many. Change your schema to

follows: [uid] .

Or maybe I didn’t get your issue.

2 Likes

The issue is that each user (like on twitter) can have multiple people he or she wants to follow.
That means 1 node has zero-to-many edges to other nodes which represents the following network.

As each user can, at any moment, unfollow and follow new people, that’s a dynamic structure. But as far as I have been able to find out, create a second follow will always replace the first follow. What I want to do is be able to create a new edge in addition to all the existing edges, not as a replacement of the already existing edges.

One way I figured how to do this is via this command:

{
  set {
    <0xa> <follows> <0xb> .
    <0xa> <follows> <0xc> .
  }
}

This will create the two edges, but I want to find a structure where I can add an edge without having to first query all of the existing edges just to append the new edge to it. This simply will not work on a large scale where the social network is huge.

Edit:

The solution was to set the “follows” predicates type to uid, and then setting adding to it the List + Count. Which is kind of what MichelDiz already pointed out, just didn’t know that a type wrapped in “[…]”, like [uid] means an array.

1 Like