Skip to main content

Use django-ratelimit for graphql

Project description

CircleCI

Eaiser to use django-ratelimit for graphql in django.

Install

pip install django-graphql-ratelimit

Usage

ratelimit key support gql:xxx, where xxx is argument.

class RequestSMSCode(graphene.Mutation):
    class Arguments:
        phone = graphene.String(required=True)

        ok = graphene.Boolean()

        @ratelimit(key="ip", rate="10/m", block=True)
        @ratelimit(key="gql:phone", rate="5/m", block=True)
        def mutate(self, info, phone):
            request = info.context
            # send sms code logic
            return Test(ok=True)

You can use django-ratelimit keys except get:xxx and post:xxx:

  • ip - Use the request IP address (i.e. request.META['REMOTE_ADDR']) I suggest you to use django-ipware to get client ip:
MIDDLEWARE = [
"django_graphql_ratelimit.middleware.ParseRemoteAddrMiddleware",
...
]
  • header:x-x - Use the value of request.META.get('HTTP_X_X', '').
  • user - Use an appropriate value from request.user. Do not use with unauthenticated users.
  • user_or_ip - Use an appropriate value from request.user if the user is authenticated, otherwise use request.META['REMOTE_ADDR'] (see the note above about reverse proxies).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-graphql-ratelimit-0.1.1.tar.gz (5.1 kB view hashes)

Uploaded Source

Built Distribution

django_graphql_ratelimit-0.1.1-py3-none-any.whl (6.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page