Use django-ratelimit for graphql
Project description
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.
from django_graphql_ratelimit import ratelimit
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 RequestSMSCode(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, modify yourMIDDLEWARE
in settings:
MIDDLEWARE = [
"django_graphql_ratelimit.middleware.ParseClientIpMiddleware",
...
]
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 fromrequest.user
if the user is authenticated, otherwise userequest.META['REMOTE_ADDR']
.
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
Built Distribution
File details
Details for the file django-graphql-ratelimit-1.0.2.tar.gz
.
File metadata
- Download URL: django-graphql-ratelimit-1.0.2.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b95d6322301feb7e067fdd4d62b104a0294e2c09ab45c113eb0c1503565413ae |
|
MD5 | 3f0f6111f671b359e898d4456bd13a83 |
|
BLAKE2b-256 | 8dcf8210f4593c2d44ced69b20699337dfde2d0655e820591cbd3efbd9530701 |
File details
Details for the file django_graphql_ratelimit-1.0.2-py3-none-any.whl
.
File metadata
- Download URL: django_graphql_ratelimit-1.0.2-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5b6a9a6517b97e1126dbf01ba318b22ee4ab14d304e8211d10ddf105e4a72917 |
|
MD5 | 300e79432ee68aede55f1c9e29f0ad9a |
|
BLAKE2b-256 | e75d1ff5624c25e99e3fe08f5b507abb57f759597bdd9c758061a0ea4cac3325 |