Query classes provide an object-orientated way to build queries and run them. They can also be used with a Query Manager to help run multiple queries.
Create a query by instantiating the Query class.
Example usage:
use Strata\Data\Query\Query;
// Setup query
$page = 2;
$query = (new Query())
->setUri('posts')
->addParam('page', $page)
->setCurrentPage($page)
->setTotalResults('[meta][total_results]')
->setRootPropertyPath('[data]')
;The above example does the following:
- Queries the URI:
/posts?page=$page - Sets a few fields to enable automated pagination
- Tells the query that data should be returned from the
dataelement
// Run directly via a query
$query->setDataProvider($api);
$posts = $query->getCollection();
// Or run via a query manager
$manager->add('posts', $query);
$posts = $query->getCollection('posts');This:
- Runs the query
- Returns a collection of results along with a pagination object
In the example above the total results data is set as [meta][total_results] which is a property path pointing to $data['meta']['total_results']
See property paths for more information on how to use these.
There are a number of base query classes you can use to construct queries. It's recommended to create your own child query classes where you can set up the query in your constructor. Base query classes do not set constructors to make creating your own query classes easier.
Intended for REST API queries. Requires a Strata\Data\Http\Rest data provider. Defaults to a GET query.
Intended for GraphQL queries. Requires a Strata\Data\Http\GraphQL data provider.
Has object-orientated methods to help build GraphQL queries. You can also set complex GraphQL queries from files. See GraphQL queries.
Intended for GraphQL mutation queries. Requires a Strata\Data\Http\GraphQL data provider.
By default, mutation queries are set to not run concurrently and do not cache.
A query has a number of methods to set it up. All setup methods return the Query object so you can use a fluent interface and chain methods together.
Each query must have a data provider and a URI in order to run.
Set the data provider to use with the query. This must be an object of the Strata\Data\Http\Rest class.
- Parameters
DataProviderInterface $dataProvider
You don't need to use this method if you are using the query manager, instead the query has a data provider assigned to it when you add the query to the query manager.
Set the URI to use for this query.
- Parameters
string $uriURI to run the query, relative to the data provider base URL
There are a lot of optional settings you can apply to a query.
Add a single parameter to send with this query. This is sent as a GET param with the request.
- Parameters
string $keyParam namemixed $valueParam value
Set array of parameters to send with this query. These are sent as GET parameters with the request.
- Parameters
array $params
Set options for the HTTP request for just this query.
- Parameters
array $options
For example to set headers:
$query->setOptions([
'headers' => [
'Content-Type' => 'text/plain'
]
]));To set an auth bearer token:
$query->setOptions([
'auth_bearer' => 'ABC123'
]));All queries default to running concurrently when used with a query manager, you can disable this by calling concurrent(false).
- Parameters
bool $concurrent = true
Mark the query as a sub-request, this suppresses errors in the HTTP request.
- Parameters
bool $subRequestDefaults to true
Set fields to return for this query. To use this method you must first set the field parameter.
- Parameters
array $fields
Example usage:
$query->setFields(['name', 'title', 'email']);Default functionality is to set the GET paramfields=name,title,email for the request.
The parameter to set to define fields to return with setFields. This defaults to "fields".
- Parameters
string $fieldParameter
Set the root property path to retrieve data from, e.g. '[data]' to return data from the array key 'data' in the response. If this is not called then data is read from the root element.
- Parameters
string $pathProperty path to root data element
Set pagination current page data.
- Parameters
string|int $currentPageProperty path in returned data to current page, or the actual value
Example usage:
// Set page to the current page variable
$query->setCurrentPage($page);
// Or set to a data field returned by the query
$query->setCurrentPage('[meta][current_page]');Set pagination results per page data.
- Parameters
string|int $resultsPerPageProperty path in returned data to results per page, or the actual value
Set pagination total results data.
- Parameters
string|int $totalResultsProperty path in returned data to total results, or the actual value
By default pagination data is read from the returned data. Some API responses set pagination data in the headers rather than the response data. Use this method to tell the query to retrieve pagination data from headers.
- Parameters
bool $paginationDataFromHeadersDefaults to true
When arrays are passed to params they are automatically converted into strings, separate by ','
You can alter the character used to separate multiple values with this method.
- Parameters
string $multipleValuesSeparator
By default, automatic caching for data requests happens if the data provider used by the query has caching enabled. This can also be controlled on the query level.
Also see more details on caching.
Manually set this query to cache. If no valid cache is set to the data provider this does nothing.
- Parameters
?int $lifetime = nullLifetime in seconds for the cache
Manually set this query to not cache.
Set the following cache tags when storing this query to the cache. These are added to any other cache tags already set.
- Parameters
array $tags = []
Query responses are lazy loaded for performance. If the cache is enabled, query responses are immediately fetched. If a live HTTP request is required then queries are executed when you access data.
You can also manually run a query via the run() method.
You can return a single item of data using get(). This automatically retrieves and decodes the query response and
returns data.
This normally returns an array, though the return type is not fixed so child query classes have flexibility on what data is returned.
Return a Collection object which contains an iterable collection of data and pagination information.
You can set the root property path to select collection data from in the response. If you don't set this in your query then the root data element is used as the collection (this will throw an exception if it is not iterable).
To build pagination you can set the current page, results per page and total results fields respectively.
$collection = $manager->getCollection('queryName');
$pagination = $collection->getPagination();You can find out if the query response was returned from the cache via $query->isHit() which returns a boolean.
This is a shortcut to the CacheableResponse::isHit method.
You can also get access to the response object itself via the getResponse() method. This returns a CacheableResponse
object. This is identical to a standard Symfony HttpClient response with the addition of
the isHit() method to indicate whether the response came from the cache or was requested live.
You can use standard HttpClient methods to convert data to an array:
$response = $query->getResponse();
$data = $query->getDataProvider()->toArray();Or you can grab the data provider for this query and use the decode method.
$response = $query->getResponse();
$data = $query->getDataProvider()->decode($response);By default, once a query is run it stores its response and does not re-run the live request again.
If you want to force the query to re-run, simply call the clearResponse() method:
$query->clearResponse();Then any subsequent calls on get(), getCollection() or getResponse() will re-run the live query. Please note if
caching is enabled for this query, then a cached response will still be returned if it exists.