Custom Connection usage in graphene_django
I have a variation of the problem described here:
Using DjangoFilterConnectionField with custom Connection in graphene_django
The modified problem is as follows: Lets say I have a Django model class 'User', that has a field 'email_addrs' that is a comma-delimited string of email addresses:
class User(models.Model):
email_addrs = models.TextField(max_length=512, null=True,
help_text="Comma-delimited string of email addresses")
...<other-fields>...
And I have a UserNode(DjangoObjectType) built on top of that model, with a 'derived' field called 'count_email_addresses' with a resolver:
class UserNode(DjangoObjectType):
class Meta:
model = User
filter_fields = ('email_addrs', )
interfaces = (Node, )
connection = UserConnection
count_email_addresses = graphene.Int()
@static_method
resolve_count_email_addresses(parent, info):
# code to resolve the count of email addresses
Note the UserConnection class (above, bound to the 'connection' Meta field) that I will describe next. The count_email_addesses field will return the number of email addresses for 'a user'. My requirement is to return the number of Users and the total number of email addresses across those users - which is an aggregation across the Users in my query. For that I am using the UserConnection class that is defined as:
class UserConnection(Connection):
class Meta:
abstract = True
count_users = graphene.Int()
aggregate_count_email_addrs = graphene.Int()
I need to write resolvers for 'count_users' and 'aggregate_count_email_addrs'. The resolver for count_users is straight-forward and looks like:
@staticmethod
def resolve_count_users(root, _info, **kwargs):
return root.length
And this works fine. The 'root' argument contains the model 'User' objects and the resolver returns their length. Note, that 'root' contains the model 'User' objects, not the UserNode objects. But that works fine as there is a one-to-one correspondance between 'User' and 'UserNode', so the count comes out fine.
The problem is in the resolver for the 'aggregate_count_email_addrs'. When I write a similar resolver, since the field 'count_email_addresses' is in UserNode and not the model 'User', I don't have access to this field to perform an aggregation across all Users.
I am sure that this is a solved problem, and I just haven't discovered it yet. Any help in getting me there is highly appreciated.