简体   繁体   中英

how to use django-ratelimit for graphene resolve

We cannot use django-ratelimit directly for graphql resolve method. Because the default decorator is get request from the first argument.

I wrote a simple decorator , which can support key like gql:xxxx with django-ratelimit , here is 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM