Collection Order

The GraphQL Content API allows users to specify the fields and direction to sort on root collection queries.

For example, for the type FriendlyUser structured in the following way:

1type FriendlyUser {
2 sys: Sys
3 name: String
4 age: Int
5}

The schema defines the following order input enum type:

1enum FriendlyUserOrder {
2 name_ASC
3 name_DESC
4 age_ASC
5 age_DESC
6 sys_id_ASC
7 sys_id_DESC
8}

Order enum values can be passed to collection queries of their corresponding type to sort the result set.

For example, to find the oldest FriendlyUser, write the following query:

1query {
2 friendlyUserCollection(order: [age_DESC], limit: 1) {
3 items {
4 name
5 }
6 }
7}

Collections can be sorted by multiple fields, each of them with a direction information.

For example, to order FriendlyUsers by their age (descending) first and for items with same age it should sort by name (ascending), write the following query:

1query {
2 friendlyUserCollection(order: [age_DESC, name_ASC]) {
3 items {
4 name
5 age
6 }
7 }
8}

You can order collections in linkedFrom:

1query {
2 catCollection {
3 items {
4 name
5 linkedFrom {
6 friendlyUserCollection(order: [age_DESC, name_ASC]) {
7 items {
8 firstName
9 }
10 }
11 }
12 }
13 }
14}

Note: You cannot order the entryCollection field.

1query {
2 catCollection {
3 items {
4 name
5 linkedFrom {
6 friendlyUserCollection(order: [age_DESC, name_ASC]) {
7 items {
8 firstName
9 }
10 entryCollection {
11 __typename
12 }
13 }
14 }
15 }
16 }
17}

Limitations

It is not possible to order on fields of type Link, Text, Location, Object, or RichText.

Note: When using allowedLocales, the order argument that you have passed will not be applied to the fallback values, which might make the result seem to be unordered.