Edge best practises re updating

Hi, is there any side effect to (repeatedly) “creating” an edge between 2 nodes?

e.g. is repeatedly running the following ok (i.e. its not an anti-pattern with a better approach, e.g. an upsert…)?

   <0x1> <follows>  <0x2>

Question 1.1:
Is there upsert support for edges!?

Question 2:
Is there upsert support for facets?
I have a use case where I want to add a created date on the follows rel, then an updated date.

Thanks!

Hey @damienburke,

I don’t think there should be any side-effect of repeatedly creating an edge.

Yes. You can do an upsert for edges. Consider the following example:

Let the data be as follows:

{
	set{
  	_:a <follows> _:b .
  	_:a <name> "alice" .
  	_:b <name> "bob" .
    _:c <name> "carl" .
  }
}

We can change the edge “alice follows bob” to “alice follows carl” by the following upsert:

upsert {
  query {
    q(func: eq(name, "alice")){
      u as uid
      follows {
        old as uid
      }
    }
    q2(func: eq(name, "carl")){
      v as uid
    }
  }
  mutation {
    set{
      uid(u) <follows> uid(v) .
    }
    delete {
      uid(u) <follows> uid(old) .
    }
  }
}

Yes. Consider the following example:

{
	set{
  	_:a <follows> _:b (since=2019-01-01) .
  	_:a <name> "alice" .
  	_:b <name> "bob" .
  }
}

We can change the date “since” to an updated date by the following upsert:

upsert {
  query {
    q(func: eq(name, "alice")){
      u as uid
      follows {
        f as uid
      }
    }
  }
  mutation {
    set{
      uid(u) <follows> uid(f) (since=2020-07-11) .
    }
  }
}

Feel free to shoot followup questions. Please mark it solved if this helps :slight_smile:

2 Likes

Thanks @ahsan
I just need to understand the last use case a bit more (everything else is 100% clear, thanks!)

re

       uid(u) <follows> uid(f) (since=2020-07-11) .

let’s say w have an updated (date) facet too. Can this be integrated into your example, so it can be read/set? So lets say since is not to be updated (if exists) and updated is.

(incomplete/invalid mutation to help clarify question)

      uid(u) <follows> uid(f) (since=val(some_facet_var??), updated=2020-07-11) .

Hey @damienburke,

Dgraph currently doesn’t support facet variables in the upsert mutation. It is an useful functionality to have and we have had this RFC in past and hope to get it in dgraph soon.
Ref: https://github.com/dgraph-io/dgraph/issues/1996

Currently, if you are using some client, then I would suggest you to fetch the facets by a query and then form the new facets (including the “updated” field) in the client side itself. Then you can mutate for the edge to update the facets.

1 Like