Ordering by nested field

I Want to Do

I want to order my results by a nested field (field from another node)

What I Did

I extended the Task example, adding a date to the schema:

type Task {
  id: ID!
  title: String!
  completed: Boolean!
  user: User!
  date: ReportedDate @hasInverse(field: task)
}

type ReportedDate {
  date: DateTime
  task: Task
}

type User {
  username: String! @id
  name: String
  tasks: [Task] @hasInverse(field: user)
}

Now I want to retrieve all tasks per user, sorted by their respective dates using GraphQL on top of Dgraph.

2 Likes

Sorting using a nested field is currently not supported.

As a possible workaround, you may include dateTime: DateTime field directly inside type Task .
If that is done, the following query will be able to fetch tasks sorted according to Date for the User.

query{
  queryUser{
    name
    tasks(order:{asc:dateTime}){
      title
    }
  }
}

I am trying to understand your use case here. Is there any specific reason that ReportedDate has been as a separate type rather than having dateTime: DateTime field inside type Task.

If your use case is fairly common and there is no other way to do this, we can accept this as a feature request. It will then be be worked upon along with supporting nested filters.

1 Like

I have many use cases for this. One being sorting addresses by state or city where state and city are their own types. They are their own types to allow to do a quick selection of all cities or all states for building out a selection list in the GUI. We also have census data and geo data attached to the city and states. That is just one example.

1 Like

Thanks for your quick reply @amaster507 .

Yeah, this feels like a valid use case. I have accepted this as a feature request and will be worked upon (after 21.03).

3 Likes

Awesome @rajas!