Skip to content

Commit f7a58b0

Browse files
committed
[graphql] SearchBy: Fixes false positive "Only one comparison operator allowed, found: xxx, not"
1 parent 89160a5 commit f7a58b0

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

src/SearchBy/Directive.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Complex\Relation;
2121
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Logical\AllOf;
2222
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Logical\AnyOf;
23+
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Not;
2324
use Nuwave\Lighthouse\Schema\AST\DocumentAST;
2425
use Nuwave\Lighthouse\Schema\Directives\BaseDirective;
2526
use Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective;
@@ -141,13 +142,15 @@ public function manipulateArgDefinition(
141142
public function handleBuilder($builder, $value): EloquentBuilder|QueryBuilder {
142143
return (new SearchBuilder(
143144
(new Collection($this->scalars))
145+
->add(Not::class)
144146
->flatten()
145147
->unique()
146148
->filter(static function (string $operator): bool {
147149
return class_exists($operator);
148150
})->map(function (string $operator): object {
149151
return $this->container->make($operator);
150-
})->all(),
152+
})
153+
->all(),
151154
))->build($builder, $value);
152155
}
153156
}

src/SearchBy/DirectiveTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
namespace LastDragon_ru\LaraASP\GraphQL\SearchBy;
44

5+
use Closure;
6+
use Exception;
7+
use LastDragon_ru\LaraASP\GraphQL\Testing\BuilderDataProvider;
58
use LastDragon_ru\LaraASP\GraphQL\Testing\TestCase;
9+
use LastDragon_ru\LaraASP\Testing\Providers\ArrayDataProvider;
10+
use LastDragon_ru\LaraASP\Testing\Providers\CompositeDataProvider;
611
use LastDragon_ru\LaraASP\Testing\Utils\WithTestData;
712

813
/**
@@ -25,6 +30,24 @@ public function testManipulateArgDefinition(string $expected, string $graphql):
2530

2631
$this->assertEquals($expected, $actual);
2732
}
33+
34+
/**
35+
* @covers ::handleBuilder
36+
*
37+
* @dataProvider dataProviderHandleBuilder
38+
*
39+
* @param array<mixed> $input
40+
*/
41+
public function testHandleBuilder(bool|Exception $expected, Closure $builder, array $input): void {
42+
if ($expected instanceof Exception) {
43+
$this->expectExceptionObject($expected);
44+
}
45+
46+
$builder = $builder($this);
47+
$directive = new Directive($this->app, [], []);
48+
49+
$this->assertNotNull($directive->handleBuilder($builder, $input));
50+
}
2851
// </editor-fold>
2952

3053
// <editor-fold desc="DataProvider">
@@ -38,5 +61,45 @@ public function dataProviderManipulateArgDefinition(): array {
3861
'only used type should be added' => ['~usedonly-expected.graphql', '~usedonly.graphql'],
3962
];
4063
}
64+
65+
/**
66+
* @return array<mixed>
67+
*/
68+
public function dataProviderHandleBuilder(): array {
69+
return (new CompositeDataProvider(
70+
new BuilderDataProvider(),
71+
new ArrayDataProvider([
72+
'valid condition' => [
73+
true,
74+
[
75+
'not' => 'yes',
76+
'allOf' => [
77+
[
78+
'a' => [
79+
'eq' => 1,
80+
'not' => 'yes',
81+
],
82+
],
83+
[
84+
'anyOf' => [
85+
[
86+
'a' => [
87+
'eq' => 2,
88+
],
89+
],
90+
[
91+
'b' => [
92+
'eq' => 3,
93+
'not' => 'yes',
94+
],
95+
],
96+
],
97+
],
98+
],
99+
],
100+
],
101+
]),
102+
))->getData();
103+
}
41104
// </editor-fold>
42105
}

0 commit comments

Comments
 (0)