What does "~{field}" do?

GraphQL noob here, having trouble finding information on what “~” does in a field name. Seeing it used in the tutorial for cases like this:

curl localhost:8080/query -XPOST -d ‘{
genre(func: gt(count(~genre), 30000)){
name@en
~genre (first:1) {
name@en
}
}
}’ | python -m json.tool | less

Is it purely for avoiding variable name reuse since the block is already named “genre”?

Hey @1vn

~ is used to refer to reverse edges. So say movies have genres

<movie_1> <genre> <genre_1>
<movie_1> <genre> <genre_2>
<movie_2> <genre> <genre_1>

And you had a @reverse directive on your schema, then Dgraph would automatically add edges in the reverse direction for you.

genre: uid @reverse

Then you could get movies related to a particular genre

me(func: uid(genre_1)) {
  # ~genre points to movies
  ~genre {
    uid
  }
}

Note that me here doesn’t have any special significance and is just used to identify a query block.

References

  1. https://tour.dgraph.io/schema/5/
  2. https://docs.dgraph.io/query-language/#reverse-edges
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.