fullstack GraphQL学习笔记(16)filter

过滤链接

在link/schema.py中

# ..code
# After the imports, add
from django.db.models import Q

# ...code
class Query(graphene.ObjectType):
    # Add the search parameter inside our links field
    links = graphene.List(LinkType, search=graphene.String())
    votes = graphene.List(VoteType)

    # Change the resolver
    def resolve_links(self, info, search=None, **kwargs):
        # The value sent with the search parameter will be on the args variable
        if search:
            filter = (
                Q(url__icontains=search) | 
                Q(description__icontains=search)
            )
            return Link.objects.filter(filter)

        return Link.objects.all()

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

  测试:

query{
	links(search:"sina"){
		id
		url
		description
	}
}

  

posted @ 2018-08-29 14:04  tutu_python  阅读(280)  评论(0)    收藏  举报