I am building a REST Api on Django RF. I need to set a requests limit from IP. It's easy to do that for a regular Api endpoint in views.py just adding following settings
REST_FRAMEWORK = {
'DEFAULT_THROTTLE_CLASSES': [
'rest_framework.throttling.AnonRateThrottle',
'rest_framework.throttling.UserRateThrottle'
],
'DEFAULT_THROTTLE_RATES': {
'anon': '100/day',
'user': '1000/day'
}
}
But I also have a Graphene django for a graphql api.
How can I set up a rate limit for that view? I have tried django-ratelimit, but it didn't work for me.
The issue was solved by customising GraphQL view to the following:
from graphene_django.views import GraphQLView
class CustomGraphQL(GraphQLView):
def parse_body(self, request):
if isinstance(request, Request):
return request.data
return super().parse_body(request)
@classmethod
def as_view(cls, *args, **kwargs):
view = super().as_view(*args, **kwargs)
view = authentication_classes((TokenAuthentication,))(view)
view = throttle_classes((AnonRateThrottle, AnonMinutesThrottle,
AnonSecondsThrottle, UserRateThrottle))(view)
view = api_view(['GET', 'POST'])(view)
return view