from graphene import ObjectType
from graphene_django import DjangoObjectType
class UserType(DjangoObjectType):
class Meta:
filter_fields = {'id': ['exact']}
model = User
class Query(ObjectType):
all_users = List(UserType)
@staticmethod
def resolve_all_users(root, info, **kwargs):
users = User.objects.all()
# filtering like user.objects.filter ....
return all_users
如果你想根据一些假设department_id
和一个可选的过滤social_club_id
:
class Query(ObjectType):
all_users = List(
UserType,
department_id=ID(required=True),
social_club_id=ID(), # optional
)
@staticmethod
def resolve_all_users(root, info, department_id, **kwargs):
social_club_id = kwargs.pop('social_club_id', None)
users = User.objects.all()
# filtering like user.objects.filter ....
return all_users.objects.filter(department_id=department_id)