-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
include
without query string
#15
Comments
I should note that Laravel as far as I can tell doesn't allow for modifying the query string, unlike |
+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.
|
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. |
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
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. |
Passing a custom request is already possible. return UserResource::make($user)->response($customRequest);
// or
return UserResource::make($user)->toResponse($customRequest); |
@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! |
No worries. I'm gonna leave this ticket open, just to see what the vibe is on this idea from more people. |
Before, when we used
This could also resolve #23 as we could just say that a Node will default include its children. |
@timacdonald - just tried the custom request object and it works great. Thanks! |
No problem. I've had an idea on this that I will implement soon. Will give more flexibility in general. |
Currently, there is not a way to include relationships without explicitly asking for them. While this makes sense in almost all cases, I have a unique case where on a specific resource I want to include some morphTo's in the include by default.
Where includes happen:
json-api/src/Support/Includes.php
Line 38 in c3f89ee
Options could be adding a default to the
$request->query('include')
, which would work for my use case, but probably wouldn't be flexible enough for most people where they would want to ALWAYS have a default include even when passing in more.I'll think a little bit about it, but I imagine that @timacdonald will have a better / specific implementation that he wants. If you can describe the best way to do it, I could take a shot at it.
The text was updated successfully, but these errors were encountered: