diff --git a/src/Adapter/AdapterInterface.php b/src/Adapter/AdapterInterface.php index d491b17..f08b0d1 100644 --- a/src/Adapter/AdapterInterface.php +++ b/src/Adapter/AdapterInterface.php @@ -57,6 +57,13 @@ interface AdapterInterface */ public function filterByExpression($query, string $expression): void; + /** + * Manipulate the query to only include specific fields. + * + * @param string|array $fields Comma-separated list of field names to include or array of such lists for every resource type + */ + public function sparseFieldset($query, $fields): void; + /** * Manipulate the query to sort by the given attribute in the given direction. */ diff --git a/src/Adapter/EloquentAdapter.php b/src/Adapter/EloquentAdapter.php index 7817914..49d0618 100644 --- a/src/Adapter/EloquentAdapter.php +++ b/src/Adapter/EloquentAdapter.php @@ -65,6 +65,10 @@ class EloquentAdapter implements AdapterInterface { } + public function sparseFieldset($query, $fields): void + { + } + public function sortByAttribute($query, Attribute $attribute, string $direction): void { $query->orderBy($this->getAttributeProperty($attribute), $direction); diff --git a/src/Adapter/NullAdapter.php b/src/Adapter/NullAdapter.php index 499cc7d..7b7b88d 100644 --- a/src/Adapter/NullAdapter.php +++ b/src/Adapter/NullAdapter.php @@ -31,6 +31,10 @@ class NullAdapter implements AdapterInterface { } + public function sparseFieldset($query, $fields): void + { + } + public function sortByAttribute($query, Attribute $attribute, string $direction): void { } diff --git a/src/Endpoint/Index.php b/src/Endpoint/Index.php index 8f41e63..e6cd375 100644 --- a/src/Endpoint/Index.php +++ b/src/Endpoint/Index.php @@ -65,6 +65,10 @@ class Index $resourceType->applyFilters($query, $filter, $context); } + if ($fields = $context->getRequest()->getQueryParams()['fields'] ?? null) { + $resourceType->applySparseFieldset($query, $fields, $context); + } + run_callbacks($schema->getListeners('listing'), [$query, $context]); $total = $schema->isCountable() ? $adapter->count($query) : null; diff --git a/src/ResourceType.php b/src/ResourceType.php index 6ddc8f5..3b35a7c 100644 --- a/src/ResourceType.php +++ b/src/ResourceType.php @@ -169,6 +169,14 @@ final class ResourceType } } + /** + * Apply the resource type's sparse fieldsets to a query. + */ + public function applySparseFieldset($query, $fields, Context $context): void + { + $this->adapter->sparseFieldset($query, $fields); + } + private function filterByAttribute($query, Attribute $attribute, $value): void { if (preg_match('/(.+)\.\.(.+)/', $value, $matches)) {