Search code examples
djangographene-python

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.


Solution

  • 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)