I would like to sum a field in my resolver of django-graphene using django-filter. Typically my resolvers would look like:
my_model = DjangoFilterConnectionField(
        MyModelNode,
        filterset_class=MyModelFilter)
def my_resolver(self, args, context, info):
    return MyModelFilter(
        data=format_query_args(args),
        queryset=self).qs
Which works fine. 
However, I would like to provide a custom queryset to the model filter so that I can perform aggregations on fields. I'm trying to do something like this:
def my_resolver(self, args, context, info):
    queryset = MyModel.objects.values(
        'customer_id').annotate(
        cost_amt=Sum('cost_amt', output_field=FloatField()))
    return MyModelFilter(
        data=format_query_args(args),
        queryset=queryset).qs
Inspecting the raw SQL in GraphiQL, it looks correct. However, the error message I receive from GraphQL is
"message": "Received incompatible instance \"{'cost_amt': 260.36, 'customer_id': 300968697}\"."
This is the correct result, but I'm unsure why GraphQL is getting this object from django-graphene. How can I provide a custom queryset and make this work?
For queryset to work you need to get instance of the model which you can get using
queryset = MyModel.objects.annotate(cost_amt=Sum('cost_amt', output_field=FloatField()))
and then you can try further actions.
return MyModelFilter(data=format_query_args(args),queryset=queryset).qs
still error
Try ASSET UNION and see if that works else you can also try DjangoConnectionField from relay.connection.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With