fullstack GraphQL学习笔记(17)分页

一次检索出太多的信息可能会给服务器造成很大的负担,这时就可以用到分页来切分检索的数据。在links/schema.py中

class Query(graphene.ObjectType):
    # Add the first and skip parameters
    links = graphene.List(
        LinkType,
        search=graphene.String(),
        first=graphene.Int(),
        skip=graphene.Int(),
    )
    votes = graphene.List(VoteType)

    # Use them to slice the Django queryset
    def resolve_links(self, info, search=None, first=None, skip=None, **kwargs):
        qs = Link.objects.all()

        if search:
            filter = (
                Q(url__icontains=search) | 
                Q(description__icontains=search)
            )
            qs = qs.filter(filter)

        if skip:
            qs = qs[skip::]

        if first:
            qs = qs[:first]

        return qs

    def resolve_votes(self, info, **kwargs):
        return Vote.objects.all()

  查询:

query{
  links(
    first:1  
  )
  {
    id
    url
    description
  }
}

  

 

posted @ 2018-08-29 15:29  tutu_python  阅读(402)  评论(1)    收藏  举报