mirror of
https://github.com/AnimeThemes/animethemes-server.git
synced 2026-07-11 01:24:46 +02:00
fix(graphql): rate limit on parse error (#998)
This commit is contained in:
@@ -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);
|
||||
|
||||
+1
-1
@@ -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!
|
||||
|
||||
Reference in New Issue
Block a user