Sorting NReco.GraphQL documentation
You can set the order of the query result elements using the orderBy argument. By default, the results aren't sorted. Sorting can be specified by more than one field, to related subqueries, or even to dynamic aggregation fields
Sort configuration
Sorting is available by argument key "sortBy" for all defined schemas (including related schemas) and it can be changed via setting filter option in the json file. To change sort key setSort
section inside section of Options
in the JSON configuration:
"Options": { "Sort": { "Key": "sortBy", "FieldName": "field", "Direction": "direction", "Enabled":true }
Property | Purpose |
---|---|
Key | sort argument name in the GraphQL query |
FieldName |
sorting by field (key-word). By default - "field" |
Direction |
Sorting key-word direction. By default - "direction". Accepts values: ASC, DESC (case insensitive) |
Enabled |
turn on/off sort feature; field may be omitted - by default true |
An example of using sorting
Graphql query:
query { orders(sortBy:{"field": "orderId", "direction": "asc"}) { orderId status } }
Response:
{ "data": { "orders": [ { "orderId": 1006, "status": "Approved" }, { "orderId": 1106, "status": "InProgress" } ] } }
An example of using sorting (related schema)
Graphql query:
query { customers(sortBy:{"field": "id", "direction": "asc"}) { id name orders(sortBy:{field:"ShipName", direction:"desc"}){ orderId ShipName aggregate_count } } }
Response:
{ "data": { "customers": [{ "id": "WOLZA", "Name": "Wolski Zajazd", "orders": [ { "orderId": 10374, "ShipName": "Wolski Zajazd", "aggregate_count": 1 }, { "orderId": 10611, "ShipName": "Wolski Zajazd", "aggregate_count": 1 } ] }] } }
An example of using sorting - by defined and aggregation fields (an array of params)
Graphql query:
query { customers(sortBy:{"field": "id", "direction": "desc"}) { id name orders(sortBy:[{field:"ShipVia", direction:"asc"}, {field:"aggregate_count", direction:"desc"}]){ orderId ShipName aggregate_count } } }
Response:
{ "data": { "customers": [ { "id": "WOLZA", "CompanyName": "Wolski Zajazd", "orders": [ { "ShipCity": "Warszawa", "ShipVia": 1, "aggregate_count": 1 }, { "ShipCity": "Warszawa", "ShipVia": 2, "aggregate_count": 2 }, { "ShipCity": "Warszawa", "ShipVia": 3, "aggregate_count": 4 } ] }, { "id": "WILMK", "CompanyName": "Wilman Kala", "orders": [ { "ShipCity": "Helsinki", "ShipVia": 1, "aggregate_count": 4 }, { "ShipCity": "Helsinki", "ShipVia": 3, "aggregate_count": 3 }] }] } }