Skip to content

Commit

Permalink
fix(graphql): @graphql Logical operators will group conditions corr…
Browse files Browse the repository at this point in the history
…ectly.
  • Loading branch information
LastDragon-ru committed Aug 18, 2022
1 parent b28362e commit d79da46
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 27 deletions.
15 changes: 11 additions & 4 deletions packages/graphql/src/SearchBy/Directives/DirectiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,17 @@ public function dataProviderHandleBuilder(): array {
from
"tmp"
where
not (
("a" != ?)
and (
("a" = ?) or ("b" != ?)
(
not (
(
("a" != ?)
and (
(
("a" = ?)
or ("b" != ?)
)
)
)
)
)
SQL
Expand Down
4 changes: 2 additions & 2 deletions packages/graphql/src/SearchBy/Operators/Logical/AllOfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function dataProviderCall(): array {
new ArrayDataProvider([
'property' => [
[
'query' => 'select * from "tmp" where ("a" = ?) and ("b" != ?)',
'query' => 'select * from "tmp" where (("a" = ?) and ("b" != ?))',
'bindings' => [
2,
22,
Expand All @@ -99,7 +99,7 @@ public function dataProviderCall(): array {
],
'with alias' => [
[
'query' => 'select * from "tmp" where ("alias"."a" = ?) and ("alias"."b" != ?)',
'query' => 'select * from "tmp" where (("alias"."a" = ?) and ("alias"."b" != ?))',
'bindings' => [
2,
22,
Expand Down
4 changes: 2 additions & 2 deletions packages/graphql/src/SearchBy/Operators/Logical/AnyOfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function dataProviderCall(): array {
new ArrayDataProvider([
'property' => [
[
'query' => 'select * from "tmp" where ("a" = ?) or ("b" != ?)',
'query' => 'select * from "tmp" where (("a" = ?) or ("b" != ?))',
'bindings' => [
2,
22,
Expand All @@ -99,7 +99,7 @@ public function dataProviderCall(): array {
],
'with alias' => [
[
'query' => 'select * from "tmp" where ("alias"."a" = ?) or ("alias"."b" != ?)',
'query' => 'select * from "tmp" where (("alias"."a" = ?) or ("alias"."b" != ?))',
'bindings' => [
2,
22,
Expand Down
38 changes: 21 additions & 17 deletions packages/graphql/src/SearchBy/Operators/Logical/Logical.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,28 @@ public function call(Handler $handler, object $builder, Property $property, Argu
throw new OperatorUnsupportedBuilder($this, $builder);
}

// The last item is the name of the operator not a property
$property = $property->getParent();
$conditions = $this->getConditions($argument);
$builder->where(
function (EloquentBuilder|QueryBuilder $builder) use ($handler, $property, $argument): void {
// The last item is the name of the operator not a property
$property = $property->getParent();
$conditions = $this->getConditions($argument);

foreach ($conditions as $arguments) {
$builder->where(
static function (EloquentBuilder|QueryBuilder $builder) use (
$handler,
$arguments,
$property
): void {
$handler->handle($builder, $property, $arguments);
},
null,
null,
$this->getBoolean(),
);
}
foreach ($conditions as $arguments) {
$builder->where(
static function (EloquentBuilder|QueryBuilder $builder) use (
$handler,
$arguments,
$property
): void {
$handler->handle($builder, $property, $arguments);
},
null,
null,
$this->getBoolean(),
);
}
},
);

return $builder;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/graphql/src/SearchBy/Operators/Logical/NotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function dataProviderCall(): array {
new ArrayDataProvider([
'property' => [
[
'query' => 'select * from "tmp" where not ("a" = ?)',
'query' => 'select * from "tmp" where (not ("a" = ?))',
'bindings' => [
2,
],
Expand All @@ -99,7 +99,7 @@ public function dataProviderCall(): array {
],
'with alias' => [
[
'query' => 'select * from "tmp" where not ("alias"."a" = ?)',
'query' => 'select * from "tmp" where (not ("alias"."a" = ?))',
'bindings' => [
2,
],
Expand Down

0 comments on commit d79da46

Please sign in to comment.