Skip to content

How to add pagination ? #416

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

Closed
dmitry-saritasa opened this issue Feb 18, 2017 · 10 comments
Closed

How to add pagination ? #416

dmitry-saritasa opened this issue Feb 18, 2017 · 10 comments

Comments

@dmitry-saritasa
Copy link

dmitry-saritasa commented Feb 18, 2017

In the swapi star wars example:
https://github.com/graphql-python/swapi-graphene/blob/master/starwars/schema.py

class Film(DjangoObjectType):
    producers = graphene.List(graphene.String)

    @resolve_only_args
    def resolve_producers(self):
        return [c.strip() for c in self.producer.split(',')]

    '''A single film.'''
    class Meta:
        model = models.Film
        interfaces = (Node, )
        exclude_fields = ('created', 'edited', 'producer')
        filter_fields = {'episode_id': ('gt', )}

Film.Connection = connection_for_type(Film)

and query

class Query(graphene.ObjectType):
    all_films = DjangoFilterConnectionField(Film)

It seems 'after' keyword is not supported? (https://facebook.github.io/relay/docs/graphql-connections.html#content)

I tried the following query
http://swapi.graphene-python.org/graphql

query cinema($withCinemas: Boolean=false){
  ids: allFilms(first:1 after: "RmlsbToy") {
    edges {
      node {
        id
        title
      }
    }
  }
  allFilms  @include(if: $withCinemas) {
    edges {
      node {
        id
        title
        director
        releaseDate
      }
    }
  }
  
  part3: film(id: "RmlsbTox") {
    ...movieDetails
  }
  part4: film(id: "RmlsbToy") {
    ...movieDetails
  }
}

fragment movieDetails on Film {
  id
  title
   # pll	
   planets {
      edges {
        node {
          id
          name
          diameter
        }
      }
    }
    species {
      edges {
        node {
          id
          name
          classification
          eyeColors
        }
      }
    }
}

and always getting this

{
  "data": {
    "ids": {
      "edges": [
        {
          "node": {
            "id": "RmlsbTox",
            "title": "A New Hope"
          }
        }
      ]
    },
....
}

which is just the first in a row?

@ivlevdenis
Copy link

ivlevdenis commented Mar 18, 2017

Use cursor, and standard query args (first, after, and etc.)

@un33k
Copy link

un33k commented Mar 21, 2017

@ivlevdenis any code snippet or link to example code would be nice.

@ivlevdenis
Copy link

@un33k ok. Simple query.

{
  offers(first: 2) {
    edges {
      node {
        id
      }
      cursor
    }
    pageInfo {
      hasNextPage
      hasPreviousPage
      startCursor
      endCursor
    }
  }
}

This response with meta about cursor state:

{
  "data": {
    "offers": {
      "edges": [
        {
          "node": {
            "id": "T2ZmZXJPYmplY3Q6MjI0"
          },
          "cursor": "YXJyYXljb25uZWN0aW9uOjA="
        },
        {
          "node": {
            "id": "T2ZmZXJPYmplY3Q6OTc3"
          },
          "cursor": "YXJyYXljb25uZWN0aW9uOjE="
        }
      ],
      "pageInfo": {
        "hasNextPage": true,
        "hasPreviousPage": false,
        "startCursor": "YXJyYXljb25uZWN0aW9uOjA=",
        "endCursor": "YXJyYXljb25uZWN0aW9uOjE="
      }
    }
  }
}

For next portion of offers need call this query:

{
  offers(first: 2, after: "YXJyYXljb25uZWN0aW9uOjE=") {
    edges {
      node {
        id
      }
      cursor
    }
    pageInfo {
      hasNextPage
      hasPreviousPage
      startCursor
      endCursor
    }
  }
}

This return

{
  "data": {
    "offers": {
      "edges": [
        {
          "node": {
            "id": "T2ZmZXJPYmplY3Q6MjA2Ng=="
          },
          "cursor": "YXJyYXljb25uZWN0aW9uOjI="
        },
        {
          "node": {
            "id": "T2ZmZXJPYmplY3Q6MjE4MQ=="
          },
          "cursor": "YXJyYXljb25uZWN0aW9uOjM="
        }
      ],
      "pageInfo": {
        "hasNextPage": true,
        "hasPreviousPage": false,
        "startCursor": "YXJyYXljb25uZWN0aW9uOjI=",
        "endCursor": "YXJyYXljb25uZWN0aW9uOjM="
      }
    }
  }
}

http://graphql.org/learn/pagination/

@un33k
Copy link

un33k commented Mar 21, 2017

@ivlevdenis Looking for a working graphene pagination example. No problem with the concept or the frontend. Thx

@ivlevdenis
Copy link

ivlevdenis commented Mar 21, 2017

@un33k for graphene+django this work "from box". It's tested and worked query's from real app.

@Fohlen
Copy link

Fohlen commented Oct 25, 2017

It doesn't for me..

@frankh
Copy link

frankh commented Feb 7, 2018

Doesn't work for me either.

@sebastiandev
Copy link

sebastiandev commented Jul 10, 2018

Is not working for me either and i think the calculations for those fields in graphql-relay are not right or documentation is not clear.

The solution is actually here but hasn't been merged yet

@stale
Copy link

stale bot commented Aug 3, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Aug 3, 2019
@stale stale bot closed this as completed Aug 10, 2019
@dikshantpatodia
Copy link

dikshantpatodia commented Jul 11, 2021

@syrusakbary @dan98765 Please merge this PR for the above issue fix https://github.com/graphql-python/graphql-relay-py/pull/14/files

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

No branches or pull requests

9 participants