Search code examples
apisymfonyapi-platform.com

API Platform Symfony : Only getting 30 results (pagination)


I'm using a Symfony API with an Angular client. When trying to get the data from a table Quotation I only get 30 results max when I want to get everything. I'm using Sellsy on the API. In the SellsyService.php I guess this function is called when I tried to get Quotations:

public function getQuotations()
    {
        $devisRequest = array(
            'method' => 'Document.getList',
            'params' => array(
                'doctype' => "estimate",
                'pagination' => array(
                    'nbperpage' => 4999,
                ),
                'search' => array(
                    //'steps' => ['sent','accepted']
                    'steps' => explode(
                        ",",
                        "draft,due,payinprogress,paid,late,sent,read,accepted,contested,expired,advanced,partialinvoiced,invoiced,stored,spent,partialspend,refused"
                    ),
                ),
            ),
        );
        $devislist = $this->sellsy->requestApi($devisRequest);

        return $devislist;
    }

I don't really know where to look, I didn't develop this API. When looking for "30" in the project, I can see this in cache:

    'api_platform.eager_loading.max_joins' => 30, 
    'api_platform.collection.pagination.items_per_page' => 30,

I don't know if it has something to do with my problem. Here's the config api_platform.yaml:

api_platform:
    swagger:
        api_keys:
            apiKey:
                name: Authorization
                type: header
    mapping:
        paths: ['%kernel.project_dir%/src/Entity']

Where should I like for this 30 limitation?


Solution

  • API Platform has pagination.

    As you guessed it, by default it's 30.

    API Platform Core has native support for paged collections. Pagination is enabled by default for all collections. Each collections contains 30 items per page.

    So you have multiple choice to solve your problem.

    1. Client side should use your pagination. So nothing to change in your backend but just query a page when looking at your quotations.

    Otherwise, you can update your configuration to disable pagination.

    From the documentation:

    Disabling the Pagination Globally

    # api/config/packages/api_platform.yaml
    api_platform:
        defaults:
            pagination_enabled: false
    

    Disabling the Pagination For a Specific Resource

    <?php
    // api/src/Entity/Quotation.php
    
    use ApiPlatform\Core\Annotation\ApiResource;
    
    /**
     * @ApiResource(attributes={"pagination_enabled"=false})
     */
    class Quotation
    {
        // ...
    }
    

    You could also let the client decide.

    Or if you don't want to disable it, but increase the number of item per page:

    Changing the Number of Items per Page Globally

    # api/config/packages/api_platform.yaml
    api_platform:
        defaults:
            pagination_items_per_page: 100 # Default value
    

    Changing the Number of Items per Page For a Specific Resource

    <?php
    // api/src/Entity/Quotation.php
    
    use ApiPlatform\Core\Annotation\ApiResource;
    
    /**
     * @ApiResource(attributes={"pagination_items_per_page"=100})
     */
    class Quotation
    {
        // ...
    }
    

    There are other things you can do such as partial pagination or changing the number of items per page client-side, but I advise you to just look into the documentation.