dingo / api

A RESTful API package for the Laravel and Lumen frameworks.
BSD 3-Clause "New" or "Revised" License
9.32k stars 1.25k forks source link

[Proposal] Relationship Eager Loading for Fractal Includes #1705

Closed xwiz closed 4 years ago

xwiz commented 4 years ago
Q A
Bug? no
New Feature? yes
Framework Laravel
Framework version 6.x
Package version 2.4.x
PHP version 7.3

Actual Behaviour

Currently, Dingo tries to eagerload model based on the include keys in transformer, and uses the value as the resource key.

Expected Behaviour

It should be the other way round. The array key should be the resource key and the value should be used as the relationship name. This way, it won't affect the resource keys in the include query string.

Explanation

If would like to have something like this: public $availableIncludes = ['billing_type' => 'billingType'];

instead of: public $availableIncludes = ['billingType' => 'billing_type'];

in order to use a query string like myapiurl?include=billing_type

Comments

Is there a reason why it is implemented in the current way? Because I assume this is how it should have been thought about in the first place.

Note: After further testing, it seems this happens only with defaultIncludes? I'm confused, has this been implemented or is this manifestation part of fractal?

xwiz commented 4 years ago

I have resolved this by using a CustomSerializer. It seems the issue is from Fractal.

specialtactics commented 4 years ago

If you have fixed it, I encourage you to do a PR into the fractal serializer project for the benefit of other users.