Skip to content

Commit

Permalink
Correct decay value calculation in @throttle directive
Browse files Browse the repository at this point in the history
  • Loading branch information
travisricks committed Jun 21, 2024
1 parent d15b988 commit 16ffb6f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ You can find and compare releases at the [GitHub release page](https://github.co

## Unreleased

### Fixed

- Correct decay value calculation in `@throttle` directive https://github.com/nuwave/lighthouse/pull/2573

## v6.38.0

### Added
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/Directives/ThrottleDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function handleField(FieldValue $fieldValue): void
sha1($name . $limit->key),
$limit->maxAttempts,
// Laravel 11 switched to using seconds
$limit->decayMinutes ?? $limit->decaySeconds * 60,
$limit->decayMinutes ?? $limit->decaySeconds / 60,
"{$resolveInfo->parentType}.{$resolveInfo->fieldName}",
);
}
Expand Down
46 changes: 46 additions & 0 deletions tests/Integration/Schema/Directives/ThrottleDirectiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests\Integration\Schema\Directives;

use Carbon\Carbon;
use Faker\Factory;
use Illuminate\Cache\RateLimiter;
use Illuminate\Cache\RateLimiting\Limit;
Expand Down Expand Up @@ -81,6 +82,51 @@ public function testNamedLimiter(): void
new RateLimitException('Query.foo'),
);
}

public function testLimitClears(): void
{
$this->schema = /** @lang GraphQL */ '
type Query {
foo: Int @throttle(name: "test")
}
';

$query = /** @lang GraphQL */ '
{
foo
}
';

$rateLimiter = $this->app->make(RateLimiter::class);
$rateLimiter->for(
'test',
static fn (): Limit => Limit::perMinute(1),
);

$knownDate = Carbon::createStrict(2020, 1, 1, 1); // arbitrary known date
Carbon::setTestNow($knownDate);

$this->graphQL($query)->assertJson([
'data' => [
'foo' => Foo::THE_ANSWER,
],
]);

$this->graphQL($query)->assertGraphQLError(
new RateLimitException('Query.foo'),
);

// wait two minutes and assert that the limit is reset
Carbon::setTestNow($knownDate->copy()->addMinutes(2));

$this->graphQL($query)->assertJson([
'data' => [
'foo' => Foo::THE_ANSWER,
],
]);

Carbon::setTestNow();
}

public function testInlineLimiter(): void
{
Expand Down

0 comments on commit 16ffb6f

Please sign in to comment.