How to query (create) reverse edge with go client?

I’m new to dgraph and graphql.

I have a user. the user has a list of posts.
And I have a post that have a user.

in the schema, I marked the user from post as reverse.

name: string .
posts: [uid] @reverse @count .
user: uid
type Post {
name: string
user: User
type User {
name: string
posts: [Post]

I first added a user without any posts.
now I added a post and in the golang client I added the uid from the user
like this

post.user = User{userUid}

when I query for type(Post)
I get the user as a result of the query.

But how do I need to query the user, to get all the posts of the user in the result?

The reverse directive is just a “Reverse Index”. You don’t need to add anything. It is automatically added based on the parent.

For example. If you add a user Bob. And later you wanna add a post from Bob. You should add "uid": "uid_from_Bob, "posts": [{MY NEW POST}]. The direction is always Parent to => Child. So, the creation of the post must come with Bob’s UID and the edge posts.

To retrieve Bob when query the post you just need to use the tilde notation in the query block request.

thank you for your reply,
but it doesn’t work for me, or I don’t get it.

if I add Bob with just the uid to the post,
I can always query for post and can get bob from that post without the tilde.
I never get the post when I query for bob
I only get the post if I query for bob, if I add

  1. the bob to the post and
  2. this post then to bob and
  3. store bob

all other combinations were to declare @reverse in the schema on user or on posts or on both, and with or without tilde in the type declaration seem to have no effect.

did I understand something completely wrong or where is my mistake?
sorry, I’m new to graphdb.

I found it :slight_smile:

I don’t have to add a property(field) posts on user type.
I just declare this posts property during a query with reverse and then I have all the posts for this user :slight_smile:

yeah makes sense. otherwise, the graph would be an endless loop.

hmm I’m confused. Looks you are questioning about HasInverse instead of Reverse. Well, what I mentioned to you should work as always did. The issue is that I don’t know the whole picture at your side to give you the precise answer, you know? But it is a fact, in the case of Reverse edges(not HasInverse) you don’t need to do anything. Just link the parent to the new child. Nothing more.

But in your case the parent could be the posts instead of the user. Or some other modeling. We never know.


I found it already.
My issue was the I declared the fields in both types.
so also posts [uid] in user type.

now when I remove the posts in user type. It’s working with the @reverse on User in Post type and a reverse field in the query for users.

thanks a lot