Allow Resources to be Looked Up from Index By Type rather than assuming entityType--bundle format

Created on 12 July 2023, over 1 year ago
Updated 9 April 2024, 8 months ago

Problem/Motivation

The JSON:API Extras module allows paths for resources to be rewritten. For example, the default path of /jsonapi/node/article could be changed to /jsonapi/article. Currently methods like getCollection and getResource require an entityType--bundle to be passed in, which we then split and use to build the endpoint URL. I believe an endpoint like /jsonapi/article is essentially invisible to our client at the moment.

JSON:API Extras actually allows you to change both the resource name and the path, so you could even go as far as making the resource for blocks 'blocky' and the path '/jsonapi/my/block'

"blocky": {
  "href": "https://drupal-contributions.lndo.site/en/jsonapi/my/block"
},

Given this level of customization, we can't ensure that we can always derive the correct resource endpoint ourselves. We instead need to look up the endpoint at the root /jsonapi endpoint which provides an index of all endpoint links.

Steps to reproduce

* Enable jsonapi_extras
* Rewrite a resource name to either not follow the entityType--bundle convention, or a path to not follow the /entityType/bundle convention.
* Find that getCollection and getResource won't be able to find that endpoint.

Proposed resolution

* Introduce an indexLookup option to json-api-client options. If this is true, we'll make a request to the apiPrefix and lookup the resource URL by the provided type. If it is false or the option is not provided we'll use our existing assumptions about entityType--bundle format (this seems like the right default to me, because we know it will work with a default Drupal Core install).
* It should be possible to cache or otherwise persist the jsonapi index so we don't have to request it over and over.
* This change to lookup behavior should apply to all of our CRUD methods - getCollection/Resource but also deleteResource, createResource and updateResource.
* The `type` parameter for these methods will need to be updated because it currently uses the EntityTypeWithBundle type.

Remaining tasks

* Implement proposed changes.
* Update documentation.
* Add test coverage.

API changes

* Add new indexLookup option to json-api-client (I don't think this applies to the base class so it needs to be added only for json-api-client)
* Update the Type of the `type` parameter in CRUD methods.

📌 Task
Status

Fixed

Component

Code

Created by

🇺🇸United States brianperry

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024