3

i started to use graphene with django and for right now i don't need all that overhead of edges and node, i know it's for pagination but right now i only need the fields of my model. To be clear, i still want to be able to use filterset i just dont know how to remove the edges and node overhead. i've tried to use graphene.List but i couldn't add filterset to it. so instead of doing this

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

i would like to do this

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

1 Answers1

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

If you want to filter based on some let's say department_id and an optional 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)

frozenOne
  • 558
  • 2
  • 8