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
Comment #2
ndobromirov commentedComment #3
gabesulliceI 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.
Comment #4
wim leers@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.
Comment #5
ndobromirov commentedThis 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.
Comment #6
ndobromirov commentedComment #7
wim leersThis seems preferable, because that avoids introducing a concrete API.
Comment #8
wim leersPostponed on #2995960: Add a Link and LinkCollection class to support RFC8288 web linking. per #3.
Comment #9
wim leers#2995960: Add a Link and LinkCollection class to support RFC8288 web linking. landed!
Comment #10
wim leersComment #11
ndobromirov commentedUpdated IS with an approved implementation direction.
Comment #13
gabesulliceThe 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.
Comment #14
ndobromirov commentedI'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.