Reputation: 148
I searched a bit couldn't find a decent solution. I would like to count the number of unique hits on a graphene query. The frontend is react + apollo if that matters. Any recommendation?
Let me clarify the question; I would like to count the number of unique hits (ip&user agent&whatever) to the graphene query/endpoint (graphql; not db).
Upvotes: 2
Views: 538
Reputation: 211
You can take advantage of get_node
function given by DjangoObjectType
.
I assume that you already use DjangoObjectType
to define your node and YourCustomModel
have a field named view_count
.
Here's how you can do this:
from graphene_django import DjangoObjectType
class YourCustomNode(DjangoObjectType):
class Meta:
model = YourCustomModel
interfaces = (graphene.relay.Node,)
filter_fields = {
...
}
@classmethod
def get_node(cls, info, id):
queryset = cls.get_queryset(cls._meta.model.objects, info)
try:
obj = YourCustomModel.objects.get(id=id)
obj.view_count += 1
obj.save(update_fields=['view_count', ])
return queryset.get(pk=id)
except cls._meta.model.DoesNotExist:
return None
Upvotes: 0
Reputation: 1131
If I correctly understand you, you'd like to count unique hits to DB. I think the best solution for you, should be used Django Debug Middleware. If you correctly add Debug Middleware you will need to add to your query:
_debug {
sql {
rawSql
}
}
After that you in _debug
section you will have clear SQL query to DB.
Upvotes: 1