Dgraph DQL Tour
Var Blocks
Exercise : Latest Movies
Write a query to find the most recently released movie by each director and
order the result by the release dates.
If you need some hints try:
Show answer
To solve this, you’ll need to
- work out how to query for all directors - what have directors done that make
them directors?
- find the most recent release date for each director
- sort the results by the most recent release
- return the directors name, and the details of the most recent release
- you’ll need one query to get the directors and their latest movies, and
another query to sort this to get most recent movies first
- for something extra, try using
since
to work out how many days since the
movie was released (or how many days till release, for some)
Spoiler alert:
Show answer
{
# Get all directors
var(func: has(director.film)) @cascade {
director.film {
date as initial_release_date
}
# Store maxDate as a variable
maxDate as max(val(date))
daysSince as math(since(maxDate)/(24*60*60))
}
# Order by maxDate
me(func: uid(maxDate), orderdesc: val(maxDate), first: 10) {
[email protected]
days : val(daysSince)
# For each director, sort by release date and get latest movie.
director.film(orderdesc: initial_release_date, first: 1) {
[email protected]
initial_release_date
}
}
}
Dgraph DQL Tour
An interactive tutorial with real queries and real results,
designed to run on Chrome on your machine.
DQL Query Blocks and Variables
This module shows you how to build more powerful queries with multiple query blocks and query variables.