4

I am trying to sort a Graphql Query in Directus v9 by a subfield.

There is a small description how to do this... but not for subfields:

https://docs.directus.io/reference/query/#sort

my query looks like this:

query {
  invoices(sort: ["customer__name"] ) {
    id
    status
    customer {
      id
      name
    }
  }
}

I have tried multiple forms like customer:name, ```customer_name``.

if i sort by field "status" in ascending order the query looks like this and it works:

query {
  invoices(sort: ["status"] ) {
    id
    status
    customer {
      id
      name
    }
  }
}

if i sort by field "status" in descending order the query looks like this and it works:

query {
  invoices(sort: ["-status"] ) {
    id
    status
    customer {
      id
      name
    }
  }
}

has anyone tried this?

  • please go through this doc -- https://docs.directus.io/reference/query/#deep – Gaurav Apr 14 '22 at 08:01
  • "Deep" section is about filtering and sorting nested tables data, not parent table by nested fields. So in this example, if one invoice have many customers, you can sort the customers by their names. – Artur Czyżewski May 27 '22 at 14:29

1 Answers1

0

Unfortunately, sorting by related table's subfield it is not possible by now. See: https://github.com/directus/directus/discussions/4502

Edit: https://github.com/directus/directus/pull/12084 they added support for sorting by nested M2O relation in v9.10.0.

Artur Czyżewski
  • 705
  • 5
  • 12