rupadana / filament-api-service

A simple api service for supporting filamentphp
https://filamentphp.com/plugins/rupadana-api-service
MIT License
140 stars 28 forks source link

[Feature]: Spatie Laravel-Query-Builder follow DTO via Spatie Laravel-data #55

Open eelco2k opened 5 months ago

eelco2k commented 5 months ago

What happened?

Allow handlers to conform to DTO. (these also need to be created/generated...)

$query = static::getEloquentQuery();
        $model = static::getModel();

        $query = QueryBuilder::for($query)
        ->allowedFields($model::$allowedFields ?? [])
        ->allowedSorts($model::$allowedSorts ?? [])
        ->allowedFilters($model::$allowedFilters ?? [])
        ->allowedIncludes($model::$allowedIncludes ?? null)
        ->paginate(request()->query('per_page'))
        ->appends(request()->query())
        ;

instead of:

return static::getApiTransformer()::collection($query)

use this:

return static::getApiTransformer()::collection(BlogData::collect($query));

where BlogData is a spatie laravel-data DTO.

The benefit would be that because the DTO knows column types etc. this information can be used to generate correct API Swagger docs for Schemas, Parameters etc.. see:

https://github.com/xolvionl/laravel-data-openapi-generator/tree/main/src/Data

How to reproduce the bug

nice to have

Package Version

latest

PHP Version

8.3

Laravel Version

11

Which operating systems does with happen with?

No response

Notes

No response

rupadana commented 5 months ago

i think this is good to have, we can use the DTO for request validation too.

eelco2k commented 4 months ago

i've created a simple version of the DTO implementation in this branch with PR: https://github.com/rupadana/filament-api-service/pull/57

But as i'm trying to get it to work i notice that it would be easier to generate the docs like xolvionl/laravel-data-openapi-generator does it.... It just directly generates a swagger json from the Routes(). so no annotations needed. When i have more time i think i will convert it like this as this would be easier to maintain instead of re-running the commands for generating a transformer or something...