0

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
}

1 Answer 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

Sign up to request clarification or add additional context in comments.

7 Comments

it works fine but i get { "data": { "allCustomers": null } query{ allCustomers(entitySlug:"Stats") { email } }
what is your implementation for class CustomerList(DjangoObjectType):?
class CustomerList(DjangoObjectType): class Meta: model = CustomerModel fields = ("city", "email")
Why you are using for_entity? have you tested it without for_entity?
def for_entity(self, entity_slug: str, user_model): qs = self.get_queryset() return qs.filter( Q(entity__slug__exact=entity_slug) & Q(active=True) & ( Q(entity__admin=user_model) | Q(entity__managers__in=[user_model]) ) )
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.