Salta ai contenuti

Ordinamento Query Elenco

I risultati della tua query possono essere ordinati utilizzando l’argomento sort. L’ordine di ordinamento (ascendente vs discendente) viene impostato specificando ASC o DESC per il nome del campo.

Negli esempi seguenti, abbiamo una tabella chiamata students, che contiene campi e relazioni come createdAt, firstName, email.

Ecco una query in cui i risultati verranno ordinati in ordine crescente di data di creazione.

Richiesta

query MyQuery1 {
students(sort: [CREATEDAT]) {
items {
id
createdAt
firstName
email
}
}
}

Risposta

{
"data": {
"students": {
"items": [
{
"id": "287cff0a-345b-4cca-9e9a-75a2161238fd",
"createdAt": "2025-12-02T05:01:31.054581Z",
"firstName": "James",
"email": "james.smith@example.com"
},
{
"id": "97fb89ac-e0ad-44f5-b671-24a1b751287c",
"createdAt": "2025-12-02T05:03:17.180675Z",
"firstName": "John",
"email": "john.williams@example.com"
},
{
"id": "429cf99f-4481-49c4-adb4-605731b20eb2",
"createdAt": "2025-12-04T14:16:53.049955Z",
"firstName": "Mary",
"email": "mary.brown@example.com"
}
]
}
}
}

I risultati della tua query possono essere ordinati per attributi su tabelle correlate, nonché utilizzando più oggetti di ordinamento. Sono classificati in priorità dall’ordine in cui vengono ricevuti.

Richiesta

query MyQuery1 {
students(
sort: [
{ firstName: ASC },
{ email: DESC }
]
) {
items {
id
firstName
email
}
}
}

Risposta

{
"data": {
"students": {
"items": [
{
"id": "287cff0a-345b-4cca-9e9a-75a2161238fd",
"firstName": "James",
"email": "james.smith@example.com"
},
{
"id": "97fb89ac-e0ad-44f5-b671-24a1b751287c",
"firstName": "John",
"email": "john.williams@example.com"
},
{
"id": "429cf99f-4481-49c4-adb4-605731b20eb2",
"firstName": "Mary",
"email": "mary.brown@example.com"
}
]
}
}
}