While working on #3104957: Fire an event to allow modules to react to user registration, I needed the top level JSON:API document to receive some metadata. Alas, I could not. After speaking with @gabesullice, we have an idea.
The DocumentExtractor service. This takes a request object and returns a JSON:API document. This extracts logic currently in the DocumentResolver argument resolver and moves it to a service that supports caching based on requests.
We currently do something like this in Commerce for resolving the current store per request with \SplObjectStorage.
https://git.drupalcode.org/project/commerce/blob/8.x-2.x/modules/store/s...
$request = $this->requestStack->getCurrentRequest();
if (!$this->stores->contains($request)) {
$this->stores[$request] = $this->chainResolver->resolve();
}
The DocumentExtractor can check if the request exists in cache and then return the extracted document, otherwise it can perform actions. This makes it possible for anyone to take a request and receive a JSON:API documentation! (for better or worse!)
Comment | File | Size | Author |
---|---|---|---|
#7 | interdiff-3104972-5-7.txt | 5.4 KB | mglaman |
#7 | 3104972-7.patch | 27.63 KB | mglaman |
#5 | 3104972-5.patch | 24.57 KB | mglaman |
#2 | 3104972-2.patch | 14.87 KB | mglaman |
Comments
Comment #2
mglaman🙈 WIP patch at EOD and I did not run tests locally. I'll pick this up tomorrow.
Comment #3
mglaman😱 they passed
Comment #4
mglamanThis will need a reroll after #3096378: DocumentResolver should only support DELETE requests with a body and #3096734: Handle relationships when creating resource objects from JSON:API request data
Comment #5
mglamanRerolled!
Comment #6
mglamanForgot newline, fix on commit.
Comment #7
mglamanThis adds the getDocumentFromRequest method to ResourceBase as an API method for retrieving the document in subclasses.
Comment #9
mglamanCrediting gabesullice for review and discussions.
Comment #11
mglaman