flugg / laravel-responder

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

null as default resourceKey #115 #120

Closed cberio closed 6 years ago

cberio commented 6 years ago

hello handsome mustache! I made small PR for trying to fix #115,

seems returning null as resourceKey is looks fine.

Serializer Available ResourceKey
SuccessSerializer 'data'
ArraySerializer $resourceKey or nothing
NoopSerializer nothing

should I add more 'feature' test case for it?

flugg commented 6 years ago

Hello! The ResourceKeyResolver actually used to return null, but I changed it to a string for the sparse fieldset feature (the only-method). Basically, Fractal uses the resource keys for the root object and relationships to filter out allowed fields.

In cases where you're transforming a model or a collection of models we will automatically resolve a resource key from the model's table, so no issue there. However, when there are no results, we would have no way of resolving a resource key, and an error would be thrown complaining about a missing resource key: https://github.com/flugger/laravel-responder/blob/master/src/FractalTransformFactory.php#L72

I'm not really sure why the data would be added in the first place, there are no places in the NoopSerializer that the resource key is actually used.