GraphQL+ query for datetime delta/duration


#1

Is it possible to query for the duration between two datetimes? I see that the subtraction operator doesn’t apply to the datetime type.


(Michel Conrado) #2

You can do something like this: (Run in https://play.dgraph.io)

{
  var(func:allofterms(name@en, "steven spielberg")) {
    films as director.film {
      p as count(starring)
      q as count(genre)
      date as initial_release_date
      years as math(since(date)/(365*24*60*60))
      score as math(cond(years > 10, 0, ln(p)+q-ln(years)))
    }
  }

  TopMovies(func: uid(films), orderasc: val(years)) #@filter(gt(val(score), 2))
  @filter(gt(val(years), 6) and le(val(years), 14))
  {
    name@en
    val(score)
    val(date)
    val(years)
  }
}