Skip to content

Commit ad80040

Browse files
committed
add tests and update md
1 parent 1862281 commit ad80040

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

CHANGELOG.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
CHANGELOG
22
=========
33

4-
[Next release](https://github.com/rebing/graphql-laravel/compare/9.3.0...master)
5-
--------------
4+
[Next release](https://github.com/rebing/graphql-laravel/compare/9.4.0...master)
5+
6+
2024-02-27, 9.4.0
7+
-----------------
8+
9+
## Added
10+
- Possibility to add resolver middleware at runtime using `GraphQL::appendGlobalResolverMiddleware(YourMiddleware::class)`
611

712
2024-02-18, 9.3.0
813
-----------------

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,16 @@ Alternatively, you can override `getMiddleware` to supply your own logic:
11501150
return array_merge([...], $this->middleware);
11511151
}
11521152
```
1153+
If you want to register middleware globally, use can use the `appendGlobalResolverMiddleware` method in ServiceProvider:
1154+
1155+
```php
1156+
...
1157+
public function boot()
1158+
{
1159+
...
1160+
GraphQL::appendGlobalResolverMiddleware(YourMiddleware::class);
1161+
}
1162+
```
11531163

11541164
#### Terminable middleware
11551165

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php declare(strict_types = 1);
2+
namespace Rebing\GraphQL\Tests\Support\Middlewares;
3+
4+
use Closure;
5+
use GraphQL\Type\Definition\ResolveInfo;
6+
use Rebing\GraphQL\Support\Middleware;
7+
8+
class GlobalMiddleware extends Middleware
9+
{
10+
/**
11+
* @phpstan-param mixed $root
12+
* @phpstan-param mixed $context
13+
* @phpstan-return mixed
14+
*/
15+
public function handle($root, array $args, $context, ResolveInfo $info, Closure $next): mixed
16+
{
17+
return parent::handle($root, $args, $context, $info, $next);
18+
}
19+
}

tests/Unit/MiddlewareTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace Rebing\GraphQL\Tests\Unit;
55

66
use Rebing\GraphQL\Support\Facades\GraphQL;
7+
use Rebing\GraphQL\Tests\Support\Middlewares\GlobalMiddleware;
78
use Rebing\GraphQL\Tests\TestCase;
89

910
class MiddlewareTest extends TestCase
@@ -79,4 +80,17 @@ public function testMiddlewareTerminateHappensAfterResponseIsSent(): void
7980
self::assertObjectHasProperty('errors', $result);
8081
self::assertMatchesRegularExpression('/^Undefined .* 6$/', $result->errors[0]->getMessage());
8182
}
83+
84+
public function testGlobalMiddlewareExecuted(): void
85+
{
86+
$mock = $this->partialMock(GlobalMiddleware::class);
87+
GraphQL::appendGlobalResolverMiddleware($mock);
88+
$result = GraphQL::queryAndReturnResult($this->queries['exampleMiddleware'], [
89+
'index' => 2,
90+
]);
91+
$mock->shouldHaveReceived('handle');
92+
93+
self::assertObjectHasProperty('errors', $result);
94+
self::assertSame('Example 3 is not allowed', $result->errors[0]->getMessage());
95+
}
8296
}

0 commit comments

Comments
 (0)