Giter Club home page Giter Club logo

Comments (3)

ThomasNoergaard avatar ThomasNoergaard commented on August 21, 2024

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 :)

from build-an-api-with-laravel.

Lulu2021-ux avatar Lulu2021-ux commented on August 21, 2024

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

from build-an-api-with-laravel.

ThomasNoergaard avatar ThomasNoergaard commented on August 21, 2024

You're welcome @Lulu2021-ux 🙂👍

from build-an-api-with-laravel.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.