Feature | Improve working with queries #477
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR provides a couple of improvements around working with queries:
Custom Query Builder
Defining a custom query builder works similar to defining a custom response. Providing your own query builder allows you to add methods that mirror the supported parameters of the API you're working with. It's also super helpful for autocompletion in your code editor.
Custom query builders are simply extending the
Saloon\Repositories\ArrayStore
that is already being used for queries. This ensures a non-breaking change.A simple example of a custom query builder:
How you'd register the query builder on a connector or request:
How you'd use the query builder:
Fluent Query Builder
A new
fluentQuery()
method makes working with queries a little smoother.From this:
To this:
I'd prefer to simplify things and merge the logic of the
fluentQuery()
method with thequery()
method. But that would require a method signature change and be a breaking change. I'll leave this decision up to you.Feature Request: Queries on resources
I'd love to make queries even more powerful by adding support for resources. This would be super powerful for building SDKs. Consider the following scenario, where you've got different resources that each need their own query builder class. Currently, you can only set the query builder on the connector, which sets the query builder for all resources. Or on each request directly, which can be a little tedious.
This is what I'd love to see:
Then you can use it like this:
I hope this all makes sense. Thanks for your work on Saloon!