Code Monkey home page Code Monkey logo

Comments (10)

juliomotol avatar juliomotol commented on May 18, 2024 1

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.

timacdonald avatar timacdonald commented on May 18, 2024 1

No problem.

I've had an idea on this that I will implement soon. Will give more flexibility in general.

from json-api.

snellingio avatar snellingio commented on May 18, 2024

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.

HollyIT avatar HollyIT commented on May 18, 2024

+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.

timacdonald avatar timacdonald commented on May 18, 2024

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.

HollyIT avatar HollyIT commented on May 18, 2024

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.

timacdonald avatar timacdonald commented on May 18, 2024

Passing a custom request is already possible.

return UserResource::make($user)->response($customRequest);

// or

return UserResource::make($user)->toResponse($customRequest);

from json-api.

HollyIT avatar HollyIT commented on May 18, 2024

@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.

timacdonald avatar timacdonald commented on May 18, 2024

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.

HollyIT avatar HollyIT commented on May 18, 2024

@timacdonald - just tried the custom request object and it works great. Thanks!

from json-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.