In this episode, we focus on a practical use case where you might want to access and pass a query builder directly into other classes. Specifically, we build up a filtering system that works off the query string parameters, allowing you to flexibly filter a list of posts.
We start by setting up the basics for a filtering structure, imagining a scenario where you want to filter posts (for example, by whether they're published or not, or by date). To do this cleanly, we create a base filter, and then a concrete PublishedFilter
class. These filters accept both the incoming request (so we can access query parameters) and the builder (so we can mutate the query).
You'll see how to set up the filters so that they can be chained together—each filter checks if a relevant query parameter exists, applies its logic if so, then returns the builder for the next filter to use. This approach makes it easy to extend functionality by simply adding new filter classes.
We go step-by-step through writing this filtering mechanism, then demonstrate it in action with a quick test/filter for published status, showing the power and flexibility of the pattern. You’ll easily be able to add more filters after this episode to keep your query logic neat and composable.
By the end, you'll understand not just why you might want to pass a builder around, but also how to put this into practice in your own projects for much cleaner and more modular code.