如何使用 django-ratelimit 进行石墨烯解析

how to use django-ratelimit for graphene resolve

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

我写了一个simple decorator,可以支持gql:xxxxdjango-ratelimit这样的key,这里是demo:

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)