diff --git a/app/Http/Middleware/GraphQL/RateLimitPerQuery.php b/app/Http/Middleware/GraphQL/RateLimitPerQuery.php index 53c63954b..880f1eda2 100644 --- a/app/Http/Middleware/GraphQL/RateLimitPerQuery.php +++ b/app/Http/Middleware/GraphQL/RateLimitPerQuery.php @@ -6,6 +6,7 @@ namespace App\Http\Middleware\GraphQL; use App\Enums\Auth\SpecialPermission; use Closure; +use GraphQL\Error\SyntaxError; use GraphQL\Language\AST\OperationDefinitionNode; use GraphQL\Language\Parser; use Illuminate\Http\JsonResponse; @@ -41,18 +42,18 @@ class RateLimitPerQuery } if ($query) { - try { - $ast = Parser::parse($query); - } catch (JsonException) { - return $next($request); - } - $rootFields = 0; - foreach ($ast->definitions as $definition) { - if ($definition instanceof OperationDefinitionNode) { - $rootFields += count($definition->selectionSet->selections ?? []); + try { + $ast = Parser::parse($query); + + foreach ($ast->definitions as $definition) { + if ($definition instanceof OperationDefinitionNode) { + $rootFields += count($definition->selectionSet->selections ?? []); + } } + } catch (JsonException|SyntaxError) { + // Do nothing } $hits = max(1, $rootFields); diff --git a/config/graphql.php b/config/graphql.php index 82b778896..4c325d79e 100644 --- a/config/graphql.php +++ b/config/graphql.php @@ -6,7 +6,7 @@ use App\GraphQL\Schema\Schemas\DefaultSchema; use App\GraphQL\Schema\Types\Base\PaginationInfoType; return [ - 'rate_limit' => env('GRAPHQL_RATE_LIMIT', 80), + 'rate_limit' => (int) env('GRAPHQL_RATE_LIMIT', 80), 'route' => [ // The prefix for routes; do NOT use a leading slash!