graphql-python / graphql-python/graphene

PageInfo.hasPreviousPage is false on second page

Ouverte
#395 15 commentaires 8 réactions 0 personnes assignées Voir sur GitHub
🐛 bug
Langage dominant
Python
Étoiles
8.2k
Forks
818
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

Hi guys,

I'm using `graphene==1.1.3` with `graphene-django==1.2.1`. I'm following the Relay specification and I'm having an issue with pagination on connections.

When paginating forward using the `first` and `after` arguments getting the first page is working fine but from the second page forward `hasPreviousPage` comes back `false`. For example, on a list of 20 users the `endCursor` for the first page of 5 items is `"abc5"`. If I ask for the second page of 5 items:
```javascript
query { users(first: 5, after: "abc5"){
pageInfo {hasPreviousPage hasNextPage startCursor endCursor}
}}
```
I get back:
```
{ "data": { "users": {
"pageInfo": {
"hasPreviousPage": false, <-- This should be true
"hasNextPage": true,
"startCursor": "abc6",
"endCursor": "abc10"
}
}}}
```
Same thing happens when paginating backwards but with `hasNextPage`. When I ask for the `...(last: 5, before: "abc16")` then `hasNextPage` comes back as `false`.

I think it's a bug on the `connection_from_list_slice` method in [this file](https://github.com/graphql-python/graphql-relay-py/blob/master/graphql_relay/connection/arrayconnection.py#L32). And here is a fragment of the code where I think the problem is:

```python
def connection_from_list_slice(list_slice, args=None, connection_type=None,
edge_type=None, pageinfo_type=None,
slice_start=0, list_length=0, list_slice_length=None):
# ...

first_edge_cursor = edges[0].cursor if edges else None
last_edge_cursor = edges[-1].cursor if edges else None
lower_bound = after_offset + 1 if after else 0
upper_bound = before_offset if before else list_length

return connection_type(
edges=edges,
page_info=pageinfo_type(
start_cursor=first_edge_cursor,
end_cursor=last_edge_cursor,
has_previous_page=isinstance(last, int) and start_offset > lower_bound,
has_next_page=isinstance(first, int) and end_offset < upper_bound
)
)

# ...
```

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Piste de recherche

Commencez dans graphql_relay/connection/arrayconnection.py, au niveau de connection_from_list_slice, et comparez les cas de pagination vers l’avant et vers l’arrière décrits dans l’issue. Vérifiez que PageInfo signale correctement hasPreviousPage et hasNextPage sur les pages suivantes, en utilisant les exemples de curseurs fournis comme critères de validation.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
graphql, python
Domaine
api, backend
Type d'issue
Bug
Difficulté
3/5
Temps estimé
1-2 jours
Activité
À l'abandon
Clarté
Clairement spécifiée
Accessibilité débutants
50/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.