WackyStudio / build-an-api-with-laravel

Official Build an API with Laravel repository
122 stars 56 forks source link

Configuration files are not serializable #52

Closed Lulu2021-ux closed 3 years ago

Lulu2021-ux commented 3 years ago

Hi guys,

I've got an issue when trying to php artisan config:cache :

image

The issue is coming from jsonapi.php 'allowedFilters' => [Spatie\QueryBuilder\AllowedFilter::exact('roles', 'roles.name')]

Here is the content of the config file that does not work :

image

I think it's because there is a closure? Would you have an advice to bypass that?

P.S : I am using Laravel 6.0 et spatie querybuilder 2.3

Nice book btw :)

ThomasNoergaard commented 3 years ago

Hi @Lulu2021-ux

Thank you, we're glad you're enjoying the book :)

Yes you're right, it is because the call to the AllowedFilter method cannot be serialized.

The reason why we are calling this in the config file, is just for convenience, so that we can easily call different types of allowed filters, such as partial and scope filters, which Spaties QueryBuilder also supports.

To fix this issue, you could move these calls into the JSONApiService class and create a method for handling filters. But before you do this, you'll have to find a way to tell the filter name and filter type in the config file. A way to do this could be like this, where the key is the filter name and the value is the filter type:

     'users' => [
            //...
            'allowedFilters' => [
                'role' => 'exact',
            ],
            //...
      ],

Then in the JSONApiService we'll add a handleFilters method, with a parameter to get the type of the resource.

    protected function handleFilters(string $type)
    {
        return collect(config("jsonapi.resources.{$type}.allowedFilters"))
            ->map(function ($filterType, $filterName) {

                switch ($filterType) {
                    case 'exact':
                        return AllowedFilter::exact($filterName);
                    case 'partial':
                        return AllowedFilter::partial($filterName);
                    case 'scope':
                        return AllowedFilter::scope($filterName);
                }
            })
            ->toArray();
    }

In this method we will get the allowed filters from the config file, for the given type and just to make it easy for us to map the key and value into the correct AllowedFilter class, we will be using a collection. When mapping the filter type and name, we can use a simple switch statement to ensure that the types are mapped correctly and that the filter name will be given. The allowedFilters method in the QueryBuilder takes an array of filters, which means we will have to convert the collection to an array in the end.

In the fetchResources method you can then simple call the handleFilters method like this:

    public function fetchResources(string $modelClass, string $type)
    {
        $models = QueryBuilder::for($modelClass)
            ->allowedSorts(config("jsonapi.resources.{$type}.allowedSorts"))
            ->allowedIncludes(config("jsonapi.resources.{$type}.allowedIncludes"))
            ->allowedFilters($this->handleFilters($type))
            ->jsonPaginate();
        return new JSONAPICollection($models);
    }

And afterwards the config file should be cacheable again.

Don't forget to run the it_can_filter_administrators_by_role and it_can_filter_users_by_role in the Features\UserTest testclass, so that you ensure that the refactoring doesn't break anything.

Hope this helps :)

Lulu2021-ux commented 3 years ago

Thank you a lot for your answer, it really helps 👍 :)

ThomasNoergaard commented 3 years ago

You're welcome @Lulu2021-ux 🙂👍