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

Fix hasNextPage - revert to count. Fix after #986

Merged
merged 7 commits into from Jun 25, 2020

Conversation

pcraciunoiu
Copy link
Contributor

@pcraciunoiu pcraciunoiu commented Jun 9, 2020

This is a follow up fix from #965.

Unfortunately, that broke the logic for has_next_page (which is still... confusing anyways).

This fixes it to perform a count again, since we need the total length to decide if there is a next page. Except for the new test and the changes in fields.py, the other test changes are a revert, essentially.

This should make it possible to do infinite scroll style pagination on DjangoConnectionFields now!

Comment on lines +1143 to +1145
# Need first: 4 here to trigger the `has_next_page` logic in graphql-relay
# See `arrayconnection.py::connection_from_list_slice`:
# has_next_page=isinstance(first, int) and end_offset < upper_bound
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this is a bug in graphql-relay. Without first param, you get has_next_page=False even if there are more elements... Sigh.

@pcraciunoiu pcraciunoiu changed the title Fix hasNextPage - revert to count Fix hasNextPage - revert to count. Fix after Jun 9, 2020
Comment on lines 143 to 144
after = get_offset_with_default(args.get("after"), -1) + 1
list_slice_length += after
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this, graphql-relay v2 slices an empty list here:

https://github.com/graphql-python/graphql-relay-py/blob/54fa42716e587c21e1fd8e6fc7d39cb32db28039/graphql_relay/connection/arrayconnection.py#L79-L82

Might be fixed in 3.0, but I didn't try that since it might be a rabbit hole.

if isinstance(iterable, QuerySet):
_len = max_limit or iterable.count()
list_length = iterable.count()
list_slice_length = max_limit or list_length
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
list_slice_length = max_limit or list_length
list_slice_length = max(max_limit, list_length)

With this change you don't need to add the lines on 143 and 144

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would think we want min? No sense in slicing more than the list has.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was my first thought as well but it's max because connection_from_list_slice needs to know what the whole list length is to be able to slice correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jkimbo so this doesn't work. It returns 6 results even when the max limit of 4 is set.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed that so you can see the failing test.

graphene_django/fields.py Outdated Show resolved Hide resolved
pcraciunoiu and others added 2 commits June 10, 2020 11:06
Co-authored-by: Jonathan Kim <jkimbo@gmail.com>
Co-authored-by: Jonathan Kim <jkimbo@gmail.com>
@pcraciunoiu
Copy link
Contributor Author

Looks like needs to handle `None. Gonna update this a bit later

@pcraciunoiu
Copy link
Contributor Author

@jkimbo thoughts on this?

@jkimbo
Copy link
Member

jkimbo commented Jun 24, 2020

@pcraciunoiu sorry for the delay with this. Looking into it now.

@jkimbo
Copy link
Member

jkimbo commented Jun 24, 2020

OK @pcraciunoiu I think I've found the solution. You were right you should use min but it looks like the slice_start and the iterable needs to be modified before I can get all the tests to pass. I've created a commit with the changes here: 81eb81a (I couldn't push the changes to your branch because I don't think you've given maintainers permission to edit it)

If you can either let me push to your branch or cherry pick that commit then we can get this merged.

@pcraciunoiu
Copy link
Contributor Author

I tried following this, but I don't see a button to allow. I cherry-picked exactly what you had and left a comment, but I think this makes sense.

@jkimbo jkimbo merged commit 3c229b6 into graphql-python:master Jun 25, 2020
@jkimbo
Copy link
Member

jkimbo commented Jun 25, 2020

@pcraciunoiu I fixed the issue you've mentioned here: #986 (comment) : #993

@pcraciunoiu
Copy link
Contributor Author

@jkimbo awesome! Thanks for taking this one step further!

@pcraciunoiu pcraciunoiu deleted the fix/connection-limit-2 branch June 25, 2020 14:47
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

Successfully merging this pull request may close these issues.

None yet

2 participants