@ORDERBY Directive

The directive @orderby provides an orderby clause on the field in which it is specified.

This directive has one argument: SQL in which you can provide the SQL expression for ordering the output. @orderby directive should be used on those object fields which return an array.

SELECT JSON_SERIALIZE(data PRETTY) AS data FROM GRAPHQL('
    query getDriversBestPerformances {
        driver {
            id: driver_id
            name
            points
            bestPerformances: driver_race_map (
                check: {
                    position: {_lte: 3}
                }
            ) @orderby(sql: "position asc")
            {
                position
                race @unnest {
                    race: name
                }
            }
        }
    }
');

The preceding example retrieves the driver details along with the races in which driver finished in top 3, ordered by position:

DATA
--------------------------------------------------------------------------------
{
  "id" : 101,
  "name" : "Lando Norris",
  "points" : 282,
  "bestPerformances" :
  [
    {
      "position" : 1,
      "race" : "Miami Grand Prix"
    },
    {
      "position" : 1,
      "race" : "Abu Dhabi Grand Prix"
    },
    {
      "position" : 1,
      "race" : "Singapore Grand Prix"
    },
    {
      "position" : 1,
      "race" : "Dutch Grand Prix"
    },
    {
      "position" : 2,
      "race" : "Australian Grand Prix"
    },
    {
      "position" : 2,
      "race" : "Qatar Grand Prix"
    },
    {
      "position" : 2,
      "race" : "Azerbaijan Grand Prix"
    },
    {
      "position" : 2,
      "race" : "British Grand Prix"
    },
    {
      "position" : 2,
      "race" : "Emilia Romagna Grand Prix"
    },
    {
      "position" : 3,
      "race" : "Spanish Grand Prix"
    },
    {
      "position" : 3,
      "race" : "Belgian Grand Prix"
    },
    {
      "position" : 3,
      "race" : "United States Grand Prix"
    },
    {
      "position" : 3,
      "race" : "São Paulo Grand Prix"
    }
  ]
}

{
  "id" : 102,
  "name" : "Oscar Piastri",
  "points" : 384,
  "bestPerformances" :
  [
    {
      "position" : 1,
      "race" : "Hungarian Grand Prix"
    },
    {
      "position" : 1,
      "race" : "Azerbaijan Grand Prix"
    },
    {
      "position" : 3,
      "race" : "Japanese Grand Prix"
    }
  ]
}
.......................................
.......................................
20 rows selected.