We’re overhauling Dgraph’s docs to make them clearer and more approachable. If
you notice any issues during this transition or have suggestions, please
let us know.
@hasInverse directive to tell Dgraph how to handle two-way
relationship.
One-way relationship
If you only ever need to traverse the graph between nodes in a particular direction, then your schema can simply contain the types and the relationship. In this schema, posts have an author - each post in the graph is linked to its author - but that relationship is one-way.Dgraph won’t store the reverse direction, so if you change your schema to
include a
@hasInverse, you’ll need to migrate the data to add the reverse
edges.Two-way relationship
In Dgraph, the directive@hasInverse is used to create a two-way relationship.
posts and author are just two directions of the same link in the
graph. For example, adding a new post with
posts. Deleting the post removes it
from Alice’s posts. Similarly, using an update mutation on an author to insert
a new post automatically adds Alice as the author.
Many edges
It isn’t possible to determine what a schema designer meant for two-way edges. There’s not even a single possible relationship between two types. Consider, for example, if an app recorded the posts anAuthor had recently liked (so it can
suggest interesting material) and just a tally of all likes on a post.
@hasInverse is needed - so you
can enforce the semantics your app needs.