diff --git a/EventListener/RateLimitListener.php b/EventListener/RateLimitListener.php index b1a53b6..8b86782 100644 --- a/EventListener/RateLimitListener.php +++ b/EventListener/RateLimitListener.php @@ -57,16 +57,24 @@ public function onKernelRequest(RequestEvent $event) } // only process on master request - if (!$event->isMasterRequest()) { + if (method_exists($event, 'isMasterRequest') && !$event->isMasterRequest()) { + return; + } + + if (!$event->isMainRequest()) { return; } $request = $event->getRequest(); - if (!$request->attributes->has('_api_resource_class')) { + if (!$request->attributes->has('_api_resource_class') && !$request->attributes->has('_graphql')) { return; } - $this->rateLimitHandler->handle($request); + if ($request->attributes->has('_api_resource_class')) { + $this->rateLimitHandler->handle($request); + } else { + $this->rateLimitHandler->handleGraphQL($request); + } if ($this->rateLimitHandler->isEnabled()) { $request->attributes->set('_api_rate_limit_info', $this->rateLimitHandler->getRateLimitInfo()); diff --git a/Service/RateLimitHandler.php b/Service/RateLimitHandler.php index 6756d4d..bf74bf0 100644 --- a/Service/RateLimitHandler.php +++ b/Service/RateLimitHandler.php @@ -156,6 +156,17 @@ public function handle(Request $request) } } + public function handleGraphQL(Request $request) + { + $annotation = new ApiRateLimit(); + + list($key, $limit, $period) = $this->getThrottle($request, $annotation); + + if ($this->enabled) { + $this->decreaseRateLimitRemaining($key, $limit, $period); + } + } + /** * @throws \Psr\Cache\InvalidArgumentException */ diff --git a/composer.json b/composer.json index e3238d5..8889f75 100644 --- a/composer.json +++ b/composer.json @@ -14,12 +14,12 @@ "php": "^7.1.3 || ^8.0", "api-platform/core": "^2.4", "doctrine/annotations": "^1.4", - "symfony/cache": "^3.4 || ^4.4 || ^5.0", - "symfony/config": "^3.4 || ^4.4 || ^5.0", - "symfony/dependency-injection": "^3.4 || ^4.4 || ^5.0", - "symfony/http-foundation": "^3.4 || ^4.4 || ^5.0", - "symfony/http-kernel": "^4.4 || ^5.0", - "symfony/security-bundle": "^3.4 || ^4.4 || ^5.0" + "symfony/cache": "^3.4 || ^4.4 || ^5.0 || ^6.0", + "symfony/config": "^3.4 || ^4.4 || ^5.0 || ^6.0", + "symfony/dependency-injection": "^3.4 || ^4.4 || ^5.0 || ^6.0", + "symfony/http-foundation": "^3.4 || ^4.4 || ^5.0 || ^6.0", + "symfony/http-kernel": "^4.4 || ^5.0 || ^6.0", + "symfony/security-bundle": "^3.4 || ^4.4 || ^5.0 || ^6.0" }, "autoload": { "psr-4": {