Comments (10)
Before, when we used league/fractal
(through spatie/laravel-fractal
), they have a feature where you could set the Default Includes for a resource. This plays well with the spec as it also mentions:
An endpoint MAY return resources related to the primary data by default.
This could also resolve #23 as we could just say that a Node will default include its children.
from json-api.
No problem.
I've had an idea on this that I will implement soon. Will give more flexibility in general.
from json-api.
I should note that Laravel as far as I can tell doesn't allow for modifying the query string, unlike $request->merge([])
. That's also an easy way to do this. We could just look in the $request->input
for merged query variables.
from json-api.
+1 on this idea. I like using JsonAPI resources for things like Inertia apps as well, just to keep the number of needed resource classes to a minimum. The work around I've implements is to just extends the base class and add in a method to force some includes, but it would be nice to have this as a core feature.
class AppResource extends JsonApiResource
{
protected array $forcedIncludes = [];
public function withIncludes($includes): static {
$this->forcedIncludes = collect(explode(',', $includes))->filter()->unique()->toArray();
return $this;
}
public function toResponse($request): JsonResponse
{
$includes = '';
if ($request->include) {
$includes = collect(array_merge(explode(',', $request->include), $this->forcedIncludes))
->filter()
->unique()
->join(',');
}
return parent::toResponse($request->merge(['include'=> $includes]));
}
}
from json-api.
Although I'm not 100% against this, my main concern is that is against the spec and leads to weird results for users.
In my eyes the beauty of not including anything by default is that the client gets full control and responses are deterministic. I don't any unexpected data.
Would it not be possible to the client to just include the relationships in the query parameter? What makes these relationships special? Just trying to understand better.
from json-api.
Take an Inertia app as an example. Instead of having to create generic Laravel JSON Resources for the App, then JSON-API resources for the API endpoints, you could utilize a single JSON API resource for both.
This is kind of a gray area of the spec:
https://jsonapi.org/format/#fetching-includes
If an endpoint supports the include parameter and a client supplies it:
The server MUST NOT include unrequested resource objects in the included section of the compound document.
Since I'm talking about an app endpoint (Inertia in this case), which puts the actual response inside the HTML body (as an attribute), it technically wouldn't be an API endpoint, so it wouldn't actually violate this.
Honestly, a better solution would be the ability to make send a custom request to the Resource class. Not sure if that's entirely possible given the underlying usage of Laravel JSON Resources.
from json-api.
Passing a custom request is already possible.
return UserResource::make($user)->response($customRequest);
// or
return UserResource::make($user)->toResponse($customRequest);
from json-api.
@timacdonald It seems like I tried that before and couldn't get it to work, but if it's handling it, then that's a perfect solution. Thanks!
from json-api.
No worries. I'm gonna leave this ticket open, just to see what the vibe is on this idea from more people.
from json-api.
@timacdonald - just tried the custom request object and it works great. Thanks!
from json-api.
Related Issues (20)
- Would you consider adding links to relationships in milestone for v1.x HOT 2
- To Many relationship wrong wrapped? HOT 7
- [Question] How to include tree relationships? HOT 7
- Getting empty results in browser
- Using with non Model resources. HOT 1
- Laravel 10 support HOT 1
- Laravel 10 support HOT 2
- Default include relationship HOT 1
- JsonApiServerImplementation 'final'`, doesn't allow custom implementations HOT 2
- Relationship attributes are not displayed in the response HOT 2
- Do this for James
- Lumen support? HOT 1
- Relationships not shown while having data HOT 5
- How to get from JsonResource to JsonApiResource HOT 1
- Type is plural HOT 1
- Relationship data is not shown as expected HOT 6
- Is the response following v1.0 or ? HOT 1
- laravel 10
- i now want some vegemite HOT 2
- Clarification for how to use with many to many
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json-api.