rupadana / filament-api-service

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

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

Open eelco2k opened 3 weeks ago

eelco2k commented 3 weeks 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 2 weeks ago

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