Sorting
Request format
GET /v1/rest/<subgraph-name>/<model-name>?sort=<fields>
Parameter | Description | Example |
---|---|---|
subgraph-name | Name of your subgraph. | "default" |
model-name | Name of your model. | "Users" |
fields | Comma-separated list of fields, prefix with - for descending order. | "name" , "-created_at" |
Examples
Single field
Fetch all articles sorted by title in ascending order.
Request:
GET /v1/rest/default/Articles?sort=title
Response:
{
"data": [
{
"type": "Articles",
"id": "1",
"attributes": {
"title": "A First Article",
"created_at": "2023-01-02T12:00:00Z"
}
},
{
"type": "Articles",
"id": "2",
"attributes": {
"title": "B Second Article",
"created_at": "2023-01-01T12:00:00Z"
}
}
]
}
Multiple fields
Fetch all articles sorted by created date in descending order and then by title in ascending order.
Request:
GET /v1/rest/default/Articles?sort=-created_at,title
Response:
{
"data": [
{
"type": "Articles",
"id": "1",
"attributes": {
"title": "A First Article",
"created_at": "2023-01-02T12:00:00Z"
}
},
{
"type": "Articles",
"id": "3",
"attributes": {
"title": "C Third Article",
"created_at": "2023-01-02T12:00:00Z"
}
},
{
"type": "Articles",
"id": "2",
"attributes": {
"title": "B Second Article",
"created_at": "2023-01-01T12:00:00Z"
}
}
]
}
Descending field
Fetch all articles sorted by views in descending order.
Request:
GET /v1/rest/default/Articles?sort=-views
Response:
{
"data": [
{
"type": "Articles",
"id": "1",
"attributes": {
"title": "Popular Article",
"views": 2000
}
},
{
"type": "Articles",
"id": "2",
"attributes": {
"title": "Less Popular Article",
"views": 1500
}
}
]
}