How to query friends and same followings?

This is my schema:

type User {
    name
    username
    follows
    <~follows>
   ...
}
name: string @index(hash) .
username: string @index(hash) .
follows: [uid] @reverse @count .
...

The follows field is followings.
The <~follows> field is followers.
If two people follow each other, then they are friends.

I want to do a few things:

  1. Query my friends(two people follow each other) list
  2. Query me and the same followings of another user

How to write my query statement?
I use GraphQL±.

I would recommend doing something similar to this

This can query people who follow each other, but how to query my friends?
Hope to provide specific sample code.
Thanks.

Maybe I know how to query friends:

{
  user(func: uid(0x4e25)) {
    name
    friends:follows @filter(uid_in(follows, 0x4e25)) {
      uid
      name
    }
  }
}

and the same followings:

{
  user(func: uid(0x4e25)) {
    name
    friends:follows @filter(uid_in(~follows, 0x4e26)) {
      uid
      name
    }
  }
}

This would be simpler once we are able to merge the PR which supports variables inside uid_in function. That should be done hopefully by next week.

That’s really great!
Have any sample code ? I want to know in advance.

The code remains the same, just that you won’t have to hard code the uid which you currently supply in the query. UID can be stored as a variable and that variable could be passed to uid_in function. I am not on my desk right now, will update with code snippets after a while. You can take a look at the wip PR: https://github.com/dgraph-io/dgraph/pull/5320

Thanks, I look forward to it.

Check if this fits what you need.

{
  User1 as var(func: eq(name, "User1")){
    followers as ~follows 
    myfollows as follows
  }
  var(func: uid(User1)){
   fTrue as follows @filter(uid(followers))
  }
  F1 as friendsOfUser1(func: uid(fTrue)){
    name
  }
  allFriends(func: uid(F1, User1)) {
    name
  }
  maAndFollowingsOfAnother(func: uid(F1)) {
    name
    follows @filter(uid(myfollows)) { name }
  }
}

Sample dataset

{
  set {

    _:User1 <dgraph.type> "User" .
    _:User1 <name> "User1" .
    _:User1 <follows> _:User2 .
    _:User1 <follows> _:User3 .
    _:User1 <follows> _:User4 .
    _:User1 <follows> _:User5 .
    _:User1 <follows> _:User6 .

    _:User2 <dgraph.type> "User" .
    _:User2 <name> "User2" .

    _:User3 <dgraph.type> "User" .
    _:User3 <name> "User3" .

    _:User4 <dgraph.type> "User" .
    _:User4 <name> "User4" .
    _:User4 <follows> _:User1 .
    _:User4 <follows> _:User2 .
    _:User4 <follows> _:User3 .
    _:User4 <follows> _:User5 .

    _:User5 <dgraph.type> "User" .
    _:User5 <name> "User5" .

    _:User6 <dgraph.type> "User" .
    _:User6 <name> "User6" .
    _:User6 <follows> _:User1 .
    _:User6 <follows> _:User5 .

  }
}

PS. It is always good when requesting queries examples with a certain logic, that you give us a sample along with your Schema. Something “dummy”. That way we can explore the structure of your graph model and think about strategies quickly.

Of course, we can create the sample ourselves. But it is not always the same thing that the user needs and it is not as fast. With Samples, we can respond even faster.