Graphene: How to add pagination ?

Created on 18 Feb 2017  路  9Comments  路  Source: graphql-python/graphene

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?

wontfix 馃悰 bug 馃摉 documentation 馃檵 help wanted

Most helpful comment

@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/

All 9 comments

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

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

@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/

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

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

It doesn't for me..

Doesn't work for me either.

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

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

romaia picture romaia  路  3Comments

mraak picture mraak  路  3Comments

japrogramer picture japrogramer  路  4Comments

fishy picture fishy  路  4Comments

junchiz picture junchiz  路  3Comments