Product changes

Robert van Boesschoten

Sort and Filtering on nested relational properties of a base query

Another update regarding the Data API. This time focussing on nested fields.
In one of our recent releases, we released the following features:

  • Sorting on nested fields
  • Filtering on nested fields

For each subject we added a short explaining and a nice little example to see it 'in action'.

Sorting on nested fields


The sort method sorts the items within the selected field. This was already available on the root of the query, but now it’s possible to sort on a relational (nested) value as well! In order to sort a relation, you’ll need to specify the nested field using the field property. After specifying the field you’ll have to specify the order using the order property. This can be done by using ASC for ascending or DESC for descending.

Example:

query myQuery{

  allArtist{

    results{

      songs(sort: {field: title, order: ASC}){

        title

      }

    }

  }

}

 

Now returns:

{

  "allArtist": {

    "results": [

      {"songs": [

        {"title": "Beat It"},

        {"title": "Billie Jean"},

        {"title": "Thriller},

      ]}

    ]

  }

}

Filtering on nested fields

 

The filter method filters the items within the selected field. This was already available on the root of the query, but now it’s possible to filter on a relational (nested) value as well!  In order to filter a nested field, you’ll need to use the where method. Next you’ll need to select the property on which you want the nested field to be filtered, after which you’ll need to select the condition that has to be met.

Example:

query{

  allArtist{

    results{

      id

      songs(where: {id: {eq: 1}}){

        id

      }

    }

  }

}

 

Now returns:

{

  "data": {

    "allArtists": {

      "results": [

        {

          "id": 1,

          "songs": [

            {

              "id": 1

            }

          ]

        },

        {

          "id": 2,

          "songs": []

        }

      ]

    }

  }

}

For a full overview of our Data API documentation, please see the Data API section in our Online Academy.

Subscribe to product changes