3

我开始在 django 中使用石墨烯,现在我不需要边和节点的所有开销,我知道它是用于分页的,但现在我只需要我的模型的字段。需要明确的是,我仍然希望能够使用过滤器集,我只是不知道如何删除边缘和节点开销。我尝试使用 graphene.List 但我无法向其中添加过滤器集。所以不要这样做

{users(nameIcontains:"a")
{
   edges{
     node{
       name
     }
   }
}

我想做这个

{users(nameIcontains:"a")
{
  name
}
4

1 回答 1

0
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)

于 2020-03-19T10:17:10.497 回答