Problem/Motivation

The idea came during the implementation of #3013596: Generating resource links (`self` + `related`) is too slow — noticeable when generating 140K of them. The goal is to have the option to skip links generation altogether for a particular entity. The new API will be leveraged by jsonapi_extras to expose a UI for it.

Proposed resolution

Move all the link generation code to a new JSON:API service class (with an interface).
Inject that service anywhere where we are generating links and use it instead of the current code paths.
Add tests for the newly added service (maybe).

Having this service in place will allow 3rd party modules to decorate it and have full controll over the link generation done in the JSON:API module.

Through a NULL-pattern implementation of the particular interface we can decide to have no links generated. This will be follow-up in JSON:API Extras module.

Remaining tasks

PoC, Patch, etc.

User interface changes

None.

API changes

API addition - new service.

Data model changes

None.

Comments

ndobromirov created an issue. See original summary.

gabesullice’s picture

Title: Expose API to allow links handling for entities from other module.s » Expose API to allow links handling for entities from other modules
Issue tags: +API-First Initiative
Parent issue: #3013596: Generating resource links (`self` + `related`) is too slow — noticeable when generating 140K of them » #2994193: [META] The Last Link: A Hypermedia Story
Related issues: +#2995960: Add a Link and LinkCollection class to support RFC8288 web linking.

I personally would like to see this feature land so that links can be customized. I think @e0ipso would like it to land for limiting response size and increase performance. I think @Wim Leers does not yet have an opinion.

I've started working on this already, but first #2995960: Add a Link and LinkCollection class to support RFC8288 web linking. must land.

https://www.drupal.org/project/jsonapi_hypermedia is a module where I'll be experimenting with this outside of JSON:API proper.

wim leers’s picture

Status: Active » Postponed (maintainer needs more info)

@ndobromirov: Can you explain how you'd envision this API working? Note that the JSON:API module does not offer any public PHP APIs. JSON:API Extras uses private APIs. That will need to continue to be the case.

@gabesullice: I was under the impression this would be very different from your work in #2995960: Add a Link and LinkCollection class to support RFC8288 web linking. and #2994193: [META] The Last Link: A Hypermedia Story, but I probably had the wrong impression.

ndobromirov’s picture

This was @eoipso's idea from the issue in the description, I've just created this one, so it's not forgotten.

No concrete idea for the new API at the moment. If we are not exposing an API, then links generation could go into a service (of sort) that others can decorate and thus change behavior from outside.

ndobromirov’s picture

Status: Postponed (maintainer needs more info) » Active
wim leers’s picture

If we are not exposing an API, then links generation could go into a service (of sort) that others can decorate and thus change behavior from outside.

This seems preferable, because that avoids introducing a concrete API.

wim leers’s picture

Title: Expose API to allow links handling for entities from other modules » [PP-1] Expose API to allow links handling for entities from other modules
Status: Active » Postponed
wim leers’s picture

Title: [PP-1] Expose API to allow links handling for entities from other modules » Expose API to allow links handling for entities from other modules
Status: Postponed » Active
wim leers’s picture

Project: JSON:API » Drupal core
Version: 8.x-2.x-dev » 8.8.x-dev
Component: Code » jsonapi.module
ndobromirov’s picture

Issue summary: View changes

Updated IS with an approved implementation direction.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

gabesullice’s picture

Status: Active » Closed (won't fix)

The JSON:API Hypermedia module now exists to add custom links. I believe a feature request could be made to that module to create an API for removing links, which would obsolete this issue.

This issue will not be addressed in Drupal core until the bulk of JSON:API Hypermedia's API is integrated into Drupal core itself.

ndobromirov’s picture

I've researched the JSON:API Hypermedia module.
The issue there is that it can manage the list of links but only during normalization phase.
My aim is to prevent the any link generation at all, so the normalizer gets an empty LinkCollection instance.

Currently this happens in JSON:API module:
Drupal\jsonapi\JsonApiResource\ResourceObject::buildLinksFromEntity()

I've added another hacky proposal for implementation from JSON:API Extras in here:
#3039331: API to enable / disable links property per entity resource type.