Eric paul
Eric paul

Reputation: 116

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
}

Upvotes: 0

Views: 600

Answers (1)

Zilay
Zilay

Reputation: 306

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

Upvotes: 1

Related Questions