Dgraph GraphQL Tour

Basic

Sort Query Results

You can sort (or order) query results by using the order argument on edges and query<Type> query functions.

When you apply ordering, the visualization of the graph remains unchanged, but the JSON response returned contains an ordered list.

The generated <type>Order input type accepts three arguments:

  • asc - Sort by a field in ascending order
  • desc - Sort by a field in descending order, and
  • then - Add an additional sort argument.

In the example query shown in the editor, we query all nodes of the type Person and sort those nodes by the name field in ascending order.

Something to try: Sort Alice’s friends by their age, in descending order; and then by their name, in ascending order.

For every type, Dgraph will generate a list of fields that are orderable. Edges to other nodes are not orderable fields within a type.

To introspect the available order fields, inspect the generated <type>Orderable enum.

{
  __type(name: "PersonOrderable") {
    name
    enumValues {
      name
    }
  }
}

2.7 Sort Query Results