首页 > 解决方案 > 如何使用 django-ratelimit 进行石墨烯解析

问题描述

我们不能django-ratelimit直接用于 graphql 解析方法。因为默认装饰器是从第一个参数获取请求。

标签: djangographene-python

解决方案


我写了一个简单的装饰器,它可以支持像gql:xxxxwith这样的键django-ratelimit,这里是演示:

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

  ok = graphene.Boolean()

  @ratelimit(key="gql:phone", rate="5/m", block=True) # here key: 'gql:phone'
  def mutate(self, info, phone):
    request = info.context
    # Do sth
    return TestMutaion(ok=True)

推荐阅读