Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The 'page_size' parameter should allow 0 values #6

Open
weierophinney opened this issue Dec 31, 2019 · 5 comments
Open

The 'page_size' parameter should allow 0 values #6

weierophinney opened this issue Dec 31, 2019 · 5 comments

Comments

@weierophinney
Copy link
Contributor

Hi, we use PhlyRestfully and now Apigility since several years ;-).

We often need to call a Web Service to count a number of entities. The HAL standard and zf-hal allow to get a count very easily using the total_items property of the fetched HAL collection.

For example...

GET https://myserver.com/comments?page=1&page_size=1
{
  "_links": { ... },
  "_embedded": {
    "comments": [ ... ]
  },
  "page_count": 45,
  "page_size": 1,
  "total_items": 45,
  "page": 1
}

Then on the client side its very simple (sample in JS / jQuery here).

$.get(url, function(payload) { console.log(payload.total_items); });

It works, but I think this is "sub-optimal" because the library forbid page_size values equal to 0 (see https://github.com/zfcampus/zf-hal/blob/7c164efe64365f343589cafd9307d91274140b55/src/Collection.php#L295).

In our case its has the following consequences

  • This forces an additional request in our database to always fetch at least one entity ;
  • It increases the size of the returned payload.

So, why the page_size parameter cannot be equal to 0 ? If their are good reasons what the best practices to count entities efficiently with REST / HAL ? If their are no good reasons could it be possible to authorize page_size 0 values ?

Thanks


Originally posted by @bgaillard at zfcampus/zf-hal#164

@weierophinney
Copy link
Contributor Author

To disable pagination, set -1 for page size.


Originally posted by @Wilt at zfcampus/zf-hal#164 (comment)

@weierophinney
Copy link
Contributor Author

@Wilt thanks for your response.

Ok, we can set -1, but i'm not sure, what's the meaning of "disabling pagination" ?

For me, as a developer, disabling pagination means do not take into account any page or page_size and returns all the results (which is obviously not wanted).

So if we suppose we have a userService->fetch($offset = 0, $pageSize = 12) Business Service function IMO enabling pagination implies this call.

$results = $this->userService->fetch($page - 1, $pageSize);

Disabling pagination should be expressed by this call.

$results = $this->userService->fetch($page - 1, PHP_INT_MAX);

So i'm not sure setting -1 is the same thing as asking to fetch absolutly no result with 0.

What I want to be able to do is that.

$results = $this->userService->fetch($page - 1, 0);

Also if we consider the client side call why would it be forbidden to simply call something that GET https://myserver.com/users?page=1&page_size=0 ?


Originally posted by @bgaillard at zfcampus/zf-hal#164 (comment)

@weierophinney
Copy link
Contributor Author

I get you; so you don't want any items from your collection, but you are only interested in count.

To achieve this you would like to set page_size to 0.

Sounds like an interesting idea, but not sure if this will work.
Did you even try to test what happens if you set 0 or NULL for page size when using a Paginator?
Will it return what you expect/want?

About your last question:

Also if we consider the client side call why would it be forbidden to simply call something that GET https://myserver.com/users?page=1&page_size=0 ?

I think because it didn't seem to make sense at first. But maybe you found a legitimate reason to allow this...


Originally posted by @Wilt at zfcampus/zf-hal#164 (comment)

@weierophinney
Copy link
Contributor Author

Have you tried rendering collections off?
https://github.com/zfcampus/zf-hal/blob/master/src/Plugin/Hal.php#L85


Originally posted by @TomHAnderson at zfcampus/zf-hal#164 (comment)

@weierophinney
Copy link
Contributor Author

Also, if you want a count just create an RPC to return just that value.


Originally posted by @TomHAnderson at zfcampus/zf-hal#164 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant