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.

Issue fork api_client-3374190

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

brianperry created an issue. See original summary.

brianperry’s picture

Status: Active » Postponed

Marking this as postponed as we focus on the vertical slice POC.

brianperry’s picture

Status: Postponed » Active

Also need to support resources that don't follow entity--bundle structure.

brianperry’s picture

Assigned: Unassigned » brianperry
brianperry’s picture

Assigned: brianperry » Unassigned
pratik_kamble’s picture

@brianperry I did not understand what is the meaning of "Skip Index Lookup For Default Resource Types". Can you help understand it with example.

brianperry’s picture

Title: Skip Index Lookup For Default Resource Types » Allow Resources to be Looked Up from Index By Type rather than assuming entityType--bundle format
Issue summary: View changes
brianperry’s picture

@pratik_kamble added details in the issue description. Let me know if it inspires any additional questions.

pratik_kamble’s picture

Assigned: Unassigned » pratik_kamble
brianperry’s picture

Created a branch to start chipping away at this.

brianperry’s picture

Assigned: pratik_kamble » brianperry

brianperry’s picture

Assigned: brianperry » Unassigned
Status: Active » Needs review
coby.sher’s picture

Status: Needs review » Reviewed & tested by the community
brianperry’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.