craftcms / element-api

Create a JSON API/Feed for your elements in Craft.
MIT License
498 stars 56 forks source link

'paginate' setting overrides 'limit' query param #119

Closed modelesque closed 2 years ago

modelesque commented 4 years ago

Let's say I have hundreds of entries in an articles channel. Consider this endpoint:

'api/articles.json' => function() {
    return [
        'elementType' => Entry::class,
        'criteria' => [
            'section' => 'articles',
            'limit' => 3,
        ],
        'transformer' => function(Entry $entry) {
            return [
                'title' => $entry->title,
                'slug' => $entry->slug,
                'id' => $entry->id,
            ];
        },
    ];
}

My 'limit' => 3 gets ignored because paginate is set to true by default, and therefore the query will ignore this and get all entries.

Currently I'm working on a huge export of entries to import via Feed Me somewhere else, and I want to run some tests but not with all 900+ entries I have in that channel. I just want to test with 3. I finally figured out that my endpoint was still querying for every entry because it was set to paginate the entries, and I guess the logic assumes you wouldn't want to paginate and limit at the same time. If paginate is true, then I think it would be better to paginate only if the elements returned exceed whatever elementsPerPage is set to.

So for example, if this is what's happening:

'paginate' => true,
'elementsPerPage' => 10,

And you're querying with a limit of 12, you would have two pages. If you're querying with a limit of 9, then you would just have one page.

There's no mention of this in the docs, so it feels like an unfortunate gotcha that requires you to remember to set 'paginate' => false anytime you want to limit your query.