When querying user list, how to judge whether has followed the user and return bool?

My schema:

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

I want to query the user list, and the user of this list should contain the is_follow field, used to judge whether I have followed this user.

Suppose I make the following query:

{
  users(func: has(username)) {
      uid
      name
      username
      is_follow: follows @filter(uid(0x4e25)){
        uid
        name
      }
  }
}

The 0x4e25 is the uid of the currently logged in user.
The is_follow field lets me know if the user is followed, but its value is user, and I want it to be a bool. What should I do?

If you used GraphQL, I would have suggested using GraphQL Lodash as you would be able to convert that to boolean directly in query.

But, Is it possible to write the above query in GraphQL?

If not, then I am also interested in the solution to above question. As it relates to http://discuss.dgraph.io/t/how-to-do-negative-lookups-in-dgraph/7202/7

Hi @pandalive,

I am scratching my head to figure out a way to get boolean result. But, as of now, I am able to come up with a simple trick which would give 0 or 1 result.

{
users(func: has(username)){
    uid
    name
    username
    is_follow: count(follows) @filter(uid(0x4e25))
    }
}
1 Like

@abhijit-kar Thanks. But instead of using GraphQL, I use GraphQL±.

@ahsan This is a good solution :grinning:, if there is no better, I can only use it. I look forward to answers from dgraph team.

That’s a good one!

I use both, still at beginning stage of learning though & It might be possible to write the above query in GraphQL Filtering in Selection, will have to verify.

Agreed.

@ahsan I still hope to get a bool, hope team can do this.
Cheers.

@ahsan Hi. I encountered another problem.
How to ignore is_follow query when user is not logged in.
Suppose I make the following query:

query all(
  $uid: string,
  $logged_user_id: string
) {
  data(func: uid($uid)) {
     uid 
     name
     username
     is_follow: count(follows @filter(uid($logged_user_id)))
  }
};

Executing a query when not logged in now throws an exception.

Hi @pandalive,

Do you mean that the node doesn’t exist for the user which is not logged in? I am not getting exception for non-existent nodes. It would be very helpful if you could let me know how to reproduce this exception on a toy data.

@ahsan When not logged in, the value of $logged_user_id is " ", the query will throw an exception:

CannotQuery(
        Status {
            code: Unknown,
            message: "Id can\'t be empty",
        },
    ),

Does dgraph query cause an error when the variable value does not exist ?

I want to ignore the is_follow query above when the variable $logged_user_id does not exist.

If Dgraph supports directives like

  • @include(if: Boolean) Only include this field in the result if the argument is true .
  • @skip(if: Boolean) Skip this field if the argument is true .

Or if the query can succeed with “” or null value.

Or by using fragments to avoid repetition and then creating multiple gql queries for each case.

Or by preventing API call altogether and prompting user to sign in.

Then it would be possible.

1 Like

@abhijit-kar Good advice!

@ahsan Looking forward to your answer.

Hi @pandalive, we appreciate your patience.
As for your first query regarding bool values. One way that I and @ahsan could figure out is to use math().

EDITED:

{
  data(func: uid(0x3)) {
     name
     cnt as count(follows) @filter(uid(0x4))
     result: math( cnt == 1 )
  }
}

Meanwhile, you can look at the RFC around the improvement.
Also, you can look at the discussion related to custom variable blocks.

1 Like

@pandalive I have edited the answer for a better query. Please have a look.

Hi @pandalive,

You can solve this problem by setting a default value to $logged_user_id as 0x0 so that it is not empty.

1 Like

@Naman Thanks, solved my problem, and look forward to the realization of RFC as soon as possible.
Cheers.

1 Like

@ahsan Thanks, this is a feasible plan.
But even so, I still hope that when the query variable does not exist, dgraph will set it to 0x0 by default, which can save a lot of work for customers.
Cheers.