18 Scaling collection endpoints with filters and pagination

 

This chapter covers

  • Designing filters, pagination, and sorting for APIs
  • Enhancing the PetSitter OpenAPI definition with these features

As the PetSitter application grows, there will eventually be a lot of jobs posted in the system at the same time. It means that pet sitters will have a hard time going through all the job postings to find those they are interested in. Also, the API response for listing the jobs can get quite large. The PetSitter team realized this during their sprint planning in chapter 16. At that time, they decided to implement filters and pagination to solve the issue. While discussing these we’re also adding a third, related topic: sorting.

Before we start looking at API design, though, let’s make sure we’re on the same page regarding the meanings of the terms filters, pagination, and sorting:

18.1 The problem

18.2 Designing filters

18.2.1 Projection filters

18.2.2 Selection filters

18.2.3 Handling nested schemas

18.2.4 Query languages

18.2.5 Special conventions

18.3 Filters for PetSitter

18.3.1 Finding filter fields

18.3.2 Adding filters to OpenAPI

18.3.3 Making a request

18.4 Designing pagination

18.4.1 Offset-based and page-based pagination

18.4.2 Cursor-based pagination

18.5 Pagination for PetSitter

18.5.1 Adding pagination to OpenAPI

18.5.2 Extending our request example

18.6 Designing sorting

18.6.1 Single-field sorting

18.6.2 Multi-field sorting

18.6.3 Consistency throughout parameter types

18.7 Sorting for PetSitter

18.7.1 Finding sorting fields