Allow to include a total count of items to collection responses

Created on 5 January 2020, almost 5 years ago
Updated 19 February 2023, almost 2 years ago

The #2942426: [DISCUSSION] Remove query counting and collection counts? explains that without a JSON:API Extras it's *almost* impossible to get a total number of items for a collection request within meta.count. This caused by the "static" code of Drupal\jsonapi\ResourceType\ResourceType::includeCount():

public function includeCount() {
  // By default, do not return counts in collection queries.
  return FALSE;
}

Proposed resolution

  1. Add string|null $totalCountMetaName to the Drupal\jsonapi\ResourceType\ResourceType.
  2. Add string|null $total_count_meta_name = NULL as the argument to Drupal\jsonapi\ResourceType\ResourceType::__construct() and assign it to $this->totalCountMetaName.
  3. Add logic to Drupal\jsonapi\ResourceType\ResourceType::includeCount() and return $this->totalCountMetaName !== NULL, so the total count is included for collection responses if the meta name is provided.
  4. Introduce Drupal\jsonapi\ResourceType\ResourceType::getTotalCountMetaName(): ?string that returns $this->totalCountMetaName or 'count' if the Drupal\jsonapi\ResourceType\ResourceType::includeCount() method is overridden.
  5. In the Drupal\jsonapi\Controller\EntityResource::respondWithCollection() use $meta[$resource_type->getTotalCountMetaName()] instead of $meta['count'].
  6. In the Drupal\jsonapi\ResourceType\ResourceTypeBuildEvent, introduce string|null $totalCountMetaName, setTotalCountMetaName(?string $name): void, and getTotalCountMetaName(): ?string that will allow changing the resource definition at a build time.
  7. In the Drupal\jsonapi\ResourceType\ResourceTypeRepository::createResourceType(), query $event->getTotalCountMetaName() for non-internal resource types and pass the value to new ResourceType.
Feature request
Status

Needs work

Version

10.1

Component
JSON API 

Last updated 1 day ago

Created by

🇺🇦Ukraine BR0kEN Dnipro

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024