How to Order by Nested Data using GraphQL

We have discussed filtering on edges multiple times mot recently: Filter on non-scalar fields or relations (Types), which is closely related to the topic of ordering on edges.

Given sample schema:

type Contact {
  name: String!
  tags: [Tag]
  category: Category
}
type Tag {
  tag @id
}
type Category {
  cat @id
}

With the query:

{
  queryContact(first: 5) {
    name
    tags { tag }
    category { cat }
  }
}

I could get a response back like:

data.queryContact: [
  { name: "A", tags: [{ tag: "n" }], category: { cat: null } },
  { name: "B", tags: [{ tag: "m" }], category: { cat: "z" } },
  { name: "C", tags: [{ tag: "p" }, { tag: "o" }], category: { cat: "x" } },
  { name: "E", tags: [], category: { cat: "y" } },
  { name: "D", tags: [], category: { cat: null } },
]

I can sort by name, but I cannot sort the above results by tags.tag nor category.cat

I am needing to this level of sorting. Is it possible without DQL and without doing it completely client side?

And just to be clear. I understand that I can order the subgraph, but what is wanted and needed is ordering the parent graph based upon the data from the sub graph.

This is not possible natively in GraphQL and requires using DQL. Out of curiosity, since tags.tag can have multiple values per Contact, how do you expect this to be sorted? Is it like you’d want to sort by the minimum possible tag per contact, so if contact had tags a and z, you’d use a when doing ascending sort and z while doing a descending sort?

That is sort of the wildcard that I threw in there to show some complexity to the issue. The use cases really vary on this and it could be sorted by length of the array, or alphabetically of the first item in the set. Really I just wanted to show that a simple sort by Contact.cat would not be the same in essence of sorting by Tags.tag. Ideally one could supply a sort function. But that is lending towards a solution in a JS hook which again has its own complexities. Sorting already queried data would not honor pagination. I don’t have the perfect answer for this, just wanted to throw out there what I am trying to wrap my head around. Sorting in a relational database with joining data is very different from sorting in a graph.