0

I'm trying to make a custom JSON:API resource for flaggings using the JSON:API Resources module.

Right now, I simply want to return all the flaggings of a given bundle with an EntityQuery. (Later, I will customize the EntityQuery, which is why I am need to go beyond what JSON:API provides by default.)

My code is based on the module's example code.

MYMODULE/src/Resource/MyQueue.php

/**
 * Processes a request for a collection containing a user's article nodes.
 *
 * @internal
 */
class MyQueue extends EntityQueryResourceBase {

  /**
   * Process the resource request.
   *
   * Code example:
   * https://git.drupalcode.org/project/jsonapi_resources/tree/8.x-1.x/tests/modules/jsonapi_resources_test/src/Resource.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The request.
   *
   * @return \Drupal\jsonapi\ResourceResponse
   *   The response.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function process(Request $request): ResourceResponse {

    $cacheability = new CacheableMetadata();

    $entity_type_id = 'flagging';
    $entity_type_bundle = 'MY_FLAGGING_TYPE';
    $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
    $entity_query = $this->getEntityQuery($entity_type_id)
      ->condition($entity_type->getKey('bundle'), $entity_type_bundle);

    $paginator = $this->getPaginatorForRequest($request);
    $paginator->applyToQuery($entity_query, $cacheability);

    $data = $this->loadResourceObjectDataFromEntityQuery($entity_query, $cacheability);

    $pagination_links = $paginator->getPaginationLinks($entity_query, $cacheability, TRUE);

    $response = $this->createJsonapiResponse($data, $request, 200, [], $pagination_links);
    $response->addCacheableDependency($cacheability);

    return $response;
  }

}

With this code, I'm getting the following error:

LogicException: This resource attempted to serve data that contains unsupported resource types.

I don't understand why I'm getting this error, or what it means.

  • If I set $entity_type_id to node and $entity_type_bundle to page, everything works as expected.
  • If I set the $entity_type_id to 'custom_content_type`, one of the content types I configured in the admin UI, it doesn't work; I get the same error.
  • I tried to set $entity_type_bundle to all three flagging bundles on my site; none of them work.
  • The $entity_query is correct for the flaggings. I can do $entity_query->count->execute() and I get the correct count.
  • I am logged in as an administrator with all permissions.
  • I can access the jsonapi in my browser as an admin at /jsonapi/flagging/MY_FLAGGING_TYPE and I see the flaggings as I expect.

1 Answer 1

0

This error is the result of not setting the _jsonapi_resource correctly in the routing file:

MYMODULE.routing.yml

# Defines a route where a user's flaggings can be found.
MYMODULE.user_flaggings:
  # %jsonapi% is a placeholder for the JSON:API base path, which can be
  # configured in a site's services.yml file.
  path: '/%jsonapi%/MYMODULE/queue'
  defaults:
    # Every JSON:API resource route must declare a _jsonapi_resource. The
    # value can either be a class or a service ID. Unlike the _controller
    # route default, it is not possible to declare a method name to be called.
    _jsonapi_resource: Drupal\MYMODULE\Resource\MyQueue
    _jsonapi_resource_types: ['node--page', 'flagging--MY_FLAGGING_TYPE']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.