'Using Django views queries in graphene resolvers

How can I use my django view queries in graphene resolvers as queries?

def get_queryset(self):
    return CustomerModel.objects.for_entity(
        entity_slug=self.kwargs['entity_slug'],
        user_model=self.request.user
    ).order_by('-updated')

I tried this but it did not work

class CustomerList(DjangoObjectType):
"""testing API
"""
class Meta:
    model = CustomerModel
    fields = ("email",)
class CustomerQuery(graphene.ObjectType):
all_customers = graphene.List(CustomerList)

def resolve_all_customers(self, root, **kwargs):
    return CustomerModel.objects.for_entity.filter(
        entity_slug=self.kwargs['entity_slug'],
        user_model=self.request.user
    ).order_by('-updated')

I get this graphql error

"message": "'NoneType' object has no attribute 'kwargs'",
"locations": [
{
"line": 2,
"column": 3
}



Solution 1:[1]

You have to define the arguement in

all_customers = graphene.List(CustomerList)

Like this

all_customers = graphene.List(CustomerList, entity_slug=graphene.String(required=True))

Complete Code:

class CustomerQuery(graphene.ObjectType):
    all_customers = graphene.List(CustomerList, entity_slug=graphene.String(required=True))

def resolve_all_customers(self, root, **kwargs):
    return CustomerModel.objects.for_entity.filter(
        entity_slug=kwargs.get('entity_slug'),
        user_model=self.request.user
    ).order_by('-updated')

To read more, check the official document

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zilay