flugg / laravel-responder

A Laravel Fractal package for building API responses, giving you the power of Fractal with Laravel's elegancy.
MIT License
859 stars 86 forks source link

Polymorphic relation mapping #111

Open IlCallo opened 6 years ago

IlCallo commented 6 years ago

Derived by #84 discussion. The idea is to allow eager loading (via the mapping implemented in 3.0 on both $relations and $load) also on morphable fields.

The syntax would be something like

$relation = [
  'morphableRelation' => [
    PizzaTransformer::class,
    MafiaTransformer::class,
    MandolinoTransformer::class,
  ]
]

I'm not sure about how it's possible to implement this without knowing the underlying data or having access to the models, but I think that using a full join maybe can be useful to retrieve all different relations of various morphed records.

Or we can allow a more complex definition and then fallback to meaningful defaults as Laravel does

$relation = [
  'morphableRelation' => [
    'pizzas' => PizzaTransformer::class,
    'mafias' => MafiaTransformer::class,
    'mandolins' => MandolinoTransformer::class,
  ]
]

In this way it's possible to add constraints on the eager loading query and load PizzaTransformer relations only when morphable_type is equal to "pizzas" (I guess the relation contains the field name of the related morphable and can be retrieved there) and same goes for the other entries. If indexes are not given (so like the first example), the transformer class itself is used to check against morphable_type (because Laravel by default uses the fully qualified names in this cases), unless a morphable map is defined (check here) and contains a mapping for the Model associated to the transformer, which can usually be retrieved by the Transformer name (App\Transformers\PizzaTransformer => Pizza).

Do you think this can be done?

flugg commented 6 years ago

I'll tinker some with polymorphic relations when I have time and see how possible something like this would be to implement.

IlCallo commented 6 years ago

Thank you :)

Hesesses commented 6 years ago

+1 :)

Hesesses commented 5 years ago

any ideas / updates on this?

flugg commented 5 years ago

Not yet, unfortunately :(

Have had limited time to work on open source the last months, but should have some time to work on this and other package-related stuff over Christmas!

Hesesses commented 5 years ago

Sorry to be pain in the ass, but any updates on this? πŸ™

flugg commented 5 years ago

You’re not a pain in the ass πŸ˜„ just been busy with work lately, but will make sure to put this on the top of the priority list!

ld-gary commented 5 years ago

Hey, gentle bump on this feature, how's it coming along?

Hesesses commented 4 years ago

πŸ˜‡ πŸ™

flugg commented 4 years ago

Sorry for this taking a long time - I'm almost done with a new major version of the package which swaps out Fractal for API resources. I'll ensure the new version has a good solution for polymorphic relations!

Hesesses commented 4 years ago

Any updates with the new major version? πŸ™

flugg commented 4 years ago

It's technically finished, just missing some tests and documentation!

The more I've worked on it the more I've realised I want to keep the feature set at a minimum. Currently I've replaced Fractal's Serializers with a new Formatter concept. However, since Transformers are replaced with Laravel's API Resources I've removed things like with and only since they would require changes to the resource classes themselves which I feel goes outside the scope of the package. That means this issue is no longer part of the responsibilities of this package. Instead I'm working on a new package which handles requests and can do filtering, inclusion of relations and sorting.

I've held back a bit on the major version because I didn't really have a good replacement for the features which are getting removed. With that being said I feel like the new version is a big improvement from the current version and even though it does a bit less things than before it does these things better.

I can push the new version up to a new branch if you (or anybody else) want to check it out and provide feedback?

Hesesses commented 4 years ago

Would be nice to be able test if and provide some feedback also!

For me (and also our projects) the ?with=xxx feature has been one of the most used features

flugg commented 4 years ago

Yeah, I feel you and I've used this feature a lot myself. Basically, I would like to have possibilities to not only include relations and filter fields with query parameters, but also query a list of data, sort the data and more. If I were to include all of these features to this package I feel the scope would be very broad and the documentation a bit too heavy. And if I were to only keep the inclusion of relations and filtering fields for this package then there would be two packages concerned about parsing the query string which I would like to avoid.

Either ways, I'll think some more about it - I might re-add these features until the new package is released. The new version is released on a new branch now, would love your feedback: https://github.com/flugger/laravel-responder/tree/feature/api-resources (keep in mind the documentation is not done).

Firtzberg commented 4 years ago

I have managed to create a polymorphic fractal transformer. Have a look at https://github.com/yucadoo/polymorphic-fractal