From e6c79a1f997868bf88f53eb07437a677cd6e2f6f Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 25 Jun 2022 08:46:38 +0400 Subject: [PATCH 01/13] `Property` methods will return instance of static. --- packages/graphql/src/Utils/Property.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/graphql/src/Utils/Property.php b/packages/graphql/src/Utils/Property.php index d2ad52841..85f0b1560 100644 --- a/packages/graphql/src/Utils/Property.php +++ b/packages/graphql/src/Utils/Property.php @@ -14,7 +14,7 @@ class Property implements Stringable { */ protected array $path; - public function __construct( + final public function __construct( string ...$path, ) { $this->path = $path; @@ -31,13 +31,13 @@ public function getPath(): array { return $this->path; } - public function getChild(string $name): Property { - return new Property(...[...$this->path, $name]); + public function getChild(string $name): static { + return new static(...[...$this->path, $name]); } - public function getParent(): Property { + public function getParent(): static { $path = array_slice($this->path, -1); - $parent = new Property(...$path); + $parent = new static(...$path); return $parent; } From 9e89f528267992abf7215e44cad2cc337352741f Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 25 Jun 2022 09:21:57 +0400 Subject: [PATCH 02/13] General code to handle builder/operators extracted into separate namespace from `@searchBy` directive. --- .../Contracts/Handler.php} | 8 +- .../src/Builder/Contracts/Operator.php | 38 +++++ .../Builder/Directives/HandlerDirective.php | 158 ++++++++++++++++++ .../Builder/Directives/OperatorDirective.php | 25 +++ .../Builder/Exceptions/BuilderException.php | 9 + .../Exceptions/Client/ClientException.php} | 6 +- .../Exceptions/Client/ConditionEmpty.php | 11 ++ .../Client/ConditionTooManyOperators.php} | 6 +- .../Client/ConditionTooManyProperties.php} | 4 +- .../Exceptions/HandlerInvalidConditions.php | 27 +++ .../Exceptions/OperatorUnsupportedBuilder.php | 6 +- .../src/{Utils => Builder}/Property.php | 2 +- .../graphql/src/SearchBy/Ast/MetadataTest.php | 8 +- .../src/SearchBy/Contracts/Operator.php | 36 +--- .../src/SearchBy/Directives/Directive.php | 135 +-------------- .../src/SearchBy/Directives/DirectiveTest.php | 12 +- .../Exceptions/BuilderInvalidConditions.php | 27 --- .../Client/SearchConditionEmpty.php | 11 -- .../SearchBy/Exceptions/SearchByException.php | 4 +- .../src/SearchBy/Operators/BaseOperator.php | 21 +-- .../SearchBy/Operators/Comparison/Between.php | 8 +- .../Operators/Comparison/BetweenTest.php | 6 +- .../Operators/Comparison/Contains.php | 8 +- .../Operators/Comparison/ContainsTest.php | 6 +- .../Operators/Comparison/EndsWithTest.php | 6 +- .../SearchBy/Operators/Comparison/Equal.php | 8 +- .../Operators/Comparison/EqualTest.php | 6 +- .../Operators/Comparison/GreaterThan.php | 8 +- .../Comparison/GreaterThanOrEqual.php | 9 +- .../Comparison/GreaterThanOrEqualTest.php | 6 +- .../Operators/Comparison/GreaterThanTest.php | 6 +- .../src/SearchBy/Operators/Comparison/In.php | 8 +- .../SearchBy/Operators/Comparison/InTest.php | 6 +- .../Operators/Comparison/IsNotNull.php | 8 +- .../Operators/Comparison/IsNotNullTest.php | 6 +- .../SearchBy/Operators/Comparison/IsNull.php | 8 +- .../Operators/Comparison/IsNullTest.php | 6 +- .../Operators/Comparison/LessThan.php | 8 +- .../Operators/Comparison/LessThanOrEqual.php | 8 +- .../Comparison/LessThanOrEqualTest.php | 6 +- .../Operators/Comparison/LessThanTest.php | 6 +- .../SearchBy/Operators/Comparison/Like.php | 8 +- .../Operators/Comparison/LikeTest.php | 6 +- .../Operators/Comparison/NotBetween.php | 8 +- .../Operators/Comparison/NotBetweenTest.php | 6 +- .../Operators/Comparison/NotEqual.php | 8 +- .../Operators/Comparison/NotEqualTest.php | 6 +- .../SearchBy/Operators/Comparison/NotIn.php | 8 +- .../Operators/Comparison/NotInTest.php | 6 +- .../SearchBy/Operators/Comparison/NotLike.php | 8 +- .../Operators/Comparison/NotLikeTest.php | 6 +- .../Operators/Comparison/StartsWithTest.php | 6 +- .../SearchBy/Operators/Complex/Relation.php | 14 +- .../Operators/Complex/RelationTest.php | 6 +- .../SearchBy/Operators/Logical/AllOfTest.php | 2 +- .../SearchBy/Operators/Logical/AnyOfTest.php | 2 +- .../SearchBy/Operators/Logical/Logical.php | 13 +- .../SearchBy/Operators/Logical/NotTest.php | 2 +- phpstan-baseline.neon | 10 +- 59 files changed, 441 insertions(+), 385 deletions(-) rename packages/graphql/src/{SearchBy/Contracts/Builder.php => Builder/Contracts/Handler.php} (52%) create mode 100644 packages/graphql/src/Builder/Contracts/Operator.php create mode 100644 packages/graphql/src/Builder/Directives/HandlerDirective.php create mode 100644 packages/graphql/src/Builder/Directives/OperatorDirective.php create mode 100644 packages/graphql/src/Builder/Exceptions/BuilderException.php rename packages/graphql/src/{SearchBy/Exceptions/Client/SearchLogicException.php => Builder/Exceptions/Client/ClientException.php} (53%) create mode 100644 packages/graphql/src/Builder/Exceptions/Client/ConditionEmpty.php rename packages/graphql/src/{SearchBy/Exceptions/Client/SearchConditionTooManyOperators.php => Builder/Exceptions/Client/ConditionTooManyOperators.php} (71%) rename packages/graphql/src/{SearchBy/Exceptions/Client/SearchConditionTooManyProperties.php => Builder/Exceptions/Client/ConditionTooManyProperties.php} (80%) create mode 100644 packages/graphql/src/Builder/Exceptions/HandlerInvalidConditions.php rename packages/graphql/src/{SearchBy => Builder}/Exceptions/OperatorUnsupportedBuilder.php (77%) rename packages/graphql/src/{Utils => Builder}/Property.php (94%) delete mode 100644 packages/graphql/src/SearchBy/Exceptions/BuilderInvalidConditions.php delete mode 100644 packages/graphql/src/SearchBy/Exceptions/Client/SearchConditionEmpty.php diff --git a/packages/graphql/src/SearchBy/Contracts/Builder.php b/packages/graphql/src/Builder/Contracts/Handler.php similarity index 52% rename from packages/graphql/src/SearchBy/Contracts/Builder.php rename to packages/graphql/src/Builder/Contracts/Handler.php index 060ea22d3..487489822 100644 --- a/packages/graphql/src/SearchBy/Contracts/Builder.php +++ b/packages/graphql/src/Builder/Contracts/Handler.php @@ -1,12 +1,12 @@ factory; + } + + /** + * @template T of object + * + * @param T $builder + * + * @return T + */ + protected function handleAnyBuilder(object $builder, mixed $value): object { + if ($value !== null) { + $argument = $this->getFactory()->getArgument($this->definitionNode, $value); + + if ($argument->value instanceof ArgumentSet) { + $builder = $this->handle($builder, $argument->value); + } else { + throw new HandlerInvalidConditions($this); + } + } + + return $builder; + } + + /** + * @template T of object + * + * @param T $builder + * + * @return T + */ + public function handle(object $builder, ArgumentSet|Argument $conditions, Property $parent = null): object { + // Prepare + $parent ??= new Property(); + $conditions = $conditions instanceof Argument + ? $conditions->value + : $conditions; + $arguments = $conditions instanceof ArgumentSet + ? $conditions->arguments + : []; + + // Empty? + if (count($arguments) === 0) { + return $builder; + } + + // Property or Operator? + $first = reset($arguments); + $isProperty = $first->directives->filter(Utils::instanceofMatcher(Operator::class))->isEmpty(); + + if ($isProperty) { + // Valid? + if (count($arguments) !== 1) { + throw new ConditionTooManyProperties(array_keys($arguments)); + } + + // Process + foreach ($arguments as $name => $argument) { + $parent = $parent->getChild($name); + $builder = $this->call($builder, $parent, $argument); + } + } elseif ($conditions instanceof ArgumentSet || $conditions instanceof Argument) { + $builder = $this->call($builder, $parent, $conditions); + } else { + throw new HandlerInvalidConditions($this); + } + + return $builder; + } + + /** + * @template T of object + * + * @param T $builder + * + * @return T + */ + protected function call(object $builder, Property $property, ArgumentSet|Argument $operator): object { + // Operator & Value + /** @var Operator|null $op */ + $op = null; + $value = null; + $filter = Utils::instanceofMatcher(Operator::class); + $operator = $operator instanceof Argument + ? $operator->value + : $operator; + + if ($operator instanceof ArgumentSet) { + if (count($operator->arguments) > 1) { + throw new ConditionTooManyOperators( + array_keys($operator->arguments), + ); + } + + foreach ($operator->arguments as $argument) { + /** @var Collection $operators */ + $operators = $argument->directives->filter($filter); + + if (count($operators) === 1) { + $op = $operators->first(); + $value = $argument; + } else { + throw new ConditionTooManyOperators( + $operators + ->map(static function (Operator $operator): string { + return $operator::getName(); + }) + ->all(), + ); + } + } + } + + // Operator? + if (!$op || !$value) { + throw new ConditionEmpty(); + } + + // Supported? + if (!$op->isBuilderSupported($builder)) { + throw new OperatorUnsupportedBuilder($op, $builder); + } + + // Return + return $op->call($this, $builder, $property, $value); + } +} diff --git a/packages/graphql/src/Builder/Directives/OperatorDirective.php b/packages/graphql/src/Builder/Directives/OperatorDirective.php new file mode 100644 index 000000000..ccd22aaa4 --- /dev/null +++ b/packages/graphql/src/Builder/Directives/OperatorDirective.php @@ -0,0 +1,25 @@ +directiveNode; + } +} diff --git a/packages/graphql/src/Builder/Exceptions/BuilderException.php b/packages/graphql/src/Builder/Exceptions/BuilderException.php new file mode 100644 index 000000000..deedf2106 --- /dev/null +++ b/packages/graphql/src/Builder/Exceptions/BuilderException.php @@ -0,0 +1,9 @@ + $operators */ @@ -16,7 +16,7 @@ public function __construct( Throwable $previous = null, ) { parent::__construct(sprintf( - 'Only one comparison operator allowed, found: `%s`.', + 'Only one operator allowed, found: `%s`.', implode('`, `', $this->getOperators()), ), $previous); } diff --git a/packages/graphql/src/SearchBy/Exceptions/Client/SearchConditionTooManyProperties.php b/packages/graphql/src/Builder/Exceptions/Client/ConditionTooManyProperties.php similarity index 80% rename from packages/graphql/src/SearchBy/Exceptions/Client/SearchConditionTooManyProperties.php rename to packages/graphql/src/Builder/Exceptions/Client/ConditionTooManyProperties.php index f0bdaab09..a13f8593b 100644 --- a/packages/graphql/src/SearchBy/Exceptions/Client/SearchConditionTooManyProperties.php +++ b/packages/graphql/src/Builder/Exceptions/Client/ConditionTooManyProperties.php @@ -1,13 +1,13 @@ $properties */ diff --git a/packages/graphql/src/Builder/Exceptions/HandlerInvalidConditions.php b/packages/graphql/src/Builder/Exceptions/HandlerInvalidConditions.php new file mode 100644 index 000000000..322472a90 --- /dev/null +++ b/packages/graphql/src/Builder/Exceptions/HandlerInvalidConditions.php @@ -0,0 +1,27 @@ +getHandler()::class, + ), + $previous, + ); + } + + public function getHandler(): Handler { + return $this->handler; + } +} diff --git a/packages/graphql/src/SearchBy/Exceptions/OperatorUnsupportedBuilder.php b/packages/graphql/src/Builder/Exceptions/OperatorUnsupportedBuilder.php similarity index 77% rename from packages/graphql/src/SearchBy/Exceptions/OperatorUnsupportedBuilder.php rename to packages/graphql/src/Builder/Exceptions/OperatorUnsupportedBuilder.php index 9c29599f2..cd649b30c 100644 --- a/packages/graphql/src/SearchBy/Exceptions/OperatorUnsupportedBuilder.php +++ b/packages/graphql/src/Builder/Exceptions/OperatorUnsupportedBuilder.php @@ -1,13 +1,13 @@ |QueryBuilder */ public function handleBuilder($builder, $value): EloquentBuilder|QueryBuilder { - if ($value !== null) { - $argument = $this->factory->getArgument($this->definitionNode, $value); - - if ($argument->value instanceof ArgumentSet) { - $builder = $this->where($builder, $argument->value); - } else { - throw new BuilderInvalidConditions($this); - } - } - - return $builder; - } - - // - // ========================================================================= - public function where(object $builder, ArgumentSet|Argument $conditions, Property $parent = null): object { - // Prepare - $parent ??= new Property(); - $conditions = $conditions instanceof Argument - ? $conditions->value - : $conditions; - $arguments = $conditions instanceof ArgumentSet - ? $conditions->arguments - : []; - - // Empty? - if (count($arguments) === 0) { - return $builder; - } - - // Property or Operator? - $first = reset($arguments); - $isProperty = $first->directives->filter(Utils::instanceofMatcher(Operator::class))->isEmpty(); - - if ($isProperty) { - // Valid? - if (count($arguments) !== 1) { - throw new SearchConditionTooManyProperties(array_keys($arguments)); - } - - // Process - foreach ($arguments as $name => $argument) { - $parent = $parent->getChild($name); - $builder = $this->call($builder, $parent, $argument); - } - } elseif ($conditions instanceof ArgumentSet || $conditions instanceof Argument) { - $builder = $this->call($builder, $parent, $conditions); - } else { - throw new BuilderInvalidConditions($this); - } - - return $builder; - } - - /** - * @template T of object - * - * @param T $builder - * - * @return T - */ - protected function call(object $builder, Property $property, ArgumentSet|Argument $operator): object { - // Operator & Value - /** @var Operator|null $op */ - $op = null; - $value = null; - $filter = Utils::instanceofMatcher(Operator::class); - $operator = $operator instanceof Argument - ? $operator->value - : $operator; - - if ($operator instanceof ArgumentSet) { - if (count($operator->arguments) > 1) { - throw new SearchConditionTooManyOperators( - array_keys($operator->arguments), - ); - } - - foreach ($operator->arguments as $argument) { - /** @var Collection $operators */ - $operators = $argument->directives->filter($filter); - - if (count($operators) === 1) { - $op = $operators->first(); - $value = $argument; - } else { - throw new SearchConditionTooManyOperators( - $operators - ->map(static function (Operator $operator): string { - return $operator::getName(); - }) - ->all(), - ); - } - } - } - - // Operator? - if (!$op || !$value) { - throw new SearchConditionEmpty(); - } - - // Supported? - if (!$op->isBuilderSupported($builder)) { - throw new OperatorUnsupportedBuilder($op, $builder); - } - - // Return - return $op->call($this, $builder, $property, $value); + return $this->handleAnyBuilder($builder, $value); } - // } diff --git a/packages/graphql/src/SearchBy/Directives/DirectiveTest.php b/packages/graphql/src/SearchBy/Directives/DirectiveTest.php index a0f7e4c75..e57e1bf00 100644 --- a/packages/graphql/src/SearchBy/Directives/DirectiveTest.php +++ b/packages/graphql/src/SearchBy/Directives/DirectiveTest.php @@ -11,11 +11,11 @@ use Illuminate\Contracts\Config\Repository; use LastDragon_ru\LaraASP\Eloquent\Testing\Package\Models\TestObject; use LastDragon_ru\LaraASP\Eloquent\Testing\Package\Models\WithTestObject; +use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\Client\ConditionEmpty; +use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\Client\ConditionTooManyOperators; +use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\Client\ConditionTooManyProperties; use LastDragon_ru\LaraASP\GraphQL\Exceptions\TypeDefinitionUnknown; use LastDragon_ru\LaraASP\GraphQL\Package; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\Client\SearchConditionEmpty; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\Client\SearchConditionTooManyOperators; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\Client\SearchConditionTooManyProperties; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Comparison\Between; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Complex\Relation; use LastDragon_ru\LaraASP\GraphQL\Testing\GraphQLExpectedSchema; @@ -352,7 +352,7 @@ public function dataProviderHandleBuilder(): array { ], ], 'empty operators' => [ - new SearchConditionEmpty(), + new ConditionEmpty(), [ 'a' => [ // empty @@ -360,7 +360,7 @@ public function dataProviderHandleBuilder(): array { ], ], 'too many properties' => [ - new SearchConditionTooManyProperties(['a', 'b']), + new ConditionTooManyProperties(['a', 'b']), [ 'a' => [ 'notEqual' => 1, @@ -371,7 +371,7 @@ public function dataProviderHandleBuilder(): array { ], ], 'too many operators' => [ - new SearchConditionTooManyOperators(['equal', 'notEqual']), + new ConditionTooManyOperators(['equal', 'notEqual']), [ 'a' => [ 'equal' => 1, diff --git a/packages/graphql/src/SearchBy/Exceptions/BuilderInvalidConditions.php b/packages/graphql/src/SearchBy/Exceptions/BuilderInvalidConditions.php deleted file mode 100644 index 86faba886..000000000 --- a/packages/graphql/src/SearchBy/Exceptions/BuilderInvalidConditions.php +++ /dev/null @@ -1,27 +0,0 @@ -getBuilder()::class, - ), - $previous, - ); - } - - public function getBuilder(): Builder { - return $this->builder; - } -} diff --git a/packages/graphql/src/SearchBy/Exceptions/Client/SearchConditionEmpty.php b/packages/graphql/src/SearchBy/Exceptions/Client/SearchConditionEmpty.php deleted file mode 100644 index d7471c7d5..000000000 --- a/packages/graphql/src/SearchBy/Exceptions/Client/SearchConditionEmpty.php +++ /dev/null @@ -1,11 +0,0 @@ -directiveNode; - } - public function isBuilderSupported(object $builder): bool { return $builder instanceof EloquentBuilder || $builder instanceof QueryBuilder; diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/Between.php b/packages/graphql/src/SearchBy/Operators/Comparison/Between.php index f777cf2f5..a161b98fd 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/Between.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/Between.php @@ -5,12 +5,12 @@ use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Query\Builder as QueryBuilder; use LastDragon_ru\LaraASP\Core\Utils\Cast; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Builder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\OperatorUnsupportedBuilder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\TypeProvider; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\OperatorUnsupportedBuilder; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\BaseOperator; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Types\Range; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use Nuwave\Lighthouse\Execution\Arguments\Argument; class Between extends BaseOperator { @@ -26,7 +26,7 @@ public function getFieldType(TypeProvider $provider, string $type): ?string { return $provider->getType(Range::class, $type); } - public function call(Builder $search, object $builder, Property $property, Argument $argument): object { + public function call(Handler $handler, object $builder, Property $property, Argument $argument): object { if (!($builder instanceof EloquentBuilder || $builder instanceof QueryBuilder)) { throw new OperatorUnsupportedBuilder($this, $builder); } diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/BetweenTest.php b/packages/graphql/src/SearchBy/Operators/Comparison/BetweenTest.php index b3b99a7e8..65e29dca4 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/BetweenTest.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/BetweenTest.php @@ -3,10 +3,10 @@ namespace LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Comparison; use Closure; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Builder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\BuilderDataProvider; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use LastDragon_ru\LaraASP\Testing\Providers\ArrayDataProvider; use LastDragon_ru\LaraASP\Testing\Providers\CompositeDataProvider; use Mockery; @@ -38,7 +38,7 @@ public function testCall( ): void { $operator = $this->app->make(Between::class); $argument = $argumentFactory($this); - $search = Mockery::mock(Builder::class); + $search = Mockery::mock(Handler::class); $builder = $builderFactory($this); $builder = $operator->call($search, $builder, $property, $argument); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/Contains.php b/packages/graphql/src/SearchBy/Operators/Comparison/Contains.php index d67aeda84..12fd58891 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/Contains.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/Contains.php @@ -7,10 +7,10 @@ use Illuminate\Database\Query\Builder as QueryBuilder; use Illuminate\Database\Query\Grammars\SqlServerGrammar; use LastDragon_ru\LaraASP\Core\Utils\Cast; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Builder; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\OperatorUnsupportedBuilder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\OperatorUnsupportedBuilder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\BaseOperator; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use Nuwave\Lighthouse\Execution\Arguments\Argument; use function strtr; @@ -24,7 +24,7 @@ public function getFieldDescription(): string { return 'Contains.'; } - public function call(Builder $search, object $builder, Property $property, Argument $argument): object { + public function call(Handler $handler, object $builder, Property $property, Argument $argument): object { if (!($builder instanceof EloquentBuilder || $builder instanceof QueryBuilder)) { throw new OperatorUnsupportedBuilder($this, $builder); } diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/ContainsTest.php b/packages/graphql/src/SearchBy/Operators/Comparison/ContainsTest.php index e9cf263a7..fdb40d384 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/ContainsTest.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/ContainsTest.php @@ -9,10 +9,10 @@ use Illuminate\Database\Query\Grammars\PostgresGrammar; use Illuminate\Database\Query\Grammars\SQLiteGrammar; use Illuminate\Database\Query\Grammars\SqlServerGrammar; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Builder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\BuilderDataProvider; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use LastDragon_ru\LaraASP\Testing\Providers\ArrayDataProvider; use LastDragon_ru\LaraASP\Testing\Providers\CompositeDataProvider; use Mockery; @@ -56,7 +56,7 @@ public function testCall( $operator = $this->app->make(Contains::class); $argument = $argumentFactory($this); - $search = Mockery::mock(Builder::class); + $search = Mockery::mock(Handler::class); $builder = $operator->call($search, $builder, $property, $argument); self::assertDatabaseQueryEquals($expected, $builder); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/EndsWithTest.php b/packages/graphql/src/SearchBy/Operators/Comparison/EndsWithTest.php index 54992a2da..4c4439898 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/EndsWithTest.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/EndsWithTest.php @@ -3,10 +3,10 @@ namespace LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Comparison; use Closure; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Builder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\BuilderDataProvider; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use LastDragon_ru\LaraASP\Testing\Providers\ArrayDataProvider; use LastDragon_ru\LaraASP\Testing\Providers\CompositeDataProvider; use Mockery; @@ -38,7 +38,7 @@ public function testCall( ): void { $operator = $this->app->make(EndsWith::class); $argument = $argumentFactory($this); - $search = Mockery::mock(Builder::class); + $search = Mockery::mock(Handler::class); $builder = $builderFactory($this); $builder = $operator->call($search, $builder, $property, $argument); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/Equal.php b/packages/graphql/src/SearchBy/Operators/Comparison/Equal.php index b41856fee..763db64cd 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/Equal.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/Equal.php @@ -4,10 +4,10 @@ use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Query\Builder as QueryBuilder; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Builder; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\OperatorUnsupportedBuilder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\OperatorUnsupportedBuilder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\BaseOperator; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use Nuwave\Lighthouse\Execution\Arguments\Argument; class Equal extends BaseOperator { @@ -19,7 +19,7 @@ public function getFieldDescription(): string { return 'Equal (`=`).'; } - public function call(Builder $search, object $builder, Property $property, Argument $argument): object { + public function call(Handler $handler, object $builder, Property $property, Argument $argument): object { if (!($builder instanceof EloquentBuilder || $builder instanceof QueryBuilder)) { throw new OperatorUnsupportedBuilder($this, $builder); } diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/EqualTest.php b/packages/graphql/src/SearchBy/Operators/Comparison/EqualTest.php index 327263ac1..00db6eb12 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/EqualTest.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/EqualTest.php @@ -3,10 +3,10 @@ namespace LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Comparison; use Closure; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Builder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\BuilderDataProvider; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use LastDragon_ru\LaraASP\Testing\Providers\ArrayDataProvider; use LastDragon_ru\LaraASP\Testing\Providers\CompositeDataProvider; use Mockery; @@ -38,7 +38,7 @@ public function testCall( ): void { $operator = $this->app->make(Equal::class); $argument = $argumentFactory($this); - $search = Mockery::mock(Builder::class); + $search = Mockery::mock(Handler::class); $builder = $builderFactory($this); $builder = $operator->call($search, $builder, $property, $argument); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/GreaterThan.php b/packages/graphql/src/SearchBy/Operators/Comparison/GreaterThan.php index d9549a28b..95de61351 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/GreaterThan.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/GreaterThan.php @@ -4,10 +4,10 @@ use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Query\Builder as QueryBuilder; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Builder; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\OperatorUnsupportedBuilder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\OperatorUnsupportedBuilder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\BaseOperator; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use Nuwave\Lighthouse\Execution\Arguments\Argument; class GreaterThan extends BaseOperator { @@ -19,7 +19,7 @@ public function getFieldDescription(): string { return 'Greater than (`>`).'; } - public function call(Builder $search, object $builder, Property $property, Argument $argument): object { + public function call(Handler $handler, object $builder, Property $property, Argument $argument): object { if (!($builder instanceof EloquentBuilder || $builder instanceof QueryBuilder)) { throw new OperatorUnsupportedBuilder($this, $builder); } diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/GreaterThanOrEqual.php b/packages/graphql/src/SearchBy/Operators/Comparison/GreaterThanOrEqual.php index 1aadf5f87..5a3d09bb0 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/GreaterThanOrEqual.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/GreaterThanOrEqual.php @@ -4,13 +4,12 @@ use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Query\Builder as QueryBuilder; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Builder; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\OperatorUnsupportedBuilder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\OperatorUnsupportedBuilder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\BaseOperator; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use Nuwave\Lighthouse\Execution\Arguments\Argument; - class GreaterThanOrEqual extends BaseOperator { public static function getName(): string { return 'greaterThanOrEqual'; @@ -20,7 +19,7 @@ public function getFieldDescription(): string { return 'Greater than or equal to (`>=`).'; } - public function call(Builder $search, object $builder, Property $property, Argument $argument): object { + public function call(Handler $handler, object $builder, Property $property, Argument $argument): object { if (!($builder instanceof EloquentBuilder || $builder instanceof QueryBuilder)) { throw new OperatorUnsupportedBuilder($this, $builder); } diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/GreaterThanOrEqualTest.php b/packages/graphql/src/SearchBy/Operators/Comparison/GreaterThanOrEqualTest.php index 33fc9fcd0..385d73740 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/GreaterThanOrEqualTest.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/GreaterThanOrEqualTest.php @@ -3,10 +3,10 @@ namespace LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Comparison; use Closure; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Builder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\BuilderDataProvider; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use LastDragon_ru\LaraASP\Testing\Providers\ArrayDataProvider; use LastDragon_ru\LaraASP\Testing\Providers\CompositeDataProvider; use Mockery; @@ -38,7 +38,7 @@ public function testCall( ): void { $operator = $this->app->make(GreaterThanOrEqual::class); $argument = $argumentFactory($this); - $search = Mockery::mock(Builder::class); + $search = Mockery::mock(Handler::class); $builder = $builderFactory($this); $builder = $operator->call($search, $builder, $property, $argument); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/GreaterThanTest.php b/packages/graphql/src/SearchBy/Operators/Comparison/GreaterThanTest.php index 32abf0673..06247d2e8 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/GreaterThanTest.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/GreaterThanTest.php @@ -3,10 +3,10 @@ namespace LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Comparison; use Closure; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Builder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\BuilderDataProvider; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use LastDragon_ru\LaraASP\Testing\Providers\ArrayDataProvider; use LastDragon_ru\LaraASP\Testing\Providers\CompositeDataProvider; use Mockery; @@ -38,7 +38,7 @@ public function testCall( ): void { $operator = $this->app->make(GreaterThan::class); $argument = $argumentFactory($this); - $search = Mockery::mock(Builder::class); + $search = Mockery::mock(Handler::class); $builder = $builderFactory($this); $builder = $operator->call($search, $builder, $property, $argument); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/In.php b/packages/graphql/src/SearchBy/Operators/Comparison/In.php index d4a746add..3fe2ce7b5 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/In.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/In.php @@ -4,11 +4,11 @@ use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Query\Builder as QueryBuilder; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Builder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\OperatorUnsupportedBuilder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\TypeProvider; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\OperatorUnsupportedBuilder; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\BaseOperator; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use Nuwave\Lighthouse\Execution\Arguments\Argument; class In extends BaseOperator { @@ -24,7 +24,7 @@ public function getFieldType(TypeProvider $provider, string $type): ?string { return "[{$type}!]"; } - public function call(Builder $search, object $builder, Property $property, Argument $argument): object { + public function call(Handler $handler, object $builder, Property $property, Argument $argument): object { if (!($builder instanceof EloquentBuilder || $builder instanceof QueryBuilder)) { throw new OperatorUnsupportedBuilder($this, $builder); } diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/InTest.php b/packages/graphql/src/SearchBy/Operators/Comparison/InTest.php index e4bf5f4f7..0354178f0 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/InTest.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/InTest.php @@ -3,10 +3,10 @@ namespace LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Comparison; use Closure; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Builder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\BuilderDataProvider; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use LastDragon_ru\LaraASP\Testing\Providers\ArrayDataProvider; use LastDragon_ru\LaraASP\Testing\Providers\CompositeDataProvider; use Mockery; @@ -38,7 +38,7 @@ public function testCall( ): void { $operator = $this->app->make(In::class); $argument = $argumentFactory($this); - $search = Mockery::mock(Builder::class); + $search = Mockery::mock(Handler::class); $builder = $builderFactory($this); $builder = $operator->call($search, $builder, $property, $argument); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/IsNotNull.php b/packages/graphql/src/SearchBy/Operators/Comparison/IsNotNull.php index 6a2fbebb4..daabd1c9d 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/IsNotNull.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/IsNotNull.php @@ -4,12 +4,12 @@ use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Query\Builder as QueryBuilder; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Builder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\OperatorUnsupportedBuilder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\TypeProvider; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\OperatorUnsupportedBuilder; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\BaseOperator; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Types\Flag; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use Nuwave\Lighthouse\Execution\Arguments\Argument; /** @@ -28,7 +28,7 @@ public function getFieldType(TypeProvider $provider, string $type): ?string { return $provider->getType(Flag::class); } - public function call(Builder $search, object $builder, Property $property, Argument $argument): object { + public function call(Handler $handler, object $builder, Property $property, Argument $argument): object { if (!($builder instanceof EloquentBuilder || $builder instanceof QueryBuilder)) { throw new OperatorUnsupportedBuilder($this, $builder); } diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/IsNotNullTest.php b/packages/graphql/src/SearchBy/Operators/Comparison/IsNotNullTest.php index 2faa7be35..c3ada3d76 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/IsNotNullTest.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/IsNotNullTest.php @@ -3,10 +3,10 @@ namespace LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Comparison; use Closure; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Builder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\BuilderDataProvider; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use LastDragon_ru\LaraASP\Testing\Providers\ArrayDataProvider; use LastDragon_ru\LaraASP\Testing\Providers\CompositeDataProvider; use Mockery; @@ -38,7 +38,7 @@ public function testCall( ): void { $operator = $this->app->make(IsNotNull::class); $argument = $argumentFactory($this); - $search = Mockery::mock(Builder::class); + $search = Mockery::mock(Handler::class); $builder = $builderFactory($this); $builder = $operator->call($search, $builder, $property, $argument); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/IsNull.php b/packages/graphql/src/SearchBy/Operators/Comparison/IsNull.php index 98190f47d..61abd36bd 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/IsNull.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/IsNull.php @@ -4,12 +4,12 @@ use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Query\Builder as QueryBuilder; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Builder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\OperatorUnsupportedBuilder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\TypeProvider; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\OperatorUnsupportedBuilder; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\BaseOperator; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Types\Flag; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use Nuwave\Lighthouse\Execution\Arguments\Argument; /** @@ -28,7 +28,7 @@ public function getFieldType(TypeProvider $provider, string $type): ?string { return $provider->getType(Flag::class); } - public function call(Builder $search, object $builder, Property $property, Argument $argument): object { + public function call(Handler $handler, object $builder, Property $property, Argument $argument): object { if (!($builder instanceof EloquentBuilder || $builder instanceof QueryBuilder)) { throw new OperatorUnsupportedBuilder($this, $builder); } diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/IsNullTest.php b/packages/graphql/src/SearchBy/Operators/Comparison/IsNullTest.php index 2e6b513a8..919a3f525 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/IsNullTest.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/IsNullTest.php @@ -3,10 +3,10 @@ namespace LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Comparison; use Closure; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Builder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\BuilderDataProvider; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use LastDragon_ru\LaraASP\Testing\Providers\ArrayDataProvider; use LastDragon_ru\LaraASP\Testing\Providers\CompositeDataProvider; use Mockery; @@ -38,7 +38,7 @@ public function testCall( ): void { $operator = $this->app->make(IsNull::class); $argument = $argumentFactory($this); - $search = Mockery::mock(Builder::class); + $search = Mockery::mock(Handler::class); $builder = $builderFactory($this); $builder = $operator->call($search, $builder, $property, $argument); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/LessThan.php b/packages/graphql/src/SearchBy/Operators/Comparison/LessThan.php index 378017862..581679ca3 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/LessThan.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/LessThan.php @@ -4,10 +4,10 @@ use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Query\Builder as QueryBuilder; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Builder; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\OperatorUnsupportedBuilder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\OperatorUnsupportedBuilder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\BaseOperator; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use Nuwave\Lighthouse\Execution\Arguments\Argument; class LessThan extends BaseOperator { @@ -19,7 +19,7 @@ public function getFieldDescription(): string { return 'Less than (`<`).'; } - public function call(Builder $search, object $builder, Property $property, Argument $argument): object { + public function call(Handler $handler, object $builder, Property $property, Argument $argument): object { if (!($builder instanceof EloquentBuilder || $builder instanceof QueryBuilder)) { throw new OperatorUnsupportedBuilder($this, $builder); } diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/LessThanOrEqual.php b/packages/graphql/src/SearchBy/Operators/Comparison/LessThanOrEqual.php index 49e58150d..d3795a2f2 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/LessThanOrEqual.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/LessThanOrEqual.php @@ -4,10 +4,10 @@ use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Query\Builder as QueryBuilder; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Builder; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\OperatorUnsupportedBuilder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\OperatorUnsupportedBuilder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\BaseOperator; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use Nuwave\Lighthouse\Execution\Arguments\Argument; class LessThanOrEqual extends BaseOperator { @@ -19,7 +19,7 @@ public function getFieldDescription(): string { return 'Less than or equal to (`<=`).'; } - public function call(Builder $search, object $builder, Property $property, Argument $argument): object { + public function call(Handler $handler, object $builder, Property $property, Argument $argument): object { if (!($builder instanceof EloquentBuilder || $builder instanceof QueryBuilder)) { throw new OperatorUnsupportedBuilder($this, $builder); } diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/LessThanOrEqualTest.php b/packages/graphql/src/SearchBy/Operators/Comparison/LessThanOrEqualTest.php index 214b711ed..4a5feb69c 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/LessThanOrEqualTest.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/LessThanOrEqualTest.php @@ -3,10 +3,10 @@ namespace LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Comparison; use Closure; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Builder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\BuilderDataProvider; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use LastDragon_ru\LaraASP\Testing\Providers\ArrayDataProvider; use LastDragon_ru\LaraASP\Testing\Providers\CompositeDataProvider; use Mockery; @@ -38,7 +38,7 @@ public function testCall( ): void { $operator = $this->app->make(LessThanOrEqual::class); $argument = $argumentFactory($this); - $search = Mockery::mock(Builder::class); + $search = Mockery::mock(Handler::class); $builder = $builderFactory($this); $builder = $operator->call($search, $builder, $property, $argument); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/LessThanTest.php b/packages/graphql/src/SearchBy/Operators/Comparison/LessThanTest.php index fb4d15ad0..1da604333 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/LessThanTest.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/LessThanTest.php @@ -3,10 +3,10 @@ namespace LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Comparison; use Closure; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Builder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\BuilderDataProvider; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use LastDragon_ru\LaraASP\Testing\Providers\ArrayDataProvider; use LastDragon_ru\LaraASP\Testing\Providers\CompositeDataProvider; use Mockery; @@ -38,7 +38,7 @@ public function testCall( ): void { $operator = $this->app->make(LessThan::class); $argument = $argumentFactory($this); - $search = Mockery::mock(Builder::class); + $search = Mockery::mock(Handler::class); $builder = $builderFactory($this); $builder = $operator->call($search, $builder, $property, $argument); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/Like.php b/packages/graphql/src/SearchBy/Operators/Comparison/Like.php index c3f6ea40f..5eb527384 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/Like.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/Like.php @@ -5,10 +5,10 @@ use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Query\Builder as QueryBuilder; use LastDragon_ru\LaraASP\Core\Utils\Cast; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Builder; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\OperatorUnsupportedBuilder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\OperatorUnsupportedBuilder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\BaseOperator; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use Nuwave\Lighthouse\Execution\Arguments\Argument; class Like extends BaseOperator { @@ -20,7 +20,7 @@ public function getFieldDescription(): string { return 'Like.'; } - public function call(Builder $search, object $builder, Property $property, Argument $argument): object { + public function call(Handler $handler, object $builder, Property $property, Argument $argument): object { if (!($builder instanceof EloquentBuilder || $builder instanceof QueryBuilder)) { throw new OperatorUnsupportedBuilder($this, $builder); } diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/LikeTest.php b/packages/graphql/src/SearchBy/Operators/Comparison/LikeTest.php index 6395bb75c..0bc9944a0 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/LikeTest.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/LikeTest.php @@ -3,10 +3,10 @@ namespace LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Comparison; use Closure; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Builder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\BuilderDataProvider; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use LastDragon_ru\LaraASP\Testing\Providers\ArrayDataProvider; use LastDragon_ru\LaraASP\Testing\Providers\CompositeDataProvider; use Mockery; @@ -38,7 +38,7 @@ public function testCall( ): void { $operator = $this->app->make(Like::class); $argument = $argumentFactory($this); - $search = Mockery::mock(Builder::class); + $search = Mockery::mock(Handler::class); $builder = $builderFactory($this); $builder = $operator->call($search, $builder, $property, $argument); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/NotBetween.php b/packages/graphql/src/SearchBy/Operators/Comparison/NotBetween.php index 93fee682c..6827a3378 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/NotBetween.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/NotBetween.php @@ -5,9 +5,9 @@ use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Query\Builder as QueryBuilder; use LastDragon_ru\LaraASP\Core\Utils\Cast; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Builder; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\OperatorUnsupportedBuilder; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\OperatorUnsupportedBuilder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use Nuwave\Lighthouse\Execution\Arguments\Argument; class NotBetween extends Between { @@ -19,7 +19,7 @@ public function getFieldDescription(): string { return 'Outside a range.'; } - public function call(Builder $search, object $builder, Property $property, Argument $argument): object { + public function call(Handler $handler, object $builder, Property $property, Argument $argument): object { if (!($builder instanceof EloquentBuilder || $builder instanceof QueryBuilder)) { throw new OperatorUnsupportedBuilder($this, $builder); } diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/NotBetweenTest.php b/packages/graphql/src/SearchBy/Operators/Comparison/NotBetweenTest.php index 70ec9da51..aa1645e4b 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/NotBetweenTest.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/NotBetweenTest.php @@ -3,10 +3,10 @@ namespace LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Comparison; use Closure; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Builder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\BuilderDataProvider; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use LastDragon_ru\LaraASP\Testing\Providers\ArrayDataProvider; use LastDragon_ru\LaraASP\Testing\Providers\CompositeDataProvider; use Mockery; @@ -38,7 +38,7 @@ public function testCall( ): void { $operator = $this->app->make(NotBetween::class); $argument = $argumentFactory($this); - $search = Mockery::mock(Builder::class); + $search = Mockery::mock(Handler::class); $builder = $builderFactory($this); $builder = $operator->call($search, $builder, $property, $argument); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/NotEqual.php b/packages/graphql/src/SearchBy/Operators/Comparison/NotEqual.php index ecd80af54..88cf1191f 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/NotEqual.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/NotEqual.php @@ -4,10 +4,10 @@ use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Query\Builder as QueryBuilder; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Builder; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\OperatorUnsupportedBuilder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\OperatorUnsupportedBuilder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\BaseOperator; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use Nuwave\Lighthouse\Execution\Arguments\Argument; class NotEqual extends BaseOperator { @@ -19,7 +19,7 @@ public function getFieldDescription(): string { return 'Not Equal (`!=`).'; } - public function call(Builder $search, object $builder, Property $property, Argument $argument): object { + public function call(Handler $handler, object $builder, Property $property, Argument $argument): object { if (!($builder instanceof EloquentBuilder || $builder instanceof QueryBuilder)) { throw new OperatorUnsupportedBuilder($this, $builder); } diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/NotEqualTest.php b/packages/graphql/src/SearchBy/Operators/Comparison/NotEqualTest.php index 9c4e5fb35..bdd7f180a 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/NotEqualTest.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/NotEqualTest.php @@ -3,10 +3,10 @@ namespace LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Comparison; use Closure; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Builder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\BuilderDataProvider; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use LastDragon_ru\LaraASP\Testing\Providers\ArrayDataProvider; use LastDragon_ru\LaraASP\Testing\Providers\CompositeDataProvider; use Mockery; @@ -38,7 +38,7 @@ public function testCall( ): void { $operator = $this->app->make(NotEqual::class); $argument = $argumentFactory($this); - $search = Mockery::mock(Builder::class); + $search = Mockery::mock(Handler::class); $builder = $builderFactory($this); $builder = $operator->call($search, $builder, $property, $argument); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/NotIn.php b/packages/graphql/src/SearchBy/Operators/Comparison/NotIn.php index 2435b885c..eeb35bc7f 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/NotIn.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/NotIn.php @@ -4,11 +4,11 @@ use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Query\Builder as QueryBuilder; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Builder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\OperatorUnsupportedBuilder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\TypeProvider; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\OperatorUnsupportedBuilder; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\BaseOperator; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use Nuwave\Lighthouse\Execution\Arguments\Argument; class NotIn extends BaseOperator { @@ -24,7 +24,7 @@ public function getFieldType(TypeProvider $provider, string $type): ?string { return "[{$type}!]"; } - public function call(Builder $search, object $builder, Property $property, Argument $argument): object { + public function call(Handler $handler, object $builder, Property $property, Argument $argument): object { if (!($builder instanceof EloquentBuilder || $builder instanceof QueryBuilder)) { throw new OperatorUnsupportedBuilder($this, $builder); } diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/NotInTest.php b/packages/graphql/src/SearchBy/Operators/Comparison/NotInTest.php index be8af0f76..80c5c2d5c 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/NotInTest.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/NotInTest.php @@ -3,10 +3,10 @@ namespace LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Comparison; use Closure; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Builder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\BuilderDataProvider; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use LastDragon_ru\LaraASP\Testing\Providers\ArrayDataProvider; use LastDragon_ru\LaraASP\Testing\Providers\CompositeDataProvider; use Mockery; @@ -38,7 +38,7 @@ public function testCall( ): void { $operator = $this->app->make(NotIn::class); $argument = $argumentFactory($this); - $search = Mockery::mock(Builder::class); + $search = Mockery::mock(Handler::class); $builder = $builderFactory($this); $builder = $operator->call($search, $builder, $property, $argument); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/NotLike.php b/packages/graphql/src/SearchBy/Operators/Comparison/NotLike.php index fd68f6bd2..69ee59cab 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/NotLike.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/NotLike.php @@ -5,10 +5,10 @@ use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Query\Builder as QueryBuilder; use LastDragon_ru\LaraASP\Core\Utils\Cast; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Builder; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\OperatorUnsupportedBuilder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\OperatorUnsupportedBuilder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\BaseOperator; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use Nuwave\Lighthouse\Execution\Arguments\Argument; class NotLike extends BaseOperator { @@ -20,7 +20,7 @@ public function getFieldDescription(): string { return 'Not like.'; } - public function call(Builder $search, object $builder, Property $property, Argument $argument): object { + public function call(Handler $handler, object $builder, Property $property, Argument $argument): object { if (!($builder instanceof EloquentBuilder || $builder instanceof QueryBuilder)) { throw new OperatorUnsupportedBuilder($this, $builder); } diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/NotLikeTest.php b/packages/graphql/src/SearchBy/Operators/Comparison/NotLikeTest.php index 91ab83457..440859b53 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/NotLikeTest.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/NotLikeTest.php @@ -3,10 +3,10 @@ namespace LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Comparison; use Closure; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Builder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\BuilderDataProvider; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use LastDragon_ru\LaraASP\Testing\Providers\ArrayDataProvider; use LastDragon_ru\LaraASP\Testing\Providers\CompositeDataProvider; use Mockery; @@ -38,7 +38,7 @@ public function testCall( ): void { $operator = $this->app->make(NotLike::class); $argument = $argumentFactory($this); - $search = Mockery::mock(Builder::class); + $search = Mockery::mock(Handler::class); $builder = $builderFactory($this); $builder = $operator->call($search, $builder, $property, $argument); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/StartsWithTest.php b/packages/graphql/src/SearchBy/Operators/Comparison/StartsWithTest.php index 30cd526ff..59160468a 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/StartsWithTest.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/StartsWithTest.php @@ -3,10 +3,10 @@ namespace LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Comparison; use Closure; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Builder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\BuilderDataProvider; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use LastDragon_ru\LaraASP\Testing\Providers\ArrayDataProvider; use LastDragon_ru\LaraASP\Testing\Providers\CompositeDataProvider; use Mockery; @@ -38,7 +38,7 @@ public function testCall( ): void { $operator = $this->app->make(StartsWith::class); $argument = $argumentFactory($this); - $search = Mockery::mock(Builder::class); + $search = Mockery::mock(Handler::class); $builder = $builderFactory($this); $builder = $operator->call($search, $builder, $property, $argument); diff --git a/packages/graphql/src/SearchBy/Operators/Complex/Relation.php b/packages/graphql/src/SearchBy/Operators/Complex/Relation.php index d0c35a45f..f87669945 100644 --- a/packages/graphql/src/SearchBy/Operators/Complex/Relation.php +++ b/packages/graphql/src/SearchBy/Operators/Complex/Relation.php @@ -10,14 +10,14 @@ use GraphQL\Type\Definition\InputObjectType; use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use LastDragon_ru\LaraASP\Eloquent\ModelHelper; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\OperatorUnsupportedBuilder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Ast\Manipulator; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Builder; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\ComplexOperator; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Directives\Directive; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\OperatorInvalidArgumentValue; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\OperatorUnsupportedBuilder; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\BaseOperator; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use Nuwave\Lighthouse\Execution\Arguments\Argument; use Nuwave\Lighthouse\Execution\Arguments\ArgumentSet; @@ -80,7 +80,7 @@ public function isBuilderSupported(object $builder): bool { return $builder instanceof EloquentBuilder; } - public function call(Builder $search, object $builder, Property $property, Argument $argument): object { + public function call(Handler $handler, object $builder, Property $property, Argument $argument): object { // Supported? if (!($builder instanceof EloquentBuilder)) { throw new OperatorUnsupportedBuilder($this, $builder); @@ -110,7 +110,7 @@ public function call(Builder $search, object $builder, Property $property, Argum if ($hasCount instanceof Argument) { $query = $builder->toBase()->newQuery(); - $query = $search->where($query, $hasCount, new Property('tmp')); + $query = $handler->handle($query, $hasCount, new Property('tmp')); $where = reset($query->wheres); $count = $where['value'] ?? $count; $operator = $where['operator'] ?? $operator; @@ -127,13 +127,13 @@ public function call(Builder $search, object $builder, Property $property, Argum $property, $operator, $count, - static function (EloquentBuilder $builder) use ($relation, $search, $alias, $has): void { + static function (EloquentBuilder $builder) use ($relation, $handler, $alias, $has): void { if ($alias === $relation->getRelationCountHash(false)) { $alias = $builder->getModel()->getTable(); } if ($has instanceof Argument && $has->value instanceof ArgumentSet) { - $search->where($builder, $has->value, new Property($alias)); + $handler->handle($builder, $has->value, new Property($alias)); } }, ); diff --git a/packages/graphql/src/SearchBy/Operators/Complex/RelationTest.php b/packages/graphql/src/SearchBy/Operators/Complex/RelationTest.php index 6c8c2b15b..88b78bd94 100644 --- a/packages/graphql/src/SearchBy/Operators/Complex/RelationTest.php +++ b/packages/graphql/src/SearchBy/Operators/Complex/RelationTest.php @@ -6,12 +6,12 @@ use Exception; use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use LastDragon_ru\LaraASP\Eloquent\Exceptions\PropertyIsNotRelation; +use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\Client\ConditionTooManyOperators; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Directives\Directive; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\Client\SearchConditionTooManyOperators; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\BuilderDataProvider; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\Models\User; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use Nuwave\Lighthouse\Execution\Arguments\Argument; use function is_array; @@ -217,7 +217,7 @@ static function (self $test) use ($graphql): Argument { }, ], '{count: { multiple operators }}' => [ - new SearchConditionTooManyOperators(['lessThan', 'equal']), + new ConditionTooManyOperators(['lessThan', 'equal']), static function (): EloquentBuilder { return User::query(); }, diff --git a/packages/graphql/src/SearchBy/Operators/Logical/AllOfTest.php b/packages/graphql/src/SearchBy/Operators/Logical/AllOfTest.php index b2b36d8ce..75d066110 100644 --- a/packages/graphql/src/SearchBy/Operators/Logical/AllOfTest.php +++ b/packages/graphql/src/SearchBy/Operators/Logical/AllOfTest.php @@ -3,10 +3,10 @@ namespace LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Logical; use Closure; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Directives\Directive; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\BuilderDataProvider; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use LastDragon_ru\LaraASP\Testing\Providers\ArrayDataProvider; use LastDragon_ru\LaraASP\Testing\Providers\CompositeDataProvider; use Nuwave\Lighthouse\Execution\Arguments\Argument; diff --git a/packages/graphql/src/SearchBy/Operators/Logical/AnyOfTest.php b/packages/graphql/src/SearchBy/Operators/Logical/AnyOfTest.php index 24b94586e..1d8d851ce 100644 --- a/packages/graphql/src/SearchBy/Operators/Logical/AnyOfTest.php +++ b/packages/graphql/src/SearchBy/Operators/Logical/AnyOfTest.php @@ -3,10 +3,10 @@ namespace LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Logical; use Closure; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Directives\Directive; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\BuilderDataProvider; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use LastDragon_ru\LaraASP\Testing\Providers\ArrayDataProvider; use LastDragon_ru\LaraASP\Testing\Providers\CompositeDataProvider; use Nuwave\Lighthouse\Execution\Arguments\Argument; diff --git a/packages/graphql/src/SearchBy/Operators/Logical/Logical.php b/packages/graphql/src/SearchBy/Operators/Logical/Logical.php index 757a76c93..9a67ac04c 100644 --- a/packages/graphql/src/SearchBy/Operators/Logical/Logical.php +++ b/packages/graphql/src/SearchBy/Operators/Logical/Logical.php @@ -4,19 +4,20 @@ use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Query\Builder as QueryBuilder; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Builder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\OperatorUnsupportedBuilder; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\OperatorInvalidArgumentValue; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\OperatorUnsupportedBuilder; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\BaseOperator; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use Nuwave\Lighthouse\Execution\Arguments\Argument; use Nuwave\Lighthouse\Execution\Arguments\ArgumentSet; + use function array_filter; use function count; use function is_array; abstract class Logical extends BaseOperator { - public function call(Builder $search, object $builder, Property $property, Argument $argument): object { + public function call(Handler $handler, object $builder, Property $property, Argument $argument): object { if (!($builder instanceof EloquentBuilder || $builder instanceof QueryBuilder)) { throw new OperatorUnsupportedBuilder($this, $builder); } @@ -28,11 +29,11 @@ public function call(Builder $search, object $builder, Property $property, Argum foreach ($conditions as $arguments) { $builder->where( static function (EloquentBuilder|QueryBuilder $builder) use ( - $search, + $handler, $arguments, $property ): void { - $search->where($builder, $arguments, $property); + $handler->handle($builder, $arguments, $property); }, null, null, diff --git a/packages/graphql/src/SearchBy/Operators/Logical/NotTest.php b/packages/graphql/src/SearchBy/Operators/Logical/NotTest.php index c43a0fb37..626a68419 100644 --- a/packages/graphql/src/SearchBy/Operators/Logical/NotTest.php +++ b/packages/graphql/src/SearchBy/Operators/Logical/NotTest.php @@ -3,10 +3,10 @@ namespace LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Logical; use Closure; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Directives\Directive; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\BuilderDataProvider; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; -use LastDragon_ru\LaraASP\GraphQL\Utils\Property; use LastDragon_ru\LaraASP\Testing\Providers\ArrayDataProvider; use LastDragon_ru\LaraASP\Testing\Providers\CompositeDataProvider; use Nuwave\Lighthouse\Execution\Arguments\Argument; diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 31507a45e..e1af4541f 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -30,6 +30,11 @@ parameters: count: 1 path: packages/graphql/src/AstManipulator.php + - + message: "#^Property LastDragon_ru\\\\LaraASP\\\\GraphQL\\\\Builder\\\\Property\\:\\:\\$path \\(array\\\\) does not accept array\\\\.$#" + count: 1 + path: packages/graphql/src/Builder/Property.php + - message: "#^Parameter \\#1 \\$enum of class LastDragon_ru\\\\LaraASP\\\\GraphQL\\\\Utils\\\\Enum\\\\EnumType constructor expects class\\-string\\, mixed given\\.$#" count: 1 @@ -100,11 +105,6 @@ parameters: count: 1 path: packages/graphql/src/Testing/Package/Models/Relations/Unsupported.php - - - message: "#^Property LastDragon_ru\\\\LaraASP\\\\GraphQL\\\\Utils\\\\Property\\:\\:\\$path \\(array\\\\) does not accept array\\\\.$#" - count: 1 - path: packages/graphql/src/Utils/Property.php - - message: "#^Parameter \\#1 \\$expression of method Illuminate\\\\Console\\\\Scheduling\\\\Event\\:\\:cron\\(\\) expects string, mixed given\\.$#" count: 1 From 90fd6fda0fbed1b191fbce814f924b25da9c8e7b Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 25 Jun 2022 15:17:59 +0400 Subject: [PATCH 03/13] Properties handling moved to new `Property` operator directive. --- .../graphql/src/Builder/Contracts/Handler.php | 3 +- .../Builder/Directives/HandlerDirective.php | 95 +++++++------------ .../Builder/Directives/PropertyDirective.php | 45 +++++++++ packages/graphql/src/Builder/Property.php | 2 +- .../graphql/src/SearchBy/Ast/Manipulator.php | 21 ++-- .../Definitions/SearchByPropertyDirective.php | 11 +++ .../src/SearchBy/Directives/DirectiveTest.php | 4 +- ...~custom-complex-operators-expected.graphql | 5 + .../DirectiveTest~example-expected.graphql | 8 ++ .../DirectiveTest~full-expected.graphql | 18 ++++ ...tiveTest~programmatically-expected.graphql | 8 ++ .../DirectiveTest~usedonly-expected.graphql | 5 + .../FailedToCreateSearchConditionForField.php | 29 ------ .../SearchBy/Operators/Comparison/Between.php | 2 +- .../Operators/Comparison/BetweenTest.php | 1 + .../Operators/Comparison/Contains.php | 2 +- .../Operators/Comparison/ContainsTest.php | 1 + .../Operators/Comparison/EndsWithTest.php | 1 + .../SearchBy/Operators/Comparison/Equal.php | 2 +- .../Operators/Comparison/EqualTest.php | 1 + .../Operators/Comparison/GreaterThan.php | 2 +- .../Comparison/GreaterThanOrEqual.php | 2 +- .../Comparison/GreaterThanOrEqualTest.php | 1 + .../Operators/Comparison/GreaterThanTest.php | 1 + .../src/SearchBy/Operators/Comparison/In.php | 2 +- .../SearchBy/Operators/Comparison/InTest.php | 1 + .../Operators/Comparison/IsNotNull.php | 2 +- .../Operators/Comparison/IsNotNullTest.php | 1 + .../SearchBy/Operators/Comparison/IsNull.php | 2 +- .../Operators/Comparison/IsNullTest.php | 1 + .../Operators/Comparison/LessThan.php | 2 +- .../Operators/Comparison/LessThanOrEqual.php | 2 +- .../Comparison/LessThanOrEqualTest.php | 1 + .../Operators/Comparison/LessThanTest.php | 1 + .../SearchBy/Operators/Comparison/Like.php | 2 +- .../Operators/Comparison/LikeTest.php | 1 + .../Operators/Comparison/NotBetween.php | 2 +- .../Operators/Comparison/NotBetweenTest.php | 1 + .../Operators/Comparison/NotEqual.php | 2 +- .../Operators/Comparison/NotEqualTest.php | 1 + .../SearchBy/Operators/Comparison/NotIn.php | 2 +- .../Operators/Comparison/NotInTest.php | 1 + .../SearchBy/Operators/Comparison/NotLike.php | 2 +- .../Operators/Comparison/NotLikeTest.php | 1 + .../Operators/Comparison/StartsWithTest.php | 1 + .../SearchBy/Operators/Complex/Relation.php | 12 ++- .../Operators/Complex/RelationTest.php | 2 + .../SearchBy/Operators/Logical/AllOfTest.php | 3 + .../SearchBy/Operators/Logical/AnyOfTest.php | 3 + .../SearchBy/Operators/Logical/Logical.php | 2 +- .../SearchBy/Operators/Logical/NotTest.php | 3 + .../src/SearchBy/Operators/Property.php | 29 ++++++ 52 files changed, 234 insertions(+), 121 deletions(-) create mode 100644 packages/graphql/src/Builder/Directives/PropertyDirective.php create mode 100644 packages/graphql/src/SearchBy/Definitions/SearchByPropertyDirective.php delete mode 100644 packages/graphql/src/SearchBy/Exceptions/FailedToCreateSearchConditionForField.php create mode 100644 packages/graphql/src/SearchBy/Operators/Property.php diff --git a/packages/graphql/src/Builder/Contracts/Handler.php b/packages/graphql/src/Builder/Contracts/Handler.php index 487489822..979d9edb8 100644 --- a/packages/graphql/src/Builder/Contracts/Handler.php +++ b/packages/graphql/src/Builder/Contracts/Handler.php @@ -3,7 +3,6 @@ namespace LastDragon_ru\LaraASP\GraphQL\Builder\Contracts; use LastDragon_ru\LaraASP\GraphQL\Builder\Property; -use Nuwave\Lighthouse\Execution\Arguments\Argument; use Nuwave\Lighthouse\Execution\Arguments\ArgumentSet; interface Handler { @@ -14,5 +13,5 @@ interface Handler { * * @return TBuilder */ - public function handle(object $builder, ArgumentSet|Argument $conditions, Property $parent = null): object; + public function handle(object $builder, Property $property, ArgumentSet $conditions): object; } diff --git a/packages/graphql/src/Builder/Directives/HandlerDirective.php b/packages/graphql/src/Builder/Directives/HandlerDirective.php index cd695cb5f..581ba78a4 100644 --- a/packages/graphql/src/Builder/Directives/HandlerDirective.php +++ b/packages/graphql/src/Builder/Directives/HandlerDirective.php @@ -12,14 +12,12 @@ use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\OperatorUnsupportedBuilder; use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\Utils\ArgumentFactory; -use Nuwave\Lighthouse\Execution\Arguments\Argument; use Nuwave\Lighthouse\Execution\Arguments\ArgumentSet; use Nuwave\Lighthouse\Schema\Directives\BaseDirective; use Nuwave\Lighthouse\Support\Utils; use function array_keys; use function count; -use function reset; abstract class HandlerDirective extends BaseDirective implements Handler { public function __construct( @@ -44,7 +42,7 @@ protected function handleAnyBuilder(object $builder, mixed $value): object { $argument = $this->getFactory()->getArgument($this->definitionNode, $value); if ($argument->value instanceof ArgumentSet) { - $builder = $this->handle($builder, $argument->value); + $builder = $this->handle($builder, new Property(), $argument->value); } else { throw new HandlerInvalidConditions($this); } @@ -60,43 +58,19 @@ protected function handleAnyBuilder(object $builder, mixed $value): object { * * @return T */ - public function handle(object $builder, ArgumentSet|Argument $conditions, Property $parent = null): object { - // Prepare - $parent ??= new Property(); - $conditions = $conditions instanceof Argument - ? $conditions->value - : $conditions; - $arguments = $conditions instanceof ArgumentSet - ? $conditions->arguments - : []; - + public function handle(object $builder, Property $property, ArgumentSet $conditions): object { // Empty? - if (count($arguments) === 0) { + if (count($conditions->arguments) === 0) { return $builder; } - // Property or Operator? - $first = reset($arguments); - $isProperty = $first->directives->filter(Utils::instanceofMatcher(Operator::class))->isEmpty(); - - if ($isProperty) { - // Valid? - if (count($arguments) !== 1) { - throw new ConditionTooManyProperties(array_keys($arguments)); - } - - // Process - foreach ($arguments as $name => $argument) { - $parent = $parent->getChild($name); - $builder = $this->call($builder, $parent, $argument); - } - } elseif ($conditions instanceof ArgumentSet || $conditions instanceof Argument) { - $builder = $this->call($builder, $parent, $conditions); - } else { - throw new HandlerInvalidConditions($this); + // Valid? + if (count($conditions->arguments) !== 1) { + throw new ConditionTooManyProperties(array_keys($conditions->arguments)); } - return $builder; + // Call + return $this->call($builder, $property, $conditions); } /** @@ -106,40 +80,35 @@ public function handle(object $builder, ArgumentSet|Argument $conditions, Proper * * @return T */ - protected function call(object $builder, Property $property, ArgumentSet|Argument $operator): object { + protected function call(object $builder, Property $property, ArgumentSet $operator): object { // Operator & Value /** @var Operator|null $op */ - $op = null; - $value = null; - $filter = Utils::instanceofMatcher(Operator::class); - $operator = $operator instanceof Argument - ? $operator->value - : $operator; - - if ($operator instanceof ArgumentSet) { - if (count($operator->arguments) > 1) { + $op = null; + $value = null; + $filter = Utils::instanceofMatcher(Operator::class); + + if (count($operator->arguments) > 1) { + throw new ConditionTooManyOperators( + array_keys($operator->arguments), + ); + } + + foreach ($operator->arguments as $name => $argument) { + /** @var Collection $operators */ + $operators = $argument->directives->filter($filter); + $property = $property->getChild($name); + $value = $argument; + $op = $operators->first(); + + if (count($operators) > 1) { throw new ConditionTooManyOperators( - array_keys($operator->arguments), + $operators + ->map(static function (Operator $operator): string { + return $operator::getName(); + }) + ->all(), ); } - - foreach ($operator->arguments as $argument) { - /** @var Collection $operators */ - $operators = $argument->directives->filter($filter); - - if (count($operators) === 1) { - $op = $operators->first(); - $value = $argument; - } else { - throw new ConditionTooManyOperators( - $operators - ->map(static function (Operator $operator): string { - return $operator::getName(); - }) - ->all(), - ); - } - } } // Operator? diff --git a/packages/graphql/src/Builder/Directives/PropertyDirective.php b/packages/graphql/src/Builder/Directives/PropertyDirective.php new file mode 100644 index 000000000..5b4fdda25 --- /dev/null +++ b/packages/graphql/src/Builder/Directives/PropertyDirective.php @@ -0,0 +1,45 @@ +value instanceof ArgumentSet)) { + throw new HandlerInvalidConditions($handler); + } + + // Empty? + if (count($argument->value->arguments) === 0) { + throw new ConditionEmpty(); + } + + // Valid? + if (count($argument->value->arguments) > 1) { + throw new ConditionTooManyOperators( + array_keys($argument->value->arguments), + ); + } + + // Apply + return $handler->handle($builder, $property, $argument->value); + } +} diff --git a/packages/graphql/src/Builder/Property.php b/packages/graphql/src/Builder/Property.php index 9c4992395..4757c6d94 100644 --- a/packages/graphql/src/Builder/Property.php +++ b/packages/graphql/src/Builder/Property.php @@ -36,7 +36,7 @@ public function getChild(string $name): static { } public function getParent(): static { - $path = array_slice($this->path, -1); + $path = array_slice($this->path, 0, -1); $parent = new static(...$path); return $parent; diff --git a/packages/graphql/src/SearchBy/Ast/Manipulator.php b/packages/graphql/src/SearchBy/Ast/Manipulator.php index 634b8da13..11f26a923 100644 --- a/packages/graphql/src/SearchBy/Ast/Manipulator.php +++ b/packages/graphql/src/SearchBy/Ast/Manipulator.php @@ -32,13 +32,13 @@ use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\DefinitionImpossibleToCreateType; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\EnumNoOperators; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\FailedToCreateSearchCondition; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\FailedToCreateSearchConditionForField; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\FakeTypeDefinitionIsNotFake; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\FakeTypeDefinitionUnknown; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\InputFieldAlreadyDefined; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\NotImplemented; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\ScalarNoOperators; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Complex\Relation; +use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Property; use Nuwave\Lighthouse\Schema\AST\DocumentAST; use Nuwave\Lighthouse\Schema\DirectiveLocator; use Nuwave\Lighthouse\Schema\TypeRegistry; @@ -47,6 +47,7 @@ use function array_shift; use function count; use function implode; +use function is_string; class Manipulator extends AstManipulator implements TypeProvider { protected Metadata $metadata; @@ -137,8 +138,8 @@ public function getInputType(InputObjectTypeDefinitionNode|InputObjectType $node ); // Add searchable fields - $description = 'Property condition.'; - $fields = $node instanceof InputObjectType + $property = $this->metadata->getOperatorInstance(Property::class); + $fields = $node instanceof InputObjectType ? $node->getFields() : $node->fields; @@ -188,12 +189,18 @@ public function getInputType(InputObjectTypeDefinitionNode|InputObjectType $node } // Create new Field + if (is_string($fieldDefinition)) { + $fieldDefinition = Parser::inputValueDefinition( + $this->getOperatorField( + $property, + $this->getTypeDefinitionNode($fieldDefinition), + $this->getNodeName($field), + ), + ); + } + if ($fieldDefinition instanceof InputValueDefinitionNode) { $type->fields[] = $fieldDefinition; - } elseif ($fieldDefinition) { - if (!$this->copyFieldToType($type, $field, $fieldDefinition, $description)) { - throw new FailedToCreateSearchConditionForField($this->getNodeName($node), $fieldName); - } } else { throw new NotImplemented($fieldType); } diff --git a/packages/graphql/src/SearchBy/Definitions/SearchByPropertyDirective.php b/packages/graphql/src/SearchBy/Definitions/SearchByPropertyDirective.php new file mode 100644 index 000000000..b02db6419 --- /dev/null +++ b/packages/graphql/src/SearchBy/Definitions/SearchByPropertyDirective.php @@ -0,0 +1,11 @@ +app->make(DirectiveLocator::class); - $directive = new class() extends Relation { + $property = $test->app->make(Property::class); + $directive = new class($property) extends Relation { public static function getName(): string { return 'custom'; } diff --git a/packages/graphql/src/SearchBy/Directives/DirectiveTest~custom-complex-operators-expected.graphql b/packages/graphql/src/SearchBy/Directives/DirectiveTest~custom-complex-operators-expected.graphql index 99d34c8f9..807a1a797 100644 --- a/packages/graphql/src/SearchBy/Directives/DirectiveTest~custom-complex-operators-expected.graphql +++ b/packages/graphql/src/SearchBy/Directives/DirectiveTest~custom-complex-operators-expected.graphql @@ -86,6 +86,7 @@ input SearchByConditionChild { Property condition. """ value: SearchByScalarStringOrNull + @searchByProperty } """ @@ -376,3 +377,7 @@ on directive @searchByOperatorStartsWith on | INPUT_FIELD_DEFINITION + +directive @searchByProperty +on + | INPUT_FIELD_DEFINITION diff --git a/packages/graphql/src/SearchBy/Directives/DirectiveTest~example-expected.graphql b/packages/graphql/src/SearchBy/Directives/DirectiveTest~example-expected.graphql index c827a0264..3c956c51a 100644 --- a/packages/graphql/src/SearchBy/Directives/DirectiveTest~example-expected.graphql +++ b/packages/graphql/src/SearchBy/Directives/DirectiveTest~example-expected.graphql @@ -51,6 +51,7 @@ input SearchByConditionCommentsQuery { Property condition. """ date: SearchByScalarDateOrNull + @searchByProperty """ Not. @@ -62,6 +63,7 @@ input SearchByConditionCommentsQuery { Property condition. """ text: SearchByScalarString + @searchByProperty """ Relationship condition. @@ -90,11 +92,13 @@ input SearchByConditionUsersQuery { Property condition. """ id: SearchByScalarID + @searchByProperty """ Property condition. """ name: SearchByScalarString + @searchByProperty """ Not. @@ -398,3 +402,7 @@ on directive @searchByOperatorStartsWith on | INPUT_FIELD_DEFINITION + +directive @searchByProperty +on + | INPUT_FIELD_DEFINITION diff --git a/packages/graphql/src/SearchBy/Directives/DirectiveTest~full-expected.graphql b/packages/graphql/src/SearchBy/Directives/DirectiveTest~full-expected.graphql index 8d178028a..c8f6d3d40 100644 --- a/packages/graphql/src/SearchBy/Directives/DirectiveTest~full-expected.graphql +++ b/packages/graphql/src/SearchBy/Directives/DirectiveTest~full-expected.graphql @@ -114,51 +114,61 @@ input SearchByConditionInputA { Property condition. """ booleanScalar: SearchByScalarBooleanOrNull + @searchByProperty """ Property condition. """ booleanScalarNotNull: SearchByScalarBoolean + @searchByProperty """ Property condition. """ enum: SearchByEnumEnumAOrNull + @searchByProperty """ Property condition. """ enumNotNull: SearchByEnumEnumA + @searchByProperty """ Property condition. """ floatScalar: SearchByScalarFloatOrNull + @searchByProperty """ Property condition. """ floatScalarNotNull: SearchByScalarFloat + @searchByProperty """ Property condition. """ idScalar: SearchByScalarIDOrNull + @searchByProperty """ Property condition. """ idScalarNotNull: SearchByScalarID + @searchByProperty """ Property condition. """ inScalarNotNull: SearchByScalarInt + @searchByProperty """ Property condition. """ intScalar: SearchByScalarIntOrNull + @searchByProperty """ Relationship condition. @@ -182,11 +192,13 @@ input SearchByConditionInputA { Property condition. """ stringScalar: SearchByScalarStringOrNull + @searchByProperty """ Property condition. """ stringScalarNotNull: SearchByScalarString + @searchByProperty } """ @@ -209,6 +221,7 @@ input SearchByConditionInputB { Property condition. """ id: SearchByScalarIDOrNull + @searchByProperty """ Not. @@ -249,6 +262,7 @@ input SearchByConditionNestedA { Property condition. """ value: SearchByScalarStringOrNull + @searchByProperty } """ @@ -1023,3 +1037,7 @@ on directive @searchByOperatorStartsWith on | INPUT_FIELD_DEFINITION + +directive @searchByProperty +on + | INPUT_FIELD_DEFINITION diff --git a/packages/graphql/src/SearchBy/Directives/DirectiveTest~programmatically-expected.graphql b/packages/graphql/src/SearchBy/Directives/DirectiveTest~programmatically-expected.graphql index 9ad1cdc3c..3e6fc6252 100644 --- a/packages/graphql/src/SearchBy/Directives/DirectiveTest~programmatically-expected.graphql +++ b/packages/graphql/src/SearchBy/Directives/DirectiveTest~programmatically-expected.graphql @@ -58,11 +58,13 @@ input SearchByConditionTestTypeA { Property condition. """ flag: SearchByScalarBooleanOrNull + @searchByProperty """ Property condition. """ name: SearchByScalarStringOrNull + @searchByProperty """ Not. @@ -74,6 +76,7 @@ input SearchByConditionTestTypeA { Property condition. """ value: SearchByEnumTestEnumOrNull + @searchByProperty } """ @@ -102,6 +105,7 @@ input SearchByConditionTestTypeB { Property condition. """ name: SearchByScalarString + @searchByProperty """ Not. @@ -472,3 +476,7 @@ on directive @searchByOperatorStartsWith on | INPUT_FIELD_DEFINITION + +directive @searchByProperty +on + | INPUT_FIELD_DEFINITION diff --git a/packages/graphql/src/SearchBy/Directives/DirectiveTest~usedonly-expected.graphql b/packages/graphql/src/SearchBy/Directives/DirectiveTest~usedonly-expected.graphql index 759839d03..c80171e2f 100644 --- a/packages/graphql/src/SearchBy/Directives/DirectiveTest~usedonly-expected.graphql +++ b/packages/graphql/src/SearchBy/Directives/DirectiveTest~usedonly-expected.graphql @@ -24,6 +24,7 @@ input SearchByConditionProperties { Property condition. """ value: SearchByScalarString + @searchByProperty } """ @@ -147,3 +148,7 @@ on directive @searchByOperatorStartsWith on | INPUT_FIELD_DEFINITION + +directive @searchByProperty +on + | INPUT_FIELD_DEFINITION diff --git a/packages/graphql/src/SearchBy/Exceptions/FailedToCreateSearchConditionForField.php b/packages/graphql/src/SearchBy/Exceptions/FailedToCreateSearchConditionForField.php deleted file mode 100644 index ea7c13a96..000000000 --- a/packages/graphql/src/SearchBy/Exceptions/FailedToCreateSearchConditionForField.php +++ /dev/null @@ -1,29 +0,0 @@ -field, - $this->type, - ), $previous); - } - - public function getType(): string { - return $this->type; - } - - public function getField(): string { - return $this->field; - } -} diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/Between.php b/packages/graphql/src/SearchBy/Operators/Comparison/Between.php index a161b98fd..31d3d01ba 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/Between.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/Between.php @@ -31,7 +31,7 @@ public function call(Handler $handler, object $builder, Property $property, Argu throw new OperatorUnsupportedBuilder($this, $builder); } - $property = (string) $property; + $property = (string) $property->getParent(); $value = Cast::toIterable($argument->toPlain()); $builder->whereBetween($property, $value); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/BetweenTest.php b/packages/graphql/src/SearchBy/Operators/Comparison/BetweenTest.php index 65e29dca4..967dc0539 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/BetweenTest.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/BetweenTest.php @@ -37,6 +37,7 @@ public function testCall( Closure $argumentFactory, ): void { $operator = $this->app->make(Between::class); + $property = $property->getChild('operator name should be ignored'); $argument = $argumentFactory($this); $search = Mockery::mock(Handler::class); $builder = $builderFactory($this); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/Contains.php b/packages/graphql/src/SearchBy/Operators/Comparison/Contains.php index 12fd58891..ef4c1e1fd 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/Contains.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/Contains.php @@ -29,7 +29,7 @@ public function call(Handler $handler, object $builder, Property $property, Argu throw new OperatorUnsupportedBuilder($this, $builder); } - $property = $builder->getGrammar()->wrap((string) $property); + $property = $builder->getGrammar()->wrap((string) $property->getParent()); $value = (string) Cast::toStringable($argument->toPlain()); $character = $this->getEscapeCharacter(); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/ContainsTest.php b/packages/graphql/src/SearchBy/Operators/Comparison/ContainsTest.php index fdb40d384..27b04717e 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/ContainsTest.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/ContainsTest.php @@ -55,6 +55,7 @@ public function testCall( } $operator = $this->app->make(Contains::class); + $property = $property->getChild('operator name should be ignored'); $argument = $argumentFactory($this); $search = Mockery::mock(Handler::class); $builder = $operator->call($search, $builder, $property, $argument); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/EndsWithTest.php b/packages/graphql/src/SearchBy/Operators/Comparison/EndsWithTest.php index 4c4439898..800fafbe3 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/EndsWithTest.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/EndsWithTest.php @@ -37,6 +37,7 @@ public function testCall( Closure $argumentFactory, ): void { $operator = $this->app->make(EndsWith::class); + $property = $property->getChild('operator name should be ignored'); $argument = $argumentFactory($this); $search = Mockery::mock(Handler::class); $builder = $builderFactory($this); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/Equal.php b/packages/graphql/src/SearchBy/Operators/Comparison/Equal.php index 763db64cd..11025bbb8 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/Equal.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/Equal.php @@ -24,7 +24,7 @@ public function call(Handler $handler, object $builder, Property $property, Argu throw new OperatorUnsupportedBuilder($this, $builder); } - $property = (string) $property; + $property = (string) $property->getParent(); $value = $argument->toPlain(); $builder = $builder->where($property, '=', $value); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/EqualTest.php b/packages/graphql/src/SearchBy/Operators/Comparison/EqualTest.php index 00db6eb12..0e6a279fe 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/EqualTest.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/EqualTest.php @@ -37,6 +37,7 @@ public function testCall( Closure $argumentFactory, ): void { $operator = $this->app->make(Equal::class); + $property = $property->getChild('operator name should be ignored'); $argument = $argumentFactory($this); $search = Mockery::mock(Handler::class); $builder = $builderFactory($this); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/GreaterThan.php b/packages/graphql/src/SearchBy/Operators/Comparison/GreaterThan.php index 95de61351..a124a951c 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/GreaterThan.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/GreaterThan.php @@ -24,7 +24,7 @@ public function call(Handler $handler, object $builder, Property $property, Argu throw new OperatorUnsupportedBuilder($this, $builder); } - $property = (string) $property; + $property = (string) $property->getParent(); $value = $argument->toPlain(); $builder->where($property, '>', $value); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/GreaterThanOrEqual.php b/packages/graphql/src/SearchBy/Operators/Comparison/GreaterThanOrEqual.php index 5a3d09bb0..5b768c4b5 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/GreaterThanOrEqual.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/GreaterThanOrEqual.php @@ -24,7 +24,7 @@ public function call(Handler $handler, object $builder, Property $property, Argu throw new OperatorUnsupportedBuilder($this, $builder); } - $property = (string) $property; + $property = (string) $property->getParent(); $value = $argument->toPlain(); $builder->where($property, '>=', $value); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/GreaterThanOrEqualTest.php b/packages/graphql/src/SearchBy/Operators/Comparison/GreaterThanOrEqualTest.php index 385d73740..60bd90347 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/GreaterThanOrEqualTest.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/GreaterThanOrEqualTest.php @@ -37,6 +37,7 @@ public function testCall( Closure $argumentFactory, ): void { $operator = $this->app->make(GreaterThanOrEqual::class); + $property = $property->getChild('operator name should be ignored'); $argument = $argumentFactory($this); $search = Mockery::mock(Handler::class); $builder = $builderFactory($this); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/GreaterThanTest.php b/packages/graphql/src/SearchBy/Operators/Comparison/GreaterThanTest.php index 06247d2e8..b2630956f 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/GreaterThanTest.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/GreaterThanTest.php @@ -37,6 +37,7 @@ public function testCall( Closure $argumentFactory, ): void { $operator = $this->app->make(GreaterThan::class); + $property = $property->getChild('operator name should be ignored'); $argument = $argumentFactory($this); $search = Mockery::mock(Handler::class); $builder = $builderFactory($this); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/In.php b/packages/graphql/src/SearchBy/Operators/Comparison/In.php index 3fe2ce7b5..d3032650e 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/In.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/In.php @@ -29,7 +29,7 @@ public function call(Handler $handler, object $builder, Property $property, Argu throw new OperatorUnsupportedBuilder($this, $builder); } - $property = (string) $property; + $property = (string) $property->getParent(); $value = $argument->toPlain(); $builder->whereIn($property, $value); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/InTest.php b/packages/graphql/src/SearchBy/Operators/Comparison/InTest.php index 0354178f0..9a1862e11 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/InTest.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/InTest.php @@ -37,6 +37,7 @@ public function testCall( Closure $argumentFactory, ): void { $operator = $this->app->make(In::class); + $property = $property->getChild('operator name should be ignored'); $argument = $argumentFactory($this); $search = Mockery::mock(Handler::class); $builder = $builderFactory($this); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/IsNotNull.php b/packages/graphql/src/SearchBy/Operators/Comparison/IsNotNull.php index daabd1c9d..1cf648e33 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/IsNotNull.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/IsNotNull.php @@ -33,7 +33,7 @@ public function call(Handler $handler, object $builder, Property $property, Argu throw new OperatorUnsupportedBuilder($this, $builder); } - $property = (string) $property; + $property = (string) $property->getParent(); $builder->whereNotNull($property); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/IsNotNullTest.php b/packages/graphql/src/SearchBy/Operators/Comparison/IsNotNullTest.php index c3ada3d76..8bc1fb187 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/IsNotNullTest.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/IsNotNullTest.php @@ -37,6 +37,7 @@ public function testCall( Closure $argumentFactory, ): void { $operator = $this->app->make(IsNotNull::class); + $property = $property->getChild('operator name should be ignored'); $argument = $argumentFactory($this); $search = Mockery::mock(Handler::class); $builder = $builderFactory($this); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/IsNull.php b/packages/graphql/src/SearchBy/Operators/Comparison/IsNull.php index 61abd36bd..a1b11bc05 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/IsNull.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/IsNull.php @@ -33,7 +33,7 @@ public function call(Handler $handler, object $builder, Property $property, Argu throw new OperatorUnsupportedBuilder($this, $builder); } - $property = (string) $property; + $property = (string) $property->getParent(); $builder->whereNull($property); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/IsNullTest.php b/packages/graphql/src/SearchBy/Operators/Comparison/IsNullTest.php index 919a3f525..ac821a88d 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/IsNullTest.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/IsNullTest.php @@ -37,6 +37,7 @@ public function testCall( Closure $argumentFactory, ): void { $operator = $this->app->make(IsNull::class); + $property = $property->getChild('operator name should be ignored'); $argument = $argumentFactory($this); $search = Mockery::mock(Handler::class); $builder = $builderFactory($this); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/LessThan.php b/packages/graphql/src/SearchBy/Operators/Comparison/LessThan.php index 581679ca3..57a08e560 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/LessThan.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/LessThan.php @@ -24,7 +24,7 @@ public function call(Handler $handler, object $builder, Property $property, Argu throw new OperatorUnsupportedBuilder($this, $builder); } - $property = (string) $property; + $property = (string) $property->getParent(); $value = $argument->toPlain(); $builder->where($property, '<', $value); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/LessThanOrEqual.php b/packages/graphql/src/SearchBy/Operators/Comparison/LessThanOrEqual.php index d3795a2f2..e4397f241 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/LessThanOrEqual.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/LessThanOrEqual.php @@ -24,7 +24,7 @@ public function call(Handler $handler, object $builder, Property $property, Argu throw new OperatorUnsupportedBuilder($this, $builder); } - $property = (string) $property; + $property = (string) $property->getParent(); $value = $argument->toPlain(); $builder->where($property, '<=', $value); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/LessThanOrEqualTest.php b/packages/graphql/src/SearchBy/Operators/Comparison/LessThanOrEqualTest.php index 4a5feb69c..d9296d6e1 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/LessThanOrEqualTest.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/LessThanOrEqualTest.php @@ -37,6 +37,7 @@ public function testCall( Closure $argumentFactory, ): void { $operator = $this->app->make(LessThanOrEqual::class); + $property = $property->getChild('operator name should be ignored'); $argument = $argumentFactory($this); $search = Mockery::mock(Handler::class); $builder = $builderFactory($this); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/LessThanTest.php b/packages/graphql/src/SearchBy/Operators/Comparison/LessThanTest.php index 1da604333..f626cd062 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/LessThanTest.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/LessThanTest.php @@ -37,6 +37,7 @@ public function testCall( Closure $argumentFactory, ): void { $operator = $this->app->make(LessThan::class); + $property = $property->getChild('operator name should be ignored'); $argument = $argumentFactory($this); $search = Mockery::mock(Handler::class); $builder = $builderFactory($this); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/Like.php b/packages/graphql/src/SearchBy/Operators/Comparison/Like.php index 5eb527384..eaae9cdac 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/Like.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/Like.php @@ -25,7 +25,7 @@ public function call(Handler $handler, object $builder, Property $property, Argu throw new OperatorUnsupportedBuilder($this, $builder); } - $property = (string) $property; + $property = (string) $property->getParent(); $value = (string) Cast::toStringable($argument->toPlain()); $builder->where($property, 'like', $value); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/LikeTest.php b/packages/graphql/src/SearchBy/Operators/Comparison/LikeTest.php index 0bc9944a0..acd91b9d6 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/LikeTest.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/LikeTest.php @@ -37,6 +37,7 @@ public function testCall( Closure $argumentFactory, ): void { $operator = $this->app->make(Like::class); + $property = $property->getChild('operator name should be ignored'); $argument = $argumentFactory($this); $search = Mockery::mock(Handler::class); $builder = $builderFactory($this); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/NotBetween.php b/packages/graphql/src/SearchBy/Operators/Comparison/NotBetween.php index 6827a3378..120ba2c75 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/NotBetween.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/NotBetween.php @@ -24,7 +24,7 @@ public function call(Handler $handler, object $builder, Property $property, Argu throw new OperatorUnsupportedBuilder($this, $builder); } - $property = (string) $property; + $property = (string) $property->getParent(); $value = Cast::toIterable($argument->toPlain()); $builder->whereNotBetween($property, $value); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/NotBetweenTest.php b/packages/graphql/src/SearchBy/Operators/Comparison/NotBetweenTest.php index aa1645e4b..2899db4fb 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/NotBetweenTest.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/NotBetweenTest.php @@ -37,6 +37,7 @@ public function testCall( Closure $argumentFactory, ): void { $operator = $this->app->make(NotBetween::class); + $property = $property->getChild('operator name should be ignored'); $argument = $argumentFactory($this); $search = Mockery::mock(Handler::class); $builder = $builderFactory($this); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/NotEqual.php b/packages/graphql/src/SearchBy/Operators/Comparison/NotEqual.php index 88cf1191f..d8b76c592 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/NotEqual.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/NotEqual.php @@ -24,7 +24,7 @@ public function call(Handler $handler, object $builder, Property $property, Argu throw new OperatorUnsupportedBuilder($this, $builder); } - $property = (string) $property; + $property = (string) $property->getParent(); $value = $argument->toPlain(); $builder->where($property, '!=', $value); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/NotEqualTest.php b/packages/graphql/src/SearchBy/Operators/Comparison/NotEqualTest.php index bdd7f180a..e58150d80 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/NotEqualTest.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/NotEqualTest.php @@ -37,6 +37,7 @@ public function testCall( Closure $argumentFactory, ): void { $operator = $this->app->make(NotEqual::class); + $property = $property->getChild('operator name should be ignored'); $argument = $argumentFactory($this); $search = Mockery::mock(Handler::class); $builder = $builderFactory($this); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/NotIn.php b/packages/graphql/src/SearchBy/Operators/Comparison/NotIn.php index eeb35bc7f..6ff64adb7 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/NotIn.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/NotIn.php @@ -29,7 +29,7 @@ public function call(Handler $handler, object $builder, Property $property, Argu throw new OperatorUnsupportedBuilder($this, $builder); } - $property = (string) $property; + $property = (string) $property->getParent(); $value = $argument->toPlain(); $builder->whereNotIn($property, $value); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/NotInTest.php b/packages/graphql/src/SearchBy/Operators/Comparison/NotInTest.php index 80c5c2d5c..dee986f78 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/NotInTest.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/NotInTest.php @@ -37,6 +37,7 @@ public function testCall( Closure $argumentFactory, ): void { $operator = $this->app->make(NotIn::class); + $property = $property->getChild('operator name should be ignored'); $argument = $argumentFactory($this); $search = Mockery::mock(Handler::class); $builder = $builderFactory($this); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/NotLike.php b/packages/graphql/src/SearchBy/Operators/Comparison/NotLike.php index 69ee59cab..fee041c2c 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/NotLike.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/NotLike.php @@ -25,7 +25,7 @@ public function call(Handler $handler, object $builder, Property $property, Argu throw new OperatorUnsupportedBuilder($this, $builder); } - $property = (string) $property; + $property = (string) $property->getParent(); $value = (string) Cast::toStringable($argument->toPlain()); $builder->where($property, 'not like', $value); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/NotLikeTest.php b/packages/graphql/src/SearchBy/Operators/Comparison/NotLikeTest.php index 440859b53..f4a43fbfc 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/NotLikeTest.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/NotLikeTest.php @@ -37,6 +37,7 @@ public function testCall( Closure $argumentFactory, ): void { $operator = $this->app->make(NotLike::class); + $property = $property->getChild('operator name should be ignored'); $argument = $argumentFactory($this); $search = Mockery::mock(Handler::class); $builder = $builderFactory($this); diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/StartsWithTest.php b/packages/graphql/src/SearchBy/Operators/Comparison/StartsWithTest.php index 59160468a..5b64d3512 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/StartsWithTest.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/StartsWithTest.php @@ -37,6 +37,7 @@ public function testCall( Closure $argumentFactory, ): void { $operator = $this->app->make(StartsWith::class); + $property = $property->getChild('operator name should be ignored'); $argument = $argumentFactory($this); $search = Mockery::mock(Handler::class); $builder = $builderFactory($this); diff --git a/packages/graphql/src/SearchBy/Operators/Complex/Relation.php b/packages/graphql/src/SearchBy/Operators/Complex/Relation.php index f87669945..bef9bb7b5 100644 --- a/packages/graphql/src/SearchBy/Operators/Complex/Relation.php +++ b/packages/graphql/src/SearchBy/Operators/Complex/Relation.php @@ -18,12 +18,19 @@ use LastDragon_ru\LaraASP\GraphQL\SearchBy\Directives\Directive; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\OperatorInvalidArgumentValue; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\BaseOperator; +use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Property as PropertyOperator; use Nuwave\Lighthouse\Execution\Arguments\Argument; use Nuwave\Lighthouse\Execution\Arguments\ArgumentSet; use function reset; class Relation extends BaseOperator implements ComplexOperator { + public function __construct( + protected PropertyOperator $property, + ) { + parent::__construct(); + } + public static function getName(): string { return 'relation'; } @@ -98,6 +105,7 @@ public function call(Handler $handler, object $builder, Property $property, Argu // * where + notExists = doesntHave // Conditions + $property = $property->getParent(); $relation = (new ModelHelper($builder))->getRelation($property->getName()); $has = $argument->value->arguments['where'] ?? null; $hasCount = $argument->value->arguments['count'] ?? null; @@ -110,7 +118,7 @@ public function call(Handler $handler, object $builder, Property $property, Argu if ($hasCount instanceof Argument) { $query = $builder->toBase()->newQuery(); - $query = $handler->handle($query, $hasCount, new Property('tmp')); + $query = $this->property->call($handler, $query, new Property(), $hasCount); $where = reset($query->wheres); $count = $where['value'] ?? $count; $operator = $where['operator'] ?? $operator; @@ -133,7 +141,7 @@ static function (EloquentBuilder $builder) use ($relation, $handler, $alias, $ha } if ($has instanceof Argument && $has->value instanceof ArgumentSet) { - $handler->handle($builder, $has->value, new Property($alias)); + $handler->handle($builder, new Property($alias), $has->value); } }, ); diff --git a/packages/graphql/src/SearchBy/Operators/Complex/RelationTest.php b/packages/graphql/src/SearchBy/Operators/Complex/RelationTest.php index 88b78bd94..6922d251b 100644 --- a/packages/graphql/src/SearchBy/Operators/Complex/RelationTest.php +++ b/packages/graphql/src/SearchBy/Operators/Complex/RelationTest.php @@ -45,6 +45,7 @@ public function testCall( } $operator = $this->app->make(Relation::class); + $property = $property->getChild('operator name should be ignored'); $argument = $argumentFactory($this); $search = $this->app->make(Directive::class); $builder = $builderFactory($this); @@ -67,6 +68,7 @@ public function dataProviderCall(): array { $graphql = <<<'GRAPHQL' input TestInput { property: TestOperators + @searchByProperty } input TestOperators { diff --git a/packages/graphql/src/SearchBy/Operators/Logical/AllOfTest.php b/packages/graphql/src/SearchBy/Operators/Logical/AllOfTest.php index 75d066110..e0c948919 100644 --- a/packages/graphql/src/SearchBy/Operators/Logical/AllOfTest.php +++ b/packages/graphql/src/SearchBy/Operators/Logical/AllOfTest.php @@ -36,6 +36,7 @@ public function testCall( Closure $argumentFactory, ): void { $operator = $this->app->make(AllOf::class); + $property = $property->getChild('operator name should be ignored'); $argument = $argumentFactory($this); $search = $this->app->make(Directive::class); $builder = $builderFactory($this); @@ -61,8 +62,10 @@ public function dataProviderCall(): array { <<<'GRAPHQL' input TestInput { a: TestOperators + @searchByProperty b: TestOperators + @searchByProperty } input TestOperators { diff --git a/packages/graphql/src/SearchBy/Operators/Logical/AnyOfTest.php b/packages/graphql/src/SearchBy/Operators/Logical/AnyOfTest.php index 1d8d851ce..acb916cdf 100644 --- a/packages/graphql/src/SearchBy/Operators/Logical/AnyOfTest.php +++ b/packages/graphql/src/SearchBy/Operators/Logical/AnyOfTest.php @@ -36,6 +36,7 @@ public function testCall( Closure $argumentFactory, ): void { $operator = $this->app->make(AnyOf::class); + $property = $property->getChild('operator name should be ignored'); $argument = $argumentFactory($this); $search = $this->app->make(Directive::class); $builder = $builderFactory($this); @@ -61,8 +62,10 @@ public function dataProviderCall(): array { <<<'GRAPHQL' input TestInput { a: TestOperators + @searchByProperty b: TestOperators + @searchByProperty } input TestOperators { diff --git a/packages/graphql/src/SearchBy/Operators/Logical/Logical.php b/packages/graphql/src/SearchBy/Operators/Logical/Logical.php index 9a67ac04c..1411e4496 100644 --- a/packages/graphql/src/SearchBy/Operators/Logical/Logical.php +++ b/packages/graphql/src/SearchBy/Operators/Logical/Logical.php @@ -33,7 +33,7 @@ static function (EloquentBuilder|QueryBuilder $builder) use ( $arguments, $property ): void { - $handler->handle($builder, $arguments, $property); + $handler->handle($builder, $property, $arguments); }, null, null, diff --git a/packages/graphql/src/SearchBy/Operators/Logical/NotTest.php b/packages/graphql/src/SearchBy/Operators/Logical/NotTest.php index 626a68419..48472124a 100644 --- a/packages/graphql/src/SearchBy/Operators/Logical/NotTest.php +++ b/packages/graphql/src/SearchBy/Operators/Logical/NotTest.php @@ -36,6 +36,7 @@ public function testCall( Closure $argumentFactory, ): void { $operator = $this->app->make(Not::class); + $property = $property->getChild('operator name should be ignored'); $argument = $argumentFactory($this); $search = $this->app->make(Directive::class); $builder = $builderFactory($this); @@ -62,8 +63,10 @@ public function dataProviderCall(): array { <<<'GRAPHQL' input TestInput { a: TestOperators + @searchByProperty b: TestOperators + @searchByProperty } input TestOperators { diff --git a/packages/graphql/src/SearchBy/Operators/Property.php b/packages/graphql/src/SearchBy/Operators/Property.php new file mode 100644 index 000000000..34b915054 --- /dev/null +++ b/packages/graphql/src/SearchBy/Operators/Property.php @@ -0,0 +1,29 @@ + Date: Sun, 26 Jun 2022 11:39:40 +0400 Subject: [PATCH 04/13] `Builder` simplification. --- .../src/Builder/Contracts/Operator.php | 2 + .../Contracts/TypeDefinition.php | 5 +- .../Contracts/TypeProvider.php | 2 +- .../Builder/Directives/HandlerDirective.php | 6 + .../TypeDefinitionImpossibleToCreateType.php} | 6 +- .../TypeDefinitionInvalidTypeName.php | 45 +++ packages/graphql/src/Builder/Manipulator.php | 117 +++++++ packages/graphql/src/Provider.php | 18 +- .../graphql/src/SearchBy/Ast/Manipulator.php | 113 ++---- .../graphql/src/SearchBy/Ast/MetadataTest.php | 323 ------------------ .../graphql/src/SearchBy/Ast/Repository.php | 31 -- .../Ast/{Metadata.php => Scalars.php} | 101 ++---- .../graphql/src/SearchBy/Ast/ScalarsTest.php | 184 ++++++++++ .../src/SearchBy/Contracts/Operator.php | 2 +- .../src/SearchBy/Directives/Directive.php | 11 +- .../src/SearchBy/Operators/BaseOperator.php | 2 +- .../SearchBy/Operators/Comparison/Between.php | 2 +- .../src/SearchBy/Operators/Comparison/In.php | 2 +- .../Operators/Comparison/IsNotNull.php | 2 +- .../SearchBy/Operators/Comparison/IsNull.php | 2 +- .../SearchBy/Operators/Comparison/NotIn.php | 2 +- .../src/SearchBy/Operators/Logical/AllOf.php | 2 +- .../src/SearchBy/Operators/Logical/AnyOf.php | 2 +- .../src/SearchBy/Operators/Property.php | 2 +- packages/graphql/src/SearchBy/Types/Flag.php | 6 +- packages/graphql/src/SearchBy/Types/Range.php | 6 +- phpstan-baseline.neon | 14 +- 27 files changed, 438 insertions(+), 572 deletions(-) rename packages/graphql/src/{SearchBy => Builder}/Contracts/TypeDefinition.php (70%) rename packages/graphql/src/{SearchBy => Builder}/Contracts/TypeProvider.php (78%) rename packages/graphql/src/{SearchBy/Exceptions/DefinitionImpossibleToCreateType.php => Builder/Exceptions/TypeDefinitionImpossibleToCreateType.php} (81%) create mode 100644 packages/graphql/src/Builder/Exceptions/TypeDefinitionInvalidTypeName.php create mode 100644 packages/graphql/src/Builder/Manipulator.php delete mode 100644 packages/graphql/src/SearchBy/Ast/MetadataTest.php delete mode 100644 packages/graphql/src/SearchBy/Ast/Repository.php rename packages/graphql/src/SearchBy/Ast/{Metadata.php => Scalars.php} (70%) create mode 100644 packages/graphql/src/SearchBy/Ast/ScalarsTest.php diff --git a/packages/graphql/src/Builder/Contracts/Operator.php b/packages/graphql/src/Builder/Contracts/Operator.php index 85673c752..49631b7bf 100644 --- a/packages/graphql/src/Builder/Contracts/Operator.php +++ b/packages/graphql/src/Builder/Contracts/Operator.php @@ -19,6 +19,8 @@ public static function getName(): string; */ public static function getDirectiveName(): string; + public function getFieldType(TypeProvider $provider, string $type): ?string; + public function getFieldDescription(): string; public function getFieldDirective(): ?DirectiveNode; diff --git a/packages/graphql/src/SearchBy/Contracts/TypeDefinition.php b/packages/graphql/src/Builder/Contracts/TypeDefinition.php similarity index 70% rename from packages/graphql/src/SearchBy/Contracts/TypeDefinition.php rename to packages/graphql/src/Builder/Contracts/TypeDefinition.php index 638ba2aa6..badccdedf 100644 --- a/packages/graphql/src/SearchBy/Contracts/TypeDefinition.php +++ b/packages/graphql/src/Builder/Contracts/TypeDefinition.php @@ -1,15 +1,14 @@ container; + } + protected function getFactory(): ArgumentFactory { return $this->factory; } diff --git a/packages/graphql/src/SearchBy/Exceptions/DefinitionImpossibleToCreateType.php b/packages/graphql/src/Builder/Exceptions/TypeDefinitionImpossibleToCreateType.php similarity index 81% rename from packages/graphql/src/SearchBy/Exceptions/DefinitionImpossibleToCreateType.php rename to packages/graphql/src/Builder/Exceptions/TypeDefinitionImpossibleToCreateType.php index 959eeea02..9496c21a0 100644 --- a/packages/graphql/src/SearchBy/Exceptions/DefinitionImpossibleToCreateType.php +++ b/packages/graphql/src/Builder/Exceptions/TypeDefinitionImpossibleToCreateType.php @@ -1,13 +1,13 @@ $definition */ diff --git a/packages/graphql/src/Builder/Exceptions/TypeDefinitionInvalidTypeName.php b/packages/graphql/src/Builder/Exceptions/TypeDefinitionInvalidTypeName.php new file mode 100644 index 000000000..29f7b8be2 --- /dev/null +++ b/packages/graphql/src/Builder/Exceptions/TypeDefinitionInvalidTypeName.php @@ -0,0 +1,45 @@ + $type + */ + public function __construct( + protected string $type, + protected string $expected, + protected string $actual, + Throwable $previous = null, + ) { + parent::__construct( + sprintf( + 'Generated type for TypeDefinition `%s` must be named as `%s`, but its name is `%s`.', + $this->type, + $this->expected, + $this->actual, + ), + $previous, + ); + } + + /** + * @return class-string + */ + public function getType(): string { + return $this->type; + } + + public function getExpected(): string { + return $this->expected; + } + + public function getActual(): string { + return $this->actual; + } +} diff --git a/packages/graphql/src/Builder/Manipulator.php b/packages/graphql/src/Builder/Manipulator.php new file mode 100644 index 000000000..76f8b5601 --- /dev/null +++ b/packages/graphql/src/Builder/Manipulator.php @@ -0,0 +1,117 @@ + + // ========================================================================= + protected function getContainer(): Container { + return $this->container; + } + // + + // + // ========================================================================= + public function getType(string $type, string $scalar = null, bool $nullable = null): string { + // Exists? + $name = $this->getTypeName($type::getName(), $scalar, $nullable); + + if ($this->isTypeDefinitionExists($name)) { + return $name; + } + + // Create new + $instance = $this->getContainer()->make($type); + $definition = $instance->getTypeDefinitionNode($name, $scalar, $nullable); + + if (!$definition) { + throw new TypeDefinitionImpossibleToCreateType($type, $scalar, $nullable); + } + + if ($name !== $this->getNodeName($definition)) { + throw new TypeDefinitionInvalidTypeName($type, $name, $this->getNodeName($definition)); + } + + // Save + $this->addTypeDefinition($definition); + + // Return + return $name; + } + + abstract protected function getTypeName(string $name, string $scalar = null, bool $nullable = null): string; + // + + // + // ========================================================================= + protected function getOperatorField( + Operator $operator, + InputValueDefinitionNode|TypeDefinitionNode|FieldDefinitionNode|InputObjectField|FieldDefinition|Type $type, + string $field = null, + ): string { + $type = $this->getNodeName($type); + $type = $operator->getFieldType($this, $type) ?? $type; + $field = $field ?: $operator::getName(); + $directive = $operator->getFieldDirective() ?? $operator::getDirectiveName(); + $directive = $directive instanceof DirectiveNode + ? Printer::doPrint($directive) + : $directive; + $description = $operator->getFieldDescription(); + + return << $operators + */ + protected function getOperatorsFields( + array $operators, + InputValueDefinitionNode|TypeDefinitionNode|FieldDefinitionNode|InputObjectField|FieldDefinition|Type $type, + ): string { + return implode( + "\n", + array_map( + function (Operator $operator) use ($type): string { + return $this->getOperatorField($operator, $type); + }, + $operators, + ), + ); + } + // +} diff --git a/packages/graphql/src/Provider.php b/packages/graphql/src/Provider.php index 8beace33d..fb1f4f2d9 100644 --- a/packages/graphql/src/Provider.php +++ b/packages/graphql/src/Provider.php @@ -5,7 +5,6 @@ use Illuminate\Contracts\Config\Repository; use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Events\Dispatcher; -use Illuminate\Contracts\Foundation\Application; use Illuminate\Support\ServiceProvider; use LastDragon_ru\LaraASP\Core\Concerns\ProviderWithConfig; use LastDragon_ru\LaraASP\Core\Concerns\ProviderWithTranslations; @@ -13,9 +12,7 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\Settings as SettingsContract; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\SchemaPrinter; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Ast\Metadata; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Ast\Repository as MetadataRepository; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Operator; +use LastDragon_ru\LaraASP\GraphQL\SearchBy\Ast\Scalars; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Definitions\SearchByDirective; use LastDragon_ru\LaraASP\GraphQL\SortBy\Definitions\SortByDirective; use LastDragon_ru\LaraASP\GraphQL\Utils\Enum\EnumType; @@ -60,18 +57,7 @@ static function (): string { } protected function registerSearchByDirective(): void { - $this->app->singleton(MetadataRepository::class); - $this->app->bind(Metadata::class, function (Application $app): Metadata { - /** @var array>|string> $scalars */ - $scalars = (array) $app->make(Repository::class)->get("{$this->getName()}.search_by.scalars"); - $metadata = new Metadata($app); - - foreach ($scalars as $scalar => $operators) { - $metadata->addScalar($scalar, $operators); - } - - return $metadata; - }); + $this->app->singleton(Scalars::class); } protected function registerEnums(): void { diff --git a/packages/graphql/src/SearchBy/Ast/Manipulator.php b/packages/graphql/src/SearchBy/Ast/Manipulator.php index 11f26a923..fc27965d2 100644 --- a/packages/graphql/src/SearchBy/Ast/Manipulator.php +++ b/packages/graphql/src/SearchBy/Ast/Manipulator.php @@ -4,32 +4,26 @@ use GraphQL\Language\AST\DirectiveNode; use GraphQL\Language\AST\EnumTypeDefinitionNode; -use GraphQL\Language\AST\FieldDefinitionNode; use GraphQL\Language\AST\InputObjectTypeDefinitionNode; use GraphQL\Language\AST\InputValueDefinitionNode; use GraphQL\Language\AST\NamedTypeNode; use GraphQL\Language\AST\NonNullTypeNode; use GraphQL\Language\AST\ScalarTypeDefinitionNode; -use GraphQL\Language\AST\TypeDefinitionNode; use GraphQL\Language\Parser; -use GraphQL\Language\Printer; use GraphQL\Type\Definition\EnumType; -use GraphQL\Type\Definition\FieldDefinition; use GraphQL\Type\Definition\InputObjectField; use GraphQL\Type\Definition\InputObjectType; use GraphQL\Type\Definition\NonNull; use GraphQL\Type\Definition\ScalarType; -use GraphQL\Type\Definition\Type; use Illuminate\Contracts\Container\Container; use Illuminate\Support\Str; -use LastDragon_ru\LaraASP\GraphQL\AstManipulator; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Operator as OperatorContract; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeProvider; +use LastDragon_ru\LaraASP\GraphQL\Builder\Manipulator as BuilderManipulator; use LastDragon_ru\LaraASP\GraphQL\Exceptions\TypeDefinitionUnknown; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\ComplexOperator; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Operator; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\TypeProvider; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Directives\Directive; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\ComplexOperatorInvalidTypeName; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\DefinitionImpossibleToCreateType; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\EnumNoOperators; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\FailedToCreateSearchCondition; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\FakeTypeDefinitionIsNotFake; @@ -43,26 +37,27 @@ use Nuwave\Lighthouse\Schema\DirectiveLocator; use Nuwave\Lighthouse\Schema\TypeRegistry; -use function array_map; use function array_shift; use function count; -use function implode; use function is_string; -class Manipulator extends AstManipulator implements TypeProvider { - protected Metadata $metadata; - +class Manipulator extends BuilderManipulator implements TypeProvider { public function __construct( + Container $container, DirectiveLocator $directives, DocumentAST $document, TypeRegistry $types, - Repository $metadata, - protected Container $container, + private Scalars $scalars, ) { - $this->metadata = $metadata->get($document); + parent::__construct($container, $directives, $document, $types); + } - parent::__construct($directives, $document, $types); + // + // ========================================================================= + protected function getScalars(): Scalars { + return $this->scalars; } + // // // ========================================================================= @@ -86,32 +81,6 @@ public function update(DirectiveNode $directive, InputValueDefinitionNode $node) // // ========================================================================= - public function getType(string $type, string $scalar = null, bool $nullable = null): string { - // Exists? - $internal = $this->getTypeName($type::getName(), $scalar, $nullable); - $name = $this->metadata->getType($internal); - - if ($name && $this->isTypeDefinitionExists($name)) { - return $name; - } - - // Create new - $definition = $this->metadata->getDefinition($type)->getTypeDefinitionNode($internal, $scalar, $nullable); - - if (!$definition) { - throw new DefinitionImpossibleToCreateType($type, $scalar, $nullable); - } - - // Save - $name = $this->getNodeName($definition); - - $this->addTypeDefinition($definition); - $this->metadata->addType($internal, $name); - - // Return - return $name; - } - public function getInputType(InputObjectTypeDefinitionNode|InputObjectType $node): string { // Exists? $name = $this->getConditionTypeName($node); @@ -138,7 +107,8 @@ public function getInputType(InputObjectTypeDefinitionNode|InputObjectType $node ); // Add searchable fields - $property = $this->metadata->getOperatorInstance(Property::class); + $property = $this->getContainer()->make(Property::class); + $scalars = $this->getScalars(); $fields = $node instanceof InputObjectType ? $node->getFields() : $node->fields; @@ -164,7 +134,7 @@ public function getInputType(InputObjectTypeDefinitionNode|InputObjectType $node try { $fieldTypeNode = $this->getTypeDefinitionNode($field); } catch (TypeDefinitionUnknown $exception) { - if ($this->metadata->isScalar($fieldType)) { + if ($scalars->isScalar($fieldType)) { $fieldTypeNode = $this->getScalarTypeNode($fieldType); } else { throw $exception; @@ -275,47 +245,6 @@ public function getScalarType(ScalarTypeDefinitionNode|ScalarType $type, bool $n return $name; } - protected function getOperatorField( - Operator $operator, - InputValueDefinitionNode|TypeDefinitionNode|FieldDefinitionNode|InputObjectField|FieldDefinition|Type $type, - string $field = null, - ): string { - $type = $this->getNodeName($type); - $type = $operator->getFieldType($this, $type) ?? $type; - $field = $field ?: $operator::getName(); - $directive = $operator->getFieldDirective() ?? $operator::getDirectiveName(); - $directive = $directive instanceof DirectiveNode - ? Printer::doPrint($directive) - : $directive; - $description = $operator->getFieldDescription(); - - return << $operators - */ - protected function getOperatorsFields( - array $operators, - InputValueDefinitionNode|TypeDefinitionNode|FieldDefinitionNode|InputObjectField|FieldDefinition|Type $type, - ): string { - return implode( - "\n", - array_map( - function (Operator $operator) use ($type): string { - return $this->getOperatorField($operator, $type); - }, - $operators, - ), - ); - } - protected function getComplexType( InputValueDefinitionNode|InputObjectField $field, InputObjectTypeDefinitionNode|InputObjectType $type, @@ -385,10 +314,10 @@ protected function getComplexTypeName( // // ========================================================================= /** - * @return array + * @return array */ protected function getEnumOperators(string $enum, bool $nullable): array { - $operators = $this->metadata->getEnumOperators($enum, $nullable); + $operators = $this->getScalars()->getEnumOperators($enum, $nullable); if (!$operators) { throw new EnumNoOperators($enum); @@ -398,10 +327,10 @@ protected function getEnumOperators(string $enum, bool $nullable): array { } /** - * @return array + * @return array */ protected function getScalarOperators(string $scalar, bool $nullable): array { - $operators = $this->metadata->getScalarOperators($scalar, $nullable); + $operators = $this->getScalars()->getScalarOperators($scalar, $nullable); if (!$operators) { throw new ScalarNoOperators($scalar); @@ -425,7 +354,7 @@ protected function getComplexOperator( // Default if (!$operator) { - $operator = $this->metadata->getComplexOperatorInstance(Relation::class); + $operator = $this->getContainer()->make(Relation::class); } // Return diff --git a/packages/graphql/src/SearchBy/Ast/MetadataTest.php b/packages/graphql/src/SearchBy/Ast/MetadataTest.php deleted file mode 100644 index 29fd119fc..000000000 --- a/packages/graphql/src/SearchBy/Ast/MetadataTest.php +++ /dev/null @@ -1,323 +0,0 @@ - - // ========================================================================= - /** - * @covers ::isScalar - */ - public function testIsScalar(): void { - $metadata = new Metadata($this->app); - - self::assertTrue($metadata->isScalar(Directive::ScalarInt)); - self::assertFalse($metadata->isScalar('unknown')); - } - - /** - * @covers ::addScalar - * - * @dataProvider dataProviderAddScalar - */ - public function testAddScalar(Exception|bool $expected, string $scalar, mixed $operators): void { - if ($expected instanceof Exception) { - self::expectExceptionObject($expected); - } - - $metadata = new Metadata($this->app); - - $metadata->addScalar($scalar, $operators); - - self::assertEquals($expected, $metadata->isScalar($scalar)); - } - - /** - * @covers ::getScalarOperators - */ - public function testGetScalarOperators(): void { - $scalar = __FUNCTION__; - $alias = 'alias'; - $metadata = new Metadata($this->app); - - $metadata->addScalar($scalar, [Equal::class, Equal::class]); - $metadata->addScalar($alias, $scalar); - - self::assertEquals( - [Equal::class], - $this->toClassNames($metadata->getScalarOperators($scalar, false)), - ); - self::assertEquals( - [Equal::class, IsNull::class, IsNotNull::class], - $this->toClassNames($metadata->getScalarOperators($scalar, true)), - ); - self::assertEquals( - $metadata->getScalarOperators($scalar, false), - $metadata->getScalarOperators($alias, false), - ); - self::assertEquals( - $metadata->getScalarOperators($scalar, true), - $metadata->getScalarOperators($alias, true), - ); - } - - /** - * @covers ::getScalarOperators - */ - public function testGetScalarOperatorsUnknownScalar(): void { - self::expectExceptionObject(new ScalarUnknown('unknown')); - - (new Metadata($this->app))->getScalarOperators('unknown', false); - } - - /** - * @covers ::getEnumOperators - */ - public function testGetEnumOperators(): void { - $enum = __FUNCTION__; - $alias = 'alias'; - $metadata = new Metadata($this->app); - - $metadata->addScalar($enum, [Equal::class, Equal::class]); - $metadata->addScalar($alias, $enum); - $metadata->addScalar(Directive::ScalarEnum, [NotEqual::class, NotEqual::class]); - - self::assertEquals( - [NotEqual::class], - $this->toClassNames($metadata->getEnumOperators('unknown', false)), - ); - self::assertEquals( - [NotEqual::class, IsNull::class, IsNotNull::class], - $this->toClassNames($metadata->getEnumOperators('unknown', true)), - ); - self::assertEquals( - [Equal::class], - $this->toClassNames($metadata->getEnumOperators($enum, false)), - ); - self::assertEquals( - [Equal::class, IsNull::class, IsNotNull::class], - $this->toClassNames($metadata->getEnumOperators($enum, true)), - ); - self::assertEquals( - $metadata->getEnumOperators($enum, false), - $metadata->getEnumOperators($alias, false), - ); - self::assertEquals( - $metadata->getEnumOperators($enum, true), - $metadata->getEnumOperators($alias, true), - ); - } - - /** - * @covers ::getOperatorInstance - */ - public function testGetOperatorInstance(): void { - $operator = new class() implements Operator { - public static function getName(): string { - return ''; - } - - public static function definition(): string { - return ''; - } - - public static function getDirectiveName(): string { - return ''; - } - - public function getFieldType(TypeProvider $provider, string $type): ?string { - return null; - } - - public function getFieldDescription(): string { - return ''; - } - - public function getFieldDirective(): ?DirectiveNode { - return null; - } - - public function isBuilderSupported(object $builder): bool { - return false; - } - - public function call(Handler $handler, object $builder, Property $property, Argument $argument): object { - return $builder; - } - }; - $metadata = Mockery::mock(Metadata::class, [$this->app]); - $metadata->makePartial(); - - $a = $metadata->getOperatorInstance($operator::class); - $b = $metadata->getOperatorInstance($operator::class); - - self::assertSame($a, $b); - } - - /** - * @covers ::getComplexOperatorInstance - */ - public function testGetComplexOperatorInstance(): void { - $operator = new class() implements ComplexOperator { - public static function getName(): string { - return ''; - } - - public static function definition(): string { - return ''; - } - - public function getDefinition( - Manipulator $ast, - InputValueDefinitionNode|InputObjectField $field, - InputObjectTypeDefinitionNode|InputObjectType $type, - string $name, - bool $nullable, - ): InputObjectTypeDefinitionNode { - throw new Exception(); - } - - public static function getDirectiveName(): string { - return ''; - } - - public function getFieldType(TypeProvider $provider, string $type): ?string { - return null; - } - - public function getFieldDescription(): string { - return ''; - } - - public function getFieldDirective(): ?DirectiveNode { - return null; - } - - public function isBuilderSupported(object $builder): bool { - return false; - } - - public function call(Handler $handler, object $builder, Property $property, Argument $argument): object { - return $builder; - } - }; - $metadata = $this->app->make(Metadata::class); - $operatorA = $metadata->getComplexOperatorInstance($operator::class); - $operatorB = $metadata->getComplexOperatorInstance($operator::class); - - self::assertSame($operatorA, $operatorB); - } - - /** - * @covers ::getDefinition - */ - public function testGetDefinition(): void { - $definition = new class() implements TypeDefinition { - public function getTypeDefinitionNode( - string $name, - string $scalar = null, - bool $nullable = null, - ): ?TypeDefinitionNode { - return null; - } - - public static function getName(): string { - return 'Test'; - } - }; - - $metadata = $this->app->make(Metadata::class); - $definitionA = $metadata->getDefinition($definition::class); - $definitionB = $metadata->getDefinition($definition::class); - - self::assertSame($definitionA, $definitionB); - } - - /** - * @covers ::addType - * @covers ::getType - */ - public function testGetType(): void { - $metadata = new Metadata($this->app); - - self::assertNull($metadata->getType('test')); - - $metadata->addType('test', 'TestType'); - - self::assertEquals('TestType', $metadata->getType('test')); - - $metadata->addType('test', 'TestType2'); - - self::assertEquals('TestType2', $metadata->getType('test')); - } - // - - // - // ========================================================================= - /** - * @return array - */ - public function dataProviderAddScalar(): array { - return [ - 'ok' => [true, 'scalar', [IsNot::class]], - 'unknown scalar' => [ - new ScalarUnknown('unknown'), - 'scalar', - 'unknown', - ], - 'empty operators' => [ - new ScalarNoOperators('scalar'), - 'scalar', - [], - ], - ]; - } - // - - // - // ========================================================================= - /** - * @param array $objects - * - * @return array - */ - protected function toClassNames(array $objects): array { - $classes = []; - - foreach ($objects as $object) { - $classes[] = $object::class; - } - - return $classes; - } - // -} diff --git a/packages/graphql/src/SearchBy/Ast/Repository.php b/packages/graphql/src/SearchBy/Ast/Repository.php deleted file mode 100644 index 7a6779500..000000000 --- a/packages/graphql/src/SearchBy/Ast/Repository.php +++ /dev/null @@ -1,31 +0,0 @@ - - */ - protected WeakMap $map; - - public function __construct( - protected Container $container, - ) { - $this->map = new WeakMap(); - } - - public function get(DocumentAST $document): Metadata { - $metadata = $this->map[$document] ?? null; - - if ($metadata === null) { - $metadata = $this->container->make(Metadata::class); - $this->map[$document] = $metadata; - } - - return $metadata; - } -} diff --git a/packages/graphql/src/SearchBy/Ast/Metadata.php b/packages/graphql/src/SearchBy/Ast/Scalars.php similarity index 70% rename from packages/graphql/src/SearchBy/Ast/Metadata.php rename to packages/graphql/src/SearchBy/Ast/Scalars.php index 3dd23ad4b..4cdba9dae 100644 --- a/packages/graphql/src/SearchBy/Ast/Metadata.php +++ b/packages/graphql/src/SearchBy/Ast/Scalars.php @@ -2,10 +2,11 @@ namespace LastDragon_ru\LaraASP\GraphQL\SearchBy\Ast; -use Illuminate\Contracts\Container\Container; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\ComplexOperator; +use Illuminate\Container\Container; +use Illuminate\Contracts\Config\Repository; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Operator as OperatorContract; +use LastDragon_ru\LaraASP\GraphQL\Package; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Operator; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\TypeDefinition; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Directives\Directive; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\ScalarNoOperators; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\ScalarUnknown; @@ -39,7 +40,7 @@ use const SORT_REGULAR; -class Metadata { +class Scalars { /** * Determines default operators available for each scalar type. * @@ -100,38 +101,20 @@ class Metadata { ], ]; - /** - * Cached operators instances. - * - * @var array - */ - protected array $operators = []; - - /** - * Cached complex operators instances. - * - * @var array - */ - protected array $complex = []; - - /** - * Types definitions. - * - * @var array - */ - protected array $definitions = []; - - /** - * Cached types created by definitions. - * - * @var array - */ - protected array $types = []; - public function __construct( - protected Container $container, + private Container $container, + Repository $config, ) { - // empty + /** @var array>|string> $scalars */ + $scalars = (array) $config->get(Package::Name.'.search_by.scalars'); + + foreach ($scalars as $scalar => $operators) { + $this->addScalar($scalar, $operators); + } + } + + protected function getContainer(): Container { + return $this->container; } public function isScalar(string $scalar): bool { @@ -154,7 +137,7 @@ public function addScalar(string $scalar, array|string $operators): void { } /** - * @return array + * @return array */ public function getScalarOperators(string $scalar, bool $nullable): array { // Is Scalar? @@ -170,8 +153,9 @@ public function getScalarOperators(string $scalar, bool $nullable): array { } while (!is_array($operators)); // Create Instances - $operators = array_map(function (string $operator): Operator { - return $this->getOperatorInstance($operator); + $container = $this->getContainer(); + $operators = array_map(static function (string $operator) use ($container): OperatorContract { + return $container->make($operator); }, $operators); // Add `null` for nullable @@ -187,52 +171,11 @@ public function getScalarOperators(string $scalar, bool $nullable): array { } /** - * @return array + * @return array */ public function getEnumOperators(string $enum, bool $nullable): array { return $this->isScalar($enum) ? $this->getScalarOperators($enum, $nullable) : $this->getScalarOperators(Directive::ScalarEnum, $nullable); } - - /** - * @param class-string $class - */ - public function getOperatorInstance(string $class): Operator { - if (!isset($this->operators[$class])) { - $this->operators[$class] = $this->container->make($class); - } - - return $this->operators[$class]; - } - - /** - * @param class-string $class - */ - public function getComplexOperatorInstance(string $class): ComplexOperator { - if (!isset($this->complex[$class])) { - $this->complex[$class] = $this->container->make($class); - } - - return $this->complex[$class]; - } - - /** - * @param class-string $definition - */ - public function getDefinition(string $definition): TypeDefinition { - if (!isset($this->definitions[$definition])) { - $this->definitions[$definition] = $this->container->make($definition); - } - - return $this->definitions[$definition]; - } - - public function getType(string $type): ?string { - return $this->types[$type] ?? null; - } - - public function addType(string $type, string $fullyQualifiedName): void { - $this->types[$type] = $fullyQualifiedName; - } } diff --git a/packages/graphql/src/SearchBy/Ast/ScalarsTest.php b/packages/graphql/src/SearchBy/Ast/ScalarsTest.php new file mode 100644 index 000000000..a3a6af8a6 --- /dev/null +++ b/packages/graphql/src/SearchBy/Ast/ScalarsTest.php @@ -0,0 +1,184 @@ + + // ========================================================================= + /** + * @before + */ + public function init(): void { + $this->afterApplicationCreated(function (): void { + $this->override(Repository::class, static function (MockInterface $mock): void { + $mock + ->shouldReceive('get') + ->andReturn(null); + }); + }); + } + // + + // + // ========================================================================= + /** + * @covers ::isScalar + */ + public function testIsScalar(): void { + $scalars = $this->app->make(Scalars::class); + + self::assertTrue($scalars->isScalar(Directive::ScalarInt)); + self::assertFalse($scalars->isScalar('unknown')); + } + + /** + * @covers ::addScalar + * + * @dataProvider dataProviderAddScalar + */ + public function testAddScalar(Exception|bool $expected, string $scalar, mixed $operators): void { + if ($expected instanceof Exception) { + self::expectExceptionObject($expected); + } + + $scalars = $this->app->make(Scalars::class); + + $scalars->addScalar($scalar, $operators); + + self::assertEquals($expected, $scalars->isScalar($scalar)); + } + + /** + * @covers ::getScalarOperators + */ + public function testGetScalarOperators(): void { + $scalar = __FUNCTION__; + $alias = 'alias'; + $scalars = $this->app->make(Scalars::class); + + $scalars->addScalar($scalar, [Equal::class, Equal::class]); + $scalars->addScalar($alias, $scalar); + + self::assertEquals( + [Equal::class], + $this->toClassNames($scalars->getScalarOperators($scalar, false)), + ); + self::assertEquals( + [Equal::class, IsNull::class, IsNotNull::class], + $this->toClassNames($scalars->getScalarOperators($scalar, true)), + ); + self::assertEquals( + $scalars->getScalarOperators($scalar, false), + $scalars->getScalarOperators($alias, false), + ); + self::assertEquals( + $scalars->getScalarOperators($scalar, true), + $scalars->getScalarOperators($alias, true), + ); + } + + /** + * @covers ::getScalarOperators + */ + public function testGetScalarOperatorsUnknownScalar(): void { + self::expectExceptionObject(new ScalarUnknown('unknown')); + + $this->app->make(Scalars::class)->getScalarOperators('unknown', false); + } + + /** + * @covers ::getEnumOperators + */ + public function testGetEnumOperators(): void { + $enum = __FUNCTION__; + $alias = 'alias'; + $scalars = $this->app->make(Scalars::class); + + $scalars->addScalar($enum, [Equal::class, Equal::class]); + $scalars->addScalar($alias, $enum); + $scalars->addScalar(Directive::ScalarEnum, [NotEqual::class, NotEqual::class]); + + self::assertEquals( + [NotEqual::class], + $this->toClassNames($scalars->getEnumOperators('unknown', false)), + ); + self::assertEquals( + [NotEqual::class, IsNull::class, IsNotNull::class], + $this->toClassNames($scalars->getEnumOperators('unknown', true)), + ); + self::assertEquals( + [Equal::class], + $this->toClassNames($scalars->getEnumOperators($enum, false)), + ); + self::assertEquals( + [Equal::class, IsNull::class, IsNotNull::class], + $this->toClassNames($scalars->getEnumOperators($enum, true)), + ); + self::assertEquals( + $scalars->getEnumOperators($enum, false), + $scalars->getEnumOperators($alias, false), + ); + self::assertEquals( + $scalars->getEnumOperators($enum, true), + $scalars->getEnumOperators($alias, true), + ); + } + // + + // + // ========================================================================= + /** + * @return array + */ + public function dataProviderAddScalar(): array { + return [ + 'ok' => [true, 'scalar', [IsNot::class]], + 'unknown scalar' => [ + new ScalarUnknown('unknown'), + 'scalar', + 'unknown', + ], + 'empty operators' => [ + new ScalarNoOperators('scalar'), + 'scalar', + [], + ], + ]; + } + // + + // + // ========================================================================= + /** + * @param array $objects + * + * @return array + */ + protected function toClassNames(array $objects): array { + $classes = []; + + foreach ($objects as $object) { + $classes[] = $object::class; + } + + return $classes; + } + // +} diff --git a/packages/graphql/src/SearchBy/Contracts/Operator.php b/packages/graphql/src/SearchBy/Contracts/Operator.php index c4c8a4d1d..ec8233480 100644 --- a/packages/graphql/src/SearchBy/Contracts/Operator.php +++ b/packages/graphql/src/SearchBy/Contracts/Operator.php @@ -5,5 +5,5 @@ use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Operator as OperatorContract; interface Operator extends OperatorContract { - public function getFieldType(TypeProvider $provider, string $type): ?string; + // empty } diff --git a/packages/graphql/src/SearchBy/Directives/Directive.php b/packages/graphql/src/SearchBy/Directives/Directive.php index 50963c219..c610a70e4 100644 --- a/packages/graphql/src/SearchBy/Directives/Directive.php +++ b/packages/graphql/src/SearchBy/Directives/Directive.php @@ -5,13 +5,11 @@ use GraphQL\Language\AST\FieldDefinitionNode; use GraphQL\Language\AST\InputValueDefinitionNode; use GraphQL\Language\AST\ObjectTypeDefinitionNode; -use Illuminate\Contracts\Container\Container; use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Query\Builder as QueryBuilder; use LastDragon_ru\LaraASP\GraphQL\Builder\Directives\HandlerDirective; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Ast\Manipulator; -use LastDragon_ru\LaraASP\GraphQL\Utils\ArgumentFactory; use Nuwave\Lighthouse\Schema\AST\DocumentAST; use Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective; use Nuwave\Lighthouse\Support\Contracts\ArgManipulator; @@ -28,13 +26,6 @@ class Directive extends HandlerDirective implements ArgManipulator, ArgBuilderDi public const ScalarLogic = self::Name.'Logic'; public const ScalarNumber = self::Name.'Number'; - public function __construct( - protected Container $container, - ArgumentFactory $factory, - ) { - parent::__construct($factory); - } - public static function definition(): string { return /** @lang GraphQL */ <<<'GRAPHQL' """ @@ -50,7 +41,7 @@ public function manipulateArgDefinition( FieldDefinitionNode &$parentField, ObjectTypeDefinitionNode &$parentType, ): void { - $this->container + $this->getContainer() ->make(Manipulator::class, ['document' => $documentAST]) ->update($this->directiveNode, $argDefinition); } diff --git a/packages/graphql/src/SearchBy/Operators/BaseOperator.php b/packages/graphql/src/SearchBy/Operators/BaseOperator.php index bffd20998..e87ecfec9 100644 --- a/packages/graphql/src/SearchBy/Operators/BaseOperator.php +++ b/packages/graphql/src/SearchBy/Operators/BaseOperator.php @@ -5,9 +5,9 @@ use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Query\Builder as QueryBuilder; use Illuminate\Support\Str; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeProvider; use LastDragon_ru\LaraASP\GraphQL\Builder\Directives\OperatorDirective; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Operator; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\TypeProvider; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Directives\Directive; use function implode; diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/Between.php b/packages/graphql/src/SearchBy/Operators/Comparison/Between.php index 31d3d01ba..bf56fb142 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/Between.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/Between.php @@ -6,9 +6,9 @@ use Illuminate\Database\Query\Builder as QueryBuilder; use LastDragon_ru\LaraASP\Core\Utils\Cast; use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeProvider; use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\OperatorUnsupportedBuilder; use LastDragon_ru\LaraASP\GraphQL\Builder\Property; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\TypeProvider; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\BaseOperator; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Types\Range; use Nuwave\Lighthouse\Execution\Arguments\Argument; diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/In.php b/packages/graphql/src/SearchBy/Operators/Comparison/In.php index d3032650e..d0251b6b4 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/In.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/In.php @@ -5,9 +5,9 @@ use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Query\Builder as QueryBuilder; use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeProvider; use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\OperatorUnsupportedBuilder; use LastDragon_ru\LaraASP\GraphQL\Builder\Property; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\TypeProvider; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\BaseOperator; use Nuwave\Lighthouse\Execution\Arguments\Argument; diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/IsNotNull.php b/packages/graphql/src/SearchBy/Operators/Comparison/IsNotNull.php index 1cf648e33..45ec9eb82 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/IsNotNull.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/IsNotNull.php @@ -5,9 +5,9 @@ use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Query\Builder as QueryBuilder; use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeProvider; use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\OperatorUnsupportedBuilder; use LastDragon_ru\LaraASP\GraphQL\Builder\Property; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\TypeProvider; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\BaseOperator; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Types\Flag; use Nuwave\Lighthouse\Execution\Arguments\Argument; diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/IsNull.php b/packages/graphql/src/SearchBy/Operators/Comparison/IsNull.php index a1b11bc05..cd619016a 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/IsNull.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/IsNull.php @@ -5,9 +5,9 @@ use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Query\Builder as QueryBuilder; use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeProvider; use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\OperatorUnsupportedBuilder; use LastDragon_ru\LaraASP\GraphQL\Builder\Property; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\TypeProvider; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\BaseOperator; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Types\Flag; use Nuwave\Lighthouse\Execution\Arguments\Argument; diff --git a/packages/graphql/src/SearchBy/Operators/Comparison/NotIn.php b/packages/graphql/src/SearchBy/Operators/Comparison/NotIn.php index 6ff64adb7..81e9d0fc3 100644 --- a/packages/graphql/src/SearchBy/Operators/Comparison/NotIn.php +++ b/packages/graphql/src/SearchBy/Operators/Comparison/NotIn.php @@ -5,9 +5,9 @@ use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Query\Builder as QueryBuilder; use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeProvider; use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\OperatorUnsupportedBuilder; use LastDragon_ru\LaraASP\GraphQL\Builder\Property; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\TypeProvider; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\BaseOperator; use Nuwave\Lighthouse\Execution\Arguments\Argument; diff --git a/packages/graphql/src/SearchBy/Operators/Logical/AllOf.php b/packages/graphql/src/SearchBy/Operators/Logical/AllOf.php index 06e881093..bdf64b4a4 100644 --- a/packages/graphql/src/SearchBy/Operators/Logical/AllOf.php +++ b/packages/graphql/src/SearchBy/Operators/Logical/AllOf.php @@ -2,7 +2,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Logical; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\TypeProvider; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeProvider; /** * @internal Must not be used directly. diff --git a/packages/graphql/src/SearchBy/Operators/Logical/AnyOf.php b/packages/graphql/src/SearchBy/Operators/Logical/AnyOf.php index aaaf2f073..d79d0ebeb 100644 --- a/packages/graphql/src/SearchBy/Operators/Logical/AnyOf.php +++ b/packages/graphql/src/SearchBy/Operators/Logical/AnyOf.php @@ -2,7 +2,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Logical; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\TypeProvider; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeProvider; /** * @internal Must not be used directly. diff --git a/packages/graphql/src/SearchBy/Operators/Property.php b/packages/graphql/src/SearchBy/Operators/Property.php index 34b915054..c976f0591 100644 --- a/packages/graphql/src/SearchBy/Operators/Property.php +++ b/packages/graphql/src/SearchBy/Operators/Property.php @@ -3,9 +3,9 @@ namespace LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators; use Illuminate\Support\Str; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeProvider; use LastDragon_ru\LaraASP\GraphQL\Builder\Directives\PropertyDirective; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Operator; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\TypeProvider; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Directives\Directive; use function implode; diff --git a/packages/graphql/src/SearchBy/Types/Flag.php b/packages/graphql/src/SearchBy/Types/Flag.php index 3dc5f619c..b95502453 100644 --- a/packages/graphql/src/SearchBy/Types/Flag.php +++ b/packages/graphql/src/SearchBy/Types/Flag.php @@ -4,11 +4,15 @@ use GraphQL\Language\AST\TypeDefinitionNode; use GraphQL\Language\Parser; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\TypeDefinition; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeDefinition; use function is_null; class Flag implements TypeDefinition { + public function __construct() { + // empty + } + public static function getName(): string { return 'Flag'; } diff --git a/packages/graphql/src/SearchBy/Types/Range.php b/packages/graphql/src/SearchBy/Types/Range.php index 50bc5c487..2f150834b 100644 --- a/packages/graphql/src/SearchBy/Types/Range.php +++ b/packages/graphql/src/SearchBy/Types/Range.php @@ -4,11 +4,15 @@ use GraphQL\Language\AST\TypeDefinitionNode; use GraphQL\Language\Parser; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\TypeDefinition; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeDefinition; use function is_null; class Range implements TypeDefinition { + public function __construct() { + // empty + } + public static function getName(): string { return 'Range'; } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index e1af4541f..47972d69c 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -30,6 +30,16 @@ parameters: count: 1 path: packages/graphql/src/AstManipulator.php + - + message: "#^Unable to resolve the template type TClass in call to method LastDragon_ru\\\\LaraASP\\\\GraphQL\\\\AstManipulator\\:\\:addTypeDefinition\\(\\)$#" + count: 1 + path: packages/graphql/src/Builder/Manipulator.php + + - + message: "#^Unable to resolve the template type TInterface in call to method LastDragon_ru\\\\LaraASP\\\\GraphQL\\\\AstManipulator\\:\\:addTypeDefinition\\(\\)$#" + count: 1 + path: packages/graphql/src/Builder/Manipulator.php + - message: "#^Property LastDragon_ru\\\\LaraASP\\\\GraphQL\\\\Builder\\\\Property\\:\\:\\$path \\(array\\\\) does not accept array\\\\.$#" count: 1 @@ -51,9 +61,9 @@ parameters: path: packages/graphql/src/SchemaPrinter/Misc/DirectiveResolver.php - - message: "#^Parameter \\#2 \\$operators of method LastDragon_ru\\\\LaraASP\\\\GraphQL\\\\SearchBy\\\\Ast\\\\Metadata\\:\\:addScalar\\(\\) expects array\\\\>\\|string, mixed given\\.$#" + message: "#^Parameter \\#2 \\$operators of method LastDragon_ru\\\\LaraASP\\\\GraphQL\\\\SearchBy\\\\Ast\\\\Scalars\\:\\:addScalar\\(\\) expects array\\\\>\\|string, mixed given\\.$#" count: 1 - path: packages/graphql/src/SearchBy/Ast/MetadataTest.php + path: packages/graphql/src/SearchBy/Ast/ScalarsTest.php - message: "#^Call to an undefined method Nuwave\\\\Lighthouse\\\\Schema\\\\Directives\\\\BaseDirective\\:\\:handleBuilder\\(\\)\\.$#" From c11f912e9a770cb6b5b431cfb5ee7b51f6b58a14 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 26 Jun 2022 11:44:10 +0400 Subject: [PATCH 05/13] Classes from `SearchBy\Ast` namespace moved to `SearchBy`. --- packages/graphql/src/Provider.php | 2 +- .../graphql/src/SearchBy/Contracts/ComplexOperator.php | 2 +- packages/graphql/src/SearchBy/Directives/Directive.php | 2 +- packages/graphql/src/SearchBy/{Ast => }/Manipulator.php | 2 +- .../graphql/src/SearchBy/Operators/Complex/Relation.php | 2 +- packages/graphql/src/SearchBy/{Ast => }/Scalars.php | 2 +- packages/graphql/src/SearchBy/{Ast => }/ScalarsTest.php | 4 ++-- phpstan-baseline.neon | 8 ++++---- 8 files changed, 12 insertions(+), 12 deletions(-) rename packages/graphql/src/SearchBy/{Ast => }/Manipulator.php (99%) rename packages/graphql/src/SearchBy/{Ast => }/Scalars.php (99%) rename packages/graphql/src/SearchBy/{Ast => }/ScalarsTest.php (97%) diff --git a/packages/graphql/src/Provider.php b/packages/graphql/src/Provider.php index fb1f4f2d9..bc1703c56 100644 --- a/packages/graphql/src/Provider.php +++ b/packages/graphql/src/Provider.php @@ -12,8 +12,8 @@ use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Contracts\Settings as SettingsContract; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\SchemaPrinter; use LastDragon_ru\LaraASP\GraphQL\SchemaPrinter\Settings\DefaultSettings; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Ast\Scalars; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Definitions\SearchByDirective; +use LastDragon_ru\LaraASP\GraphQL\SearchBy\Scalars; use LastDragon_ru\LaraASP\GraphQL\SortBy\Definitions\SortByDirective; use LastDragon_ru\LaraASP\GraphQL\Utils\Enum\EnumType; use Nuwave\Lighthouse\Events\RegisterDirectiveNamespaces; diff --git a/packages/graphql/src/SearchBy/Contracts/ComplexOperator.php b/packages/graphql/src/SearchBy/Contracts/ComplexOperator.php index e138c2cc4..18c38bfd7 100644 --- a/packages/graphql/src/SearchBy/Contracts/ComplexOperator.php +++ b/packages/graphql/src/SearchBy/Contracts/ComplexOperator.php @@ -6,7 +6,7 @@ use GraphQL\Language\AST\InputValueDefinitionNode; use GraphQL\Type\Definition\InputObjectField; use GraphQL\Type\Definition\InputObjectType; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Ast\Manipulator; +use LastDragon_ru\LaraASP\GraphQL\SearchBy\Manipulator; /** * Complex operator. diff --git a/packages/graphql/src/SearchBy/Directives/Directive.php b/packages/graphql/src/SearchBy/Directives/Directive.php index c610a70e4..53b2f9713 100644 --- a/packages/graphql/src/SearchBy/Directives/Directive.php +++ b/packages/graphql/src/SearchBy/Directives/Directive.php @@ -9,7 +9,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Query\Builder as QueryBuilder; use LastDragon_ru\LaraASP\GraphQL\Builder\Directives\HandlerDirective; -use LastDragon_ru\LaraASP\GraphQL\SearchBy\Ast\Manipulator; +use LastDragon_ru\LaraASP\GraphQL\SearchBy\Manipulator; use Nuwave\Lighthouse\Schema\AST\DocumentAST; use Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective; use Nuwave\Lighthouse\Support\Contracts\ArgManipulator; diff --git a/packages/graphql/src/SearchBy/Ast/Manipulator.php b/packages/graphql/src/SearchBy/Manipulator.php similarity index 99% rename from packages/graphql/src/SearchBy/Ast/Manipulator.php rename to packages/graphql/src/SearchBy/Manipulator.php index fc27965d2..abc15c18d 100644 --- a/packages/graphql/src/SearchBy/Ast/Manipulator.php +++ b/packages/graphql/src/SearchBy/Manipulator.php @@ -1,6 +1,6 @@ diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 47972d69c..9b6644804 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -61,14 +61,14 @@ parameters: path: packages/graphql/src/SchemaPrinter/Misc/DirectiveResolver.php - - message: "#^Parameter \\#2 \\$operators of method LastDragon_ru\\\\LaraASP\\\\GraphQL\\\\SearchBy\\\\Ast\\\\Scalars\\:\\:addScalar\\(\\) expects array\\\\>\\|string, mixed given\\.$#" + message: "#^Call to an undefined method Nuwave\\\\Lighthouse\\\\Schema\\\\Directives\\\\BaseDirective\\:\\:handleBuilder\\(\\)\\.$#" count: 1 - path: packages/graphql/src/SearchBy/Ast/ScalarsTest.php + path: packages/graphql/src/SearchBy/Directives/DirectiveTest.php - - message: "#^Call to an undefined method Nuwave\\\\Lighthouse\\\\Schema\\\\Directives\\\\BaseDirective\\:\\:handleBuilder\\(\\)\\.$#" + message: "#^Parameter \\#2 \\$operators of method LastDragon_ru\\\\LaraASP\\\\GraphQL\\\\SearchBy\\\\Scalars\\:\\:addScalar\\(\\) expects array\\\\>\\|string, mixed given\\.$#" count: 1 - path: packages/graphql/src/SearchBy/Directives/DirectiveTest.php + path: packages/graphql/src/SearchBy/ScalarsTest.php - message: "#^Cannot call method limit\\(\\) on mixed\\.$#" From 17ed998c429065aa33e2635b5a4ba1c11e55248b Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sun, 26 Jun 2022 14:59:53 +0400 Subject: [PATCH 06/13] `@sortBy` will use operators-directives. --- packages/graphql/src/AstManipulator.php | 7 - .../Builder/Directives/HandlerDirective.php | 22 +- .../Builder/Directives/PropertyDirective.php | 5 + .../src/SearchBy/Operators/Property.php | 5 - .../src/SortBy/Builders/Query/Builder.php | 11 +- .../src/SortBy/Builders/Query/BuilderTest.php | 7 +- .../SortByOperatorOrderByDirective.php | 11 + .../Definitions/SortByPropertyDirective.php | 11 + .../src/SortBy/Directives/Directive.php | 79 +---- .../src/SortBy/Directives/DirectiveTest.php | 279 +++++++----------- .../DirectiveTest~full-expected.graphql | 28 +- .../DirectiveTest~registry-expected.graphql | 27 +- .../SortBy/Exceptions/BuilderUnsupported.php | 29 -- .../Exceptions/Client/SortClauseEmpty.php | 14 - .../Exceptions/Client/SortClauseInvalid.php | 31 -- .../Client/SortClauseTooManyProperties.php | 14 - .../Exceptions/Client/SortLogicException.php | 17 -- .../FailedToCreateSortClauseForField.php | 29 -- .../src/SortBy/Exceptions/SortByException.php | 4 +- packages/graphql/src/SortBy/Manipulator.php | 95 +++--- .../src/SortBy/Operators/BaseOperator.php | 29 ++ .../graphql/src/SortBy/Operators/OrderBy.php | 57 ++++ .../src/SortBy/Operators/OrderByTest.php | 171 +++++++++++ .../graphql/src/SortBy/Operators/Property.php | 23 ++ .../graphql/src/SortBy/Types/Direction.php | 44 +++ phpstan-baseline.neon | 10 +- 26 files changed, 578 insertions(+), 481 deletions(-) create mode 100644 packages/graphql/src/SortBy/Definitions/SortByOperatorOrderByDirective.php create mode 100644 packages/graphql/src/SortBy/Definitions/SortByPropertyDirective.php delete mode 100644 packages/graphql/src/SortBy/Exceptions/BuilderUnsupported.php delete mode 100644 packages/graphql/src/SortBy/Exceptions/Client/SortClauseEmpty.php delete mode 100644 packages/graphql/src/SortBy/Exceptions/Client/SortClauseInvalid.php delete mode 100644 packages/graphql/src/SortBy/Exceptions/Client/SortClauseTooManyProperties.php delete mode 100644 packages/graphql/src/SortBy/Exceptions/Client/SortLogicException.php delete mode 100644 packages/graphql/src/SortBy/Exceptions/FailedToCreateSortClauseForField.php create mode 100644 packages/graphql/src/SortBy/Operators/BaseOperator.php create mode 100644 packages/graphql/src/SortBy/Operators/OrderBy.php create mode 100644 packages/graphql/src/SortBy/Operators/OrderByTest.php create mode 100644 packages/graphql/src/SortBy/Operators/Property.php create mode 100644 packages/graphql/src/SortBy/Types/Direction.php diff --git a/packages/graphql/src/AstManipulator.php b/packages/graphql/src/AstManipulator.php index 7adbd9ebe..4ea5fa046 100644 --- a/packages/graphql/src/AstManipulator.php +++ b/packages/graphql/src/AstManipulator.php @@ -43,15 +43,8 @@ public function __construct( protected DocumentAST $document, protected TypeRegistry $types, ) { - $this->addDefaultTypeDefinitions(); - } - - // - // ========================================================================= - protected function addDefaultTypeDefinitions(): void { // empty } - // // // ========================================================================= diff --git a/packages/graphql/src/Builder/Directives/HandlerDirective.php b/packages/graphql/src/Builder/Directives/HandlerDirective.php index 28f51a44b..cb43eefaf 100644 --- a/packages/graphql/src/Builder/Directives/HandlerDirective.php +++ b/packages/graphql/src/Builder/Directives/HandlerDirective.php @@ -2,6 +2,8 @@ namespace LastDragon_ru\LaraASP\GraphQL\Builder\Directives; +use GraphQL\Language\AST\InputValueDefinitionNode; +use GraphQL\Language\AST\ListTypeNode; use Illuminate\Contracts\Container\Container; use Illuminate\Support\Collection; use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; @@ -19,6 +21,7 @@ use function array_keys; use function count; +use function is_array; abstract class HandlerDirective extends BaseDirective implements Handler { public function __construct( @@ -45,12 +48,19 @@ protected function getFactory(): ArgumentFactory { */ protected function handleAnyBuilder(object $builder, mixed $value): object { if ($value !== null) { - $argument = $this->getFactory()->getArgument($this->definitionNode, $value); - - if ($argument->value instanceof ArgumentSet) { - $builder = $this->handle($builder, new Property(), $argument->value); - } else { - throw new HandlerInvalidConditions($this); + $argument = $this->getFactory()->getArgument($this->definitionNode, $value); + $isList = $this->definitionNode instanceof InputValueDefinitionNode + && $this->definitionNode->type instanceof ListTypeNode; + $conditions = $isList && is_array($argument->value) + ? $argument->value + : [$argument->value]; + + foreach ($conditions as $condition) { + if ($condition instanceof ArgumentSet) { + $builder = $this->handle($builder, new Property(), $condition); + } else { + throw new HandlerInvalidConditions($this); + } } } diff --git a/packages/graphql/src/Builder/Directives/PropertyDirective.php b/packages/graphql/src/Builder/Directives/PropertyDirective.php index 5b4fdda25..e48cc3389 100644 --- a/packages/graphql/src/Builder/Directives/PropertyDirective.php +++ b/packages/graphql/src/Builder/Directives/PropertyDirective.php @@ -3,6 +3,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\Builder\Directives; use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler; +use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeProvider; use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\Client\ConditionEmpty; use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\Client\ConditionTooManyOperators; use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\HandlerInvalidConditions; @@ -18,6 +19,10 @@ public static function getName(): string { return 'Property'; } + public function getFieldType(TypeProvider $provider, string $type): ?string { + return null; + } + public function isBuilderSupported(object $builder): bool { return true; } diff --git a/packages/graphql/src/SearchBy/Operators/Property.php b/packages/graphql/src/SearchBy/Operators/Property.php index c976f0591..8ec3726b2 100644 --- a/packages/graphql/src/SearchBy/Operators/Property.php +++ b/packages/graphql/src/SearchBy/Operators/Property.php @@ -3,7 +3,6 @@ namespace LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators; use Illuminate\Support\Str; -use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeProvider; use LastDragon_ru\LaraASP\GraphQL\Builder\Directives\PropertyDirective; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Operator; use LastDragon_ru\LaraASP\GraphQL\SearchBy\Directives\Directive; @@ -22,8 +21,4 @@ public static function getDirectiveName(): string { public function getFieldDescription(): string { return 'Property condition.'; } - - public function getFieldType(TypeProvider $provider, string $type): ?string { - return null; - } } diff --git a/packages/graphql/src/SortBy/Builders/Query/Builder.php b/packages/graphql/src/SortBy/Builders/Query/Builder.php index e7dc20b05..b6eb7d5ec 100644 --- a/packages/graphql/src/SortBy/Builders/Query/Builder.php +++ b/packages/graphql/src/SortBy/Builders/Query/Builder.php @@ -4,10 +4,8 @@ use Illuminate\Database\Query\Builder as QueryBuilder; use LastDragon_ru\LaraASP\GraphQL\SortBy\Builders\Clause; -use LastDragon_ru\LaraASP\GraphQL\SortBy\Exceptions\BuilderUnsupported; -use function count; -use function reset; +use function implode; class Builder { public function __construct() { @@ -21,14 +19,9 @@ public function handle(QueryBuilder $builder, array $clauses): QueryBuilder { foreach ($clauses as $clause) { // Column $path = $clause->getPath(); - $column = reset($path); + $column = implode('.', $path); $direction = $clause->getDirection(); - // Nested? - if (count($path) > 1) { - throw new BuilderUnsupported($builder::class); - } - // Order if ($direction) { $builder = $builder->orderBy($column, $direction); diff --git a/packages/graphql/src/SortBy/Builders/Query/BuilderTest.php b/packages/graphql/src/SortBy/Builders/Query/BuilderTest.php index d49045ee5..372728cc8 100644 --- a/packages/graphql/src/SortBy/Builders/Query/BuilderTest.php +++ b/packages/graphql/src/SortBy/Builders/Query/BuilderTest.php @@ -4,9 +4,7 @@ use Closure; use Exception; -use Illuminate\Database\Query\Builder as QueryBuilder; use LastDragon_ru\LaraASP\GraphQL\SortBy\Builders\Clause; -use LastDragon_ru\LaraASP\GraphQL\SortBy\Exceptions\BuilderUnsupported; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\QueryBuilderDataProvider; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; use LastDragon_ru\LaraASP\Testing\Providers\ArrayDataProvider; @@ -74,7 +72,10 @@ public function dataProviderHandle(): array { ], ], 'nested not supported' => [ - new BuilderUnsupported(QueryBuilder::class), + [ + 'query' => 'select * from "tmp" order by "test"."name" asc', + 'bindings' => [], + ], [ new Clause(['test', 'name'], 'asc'), ], diff --git a/packages/graphql/src/SortBy/Definitions/SortByOperatorOrderByDirective.php b/packages/graphql/src/SortBy/Definitions/SortByOperatorOrderByDirective.php new file mode 100644 index 000000000..d962d631b --- /dev/null +++ b/packages/graphql/src/SortBy/Definitions/SortByOperatorOrderByDirective.php @@ -0,0 +1,11 @@ +container + $this->getContainer() ->make(Manipulator::class, ['document' => $documentAST]) ->update($argDefinition, $parentField); } @@ -63,61 +45,10 @@ public function manipulateArgDefinition( * @return EloquentBuilder|QueryBuilder */ public function handleBuilder($builder, mixed $value): EloquentBuilder|QueryBuilder { - return $builder instanceof EloquentBuilder - ? $this->container->make(SortByEloquentBuilder::class)->handle($builder, $this->getClauses($value)) - : $this->container->make(SortByQueryBuilder::class)->handle($builder, $this->getClauses($value)); + return $this->handleAnyBuilder($builder, $value); } public function handleScoutBuilder(ScoutBuilder $builder, mixed $value): ScoutBuilder { - return $this->container->make(SortByScoutBuilder::class)->handle($builder, $this->getClauses($value)); - } - - /** - * @param array>>> $clauses - * - * @return array - */ - protected function getClauses(array $clauses): array { - // $value = [ - // ['a' => 'desc'] - // ['a' => ['b' => 'ask']] - // ] - $parsed = []; - - foreach ($clauses as $index => $clause) { - // Parse - $path = []; - $direction = null; - - do { - // Empty? - if (!$clause) { - throw new SortClauseEmpty($index, $clauses[$index]); - } - - // More than one property? - if (count($clause) > 1) { - throw new SortClauseTooManyProperties($index, $clauses[$index]); - } - - // Process - $path[] = key($clause); - $clause = array_shift($clause); - - if (!is_array($clause)) { - $direction = $clause; - $clause = null; - } elseif (!$clause) { - throw new SortClauseEmpty($index, $clauses[$index]); - } else { - // empty - } - } while ($clause); - - // Save - $parsed[] = new Clause($path, $direction); - } - - return $parsed; + return $this->handleAnyBuilder($builder, $value); } } diff --git a/packages/graphql/src/SortBy/Directives/DirectiveTest.php b/packages/graphql/src/SortBy/Directives/DirectiveTest.php index ac08db9e2..fa441d971 100644 --- a/packages/graphql/src/SortBy/Directives/DirectiveTest.php +++ b/packages/graphql/src/SortBy/Directives/DirectiveTest.php @@ -4,25 +4,21 @@ use Closure; use Exception; +use GraphQL\Language\Parser; use GraphQL\Type\Definition\InputObjectType; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Query\Builder as QueryBuilder; -use Laravel\Scout\Builder as ScoutBuilder; -use LastDragon_ru\LaraASP\GraphQL\SortBy\Builders\Clause; -use LastDragon_ru\LaraASP\GraphQL\SortBy\Builders\Eloquent\Builder as SortByEloquentBuilder; -use LastDragon_ru\LaraASP\GraphQL\SortBy\Builders\Query\Builder as SortByQueryBuilder; -use LastDragon_ru\LaraASP\GraphQL\SortBy\Builders\Scout\Builder as SortByScoutBuilder; -use LastDragon_ru\LaraASP\GraphQL\SortBy\Exceptions\Client\SortClauseEmpty; -use LastDragon_ru\LaraASP\GraphQL\SortBy\Exceptions\Client\SortClauseTooManyProperties; +use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\Client\ConditionTooManyProperties; use LastDragon_ru\LaraASP\GraphQL\SortBy\Exceptions\FailedToCreateSortClause; use LastDragon_ru\LaraASP\GraphQL\Testing\GraphQLExpectedSchema; +use LastDragon_ru\LaraASP\GraphQL\Testing\Package\BuilderDataProvider; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; -use Mockery; -use Mockery\MockInterface; +use LastDragon_ru\LaraASP\Testing\Providers\ArrayDataProvider; +use LastDragon_ru\LaraASP\Testing\Providers\CompositeDataProvider; use Nuwave\Lighthouse\Schema\TypeRegistry; +use function is_array; + /** * @internal * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SortBy\Directives\Directive @@ -35,9 +31,14 @@ class DirectiveTest extends TestCase { * * @dataProvider dataProviderManipulateArgDefinition * - * @param Closure(self): GraphQLExpectedSchema $expected + * @param Closure(static): GraphQLExpectedSchema $expected + * @param Closure(static): void $prepare */ - public function testManipulateArgDefinition(Closure $expected, string $graphql): void { + public function testManipulateArgDefinition(Closure $expected, string $graphql, ?Closure $prepare = null): void { + if ($prepare) { + $prepare($this); + } + self::assertGraphQLSchemaEquals( $expected($this), $this->getTestData()->file($graphql), @@ -143,111 +144,48 @@ public function testManipulateArgDefinitionTypeRegistryEmpty(): void { } /** - * @covers ::getClauses + * @covers ::handleBuilder * - * @dataProvider dataProviderGetClauses + * @dataProvider dataProviderHandleBuilder * - * @param Exception|array,?string}> $expected - * @param array $clauses + * @param array{query: string, bindings: array}|Exception $expected + * @param Closure(static): object $builderFactory */ - public function testGetClauses(Exception|array $expected, array $clauses): void { + public function testHandleBuilder( + array|Exception $expected, + Closure $builderFactory, + mixed $value, + ): void { if ($expected instanceof Exception) { self::expectExceptionObject($expected); } - $directive = new class() extends Directive { - /** @noinspection PhpMissingParentConstructorInspection */ - public function __construct() { - // empty + $this->useGraphQLSchema( + /** @lang GraphQL */ + <<<'GRAPHQL' + type Query { + test(input: Test @sortBy): String! @mock } - /** - * @inheritDoc - */ - public function getClauses(array $clauses): array { - return parent::getClauses($clauses); + input Test { + a: Int! + b: String } - }; - - self::assertEquals($expected, $directive->getClauses($clauses)); - } - - /** - * @covers ::handleBuilder - */ - public function testHandleBuilderQuery(): void { - $directive = $this->app->make(Directive::class); - $builder = Mockery::mock(QueryBuilder::class); - $clauses = [ - new Clause(['a'], null), - ]; - - $this->override( - SortByQueryBuilder::class, - static function (MockInterface $mock) use ($builder, $clauses): void { - $mock - ->shouldReceive('handle') - ->with($builder, $clauses) - ->once() - ->andReturn($builder); - }, - ); - - $directive->handleBuilder($builder, [ - ['a' => null], - ]); - } - - /** - * @covers ::handleBuilder - */ - public function testHandleBuilderEloquent(): void { - $directive = $this->app->make(Directive::class); - $builder = Mockery::mock(EloquentBuilder::class); - $clauses = [ - new Clause(['a'], null), - ]; - - $this->override( - SortByEloquentBuilder::class, - static function (MockInterface $mock) use ($builder, $clauses): void { - $mock - ->shouldReceive('handle') - ->with($builder, $clauses) - ->once() - ->andReturn($builder); - }, + GRAPHQL, ); - $directive->handleBuilder($builder, [ - ['a' => null], - ]); - } - - /** - * @covers ::handleScoutBuilder - */ - public function testHandleScoutBuilder(): void { - $directive = $this->app->make(Directive::class); - $builder = Mockery::mock(ScoutBuilder::class); - $clauses = [ - new Clause(['a'], null), - ]; + $definitionNode = Parser::inputValueDefinition('input: [SortByClauseTest!]'); + $directiveNode = Parser::directive('@test'); + $directive = $this->app->make(Directive::class)->hydrate($directiveNode, $definitionNode); + $builder = $builderFactory($this); + $actual = $directive->handleBuilder($builder, $value); - $this->override( - SortByScoutBuilder::class, - static function (MockInterface $mock) use ($builder, $clauses): void { - $mock - ->shouldReceive('handle') - ->with($builder, $clauses) - ->once() - ->andReturn($builder); - }, - ); - - $directive->handleScoutBuilder($builder, [ - ['a' => null], - ]); + if (is_array($expected)) { + self::assertInstanceOf($builder::class, $actual); + self::assertDatabaseQueryEquals($expected, $actual); + } else { + self::fail('Something wrong...'); + } } // @@ -274,106 +212,97 @@ static function (self $test): GraphQLExpectedSchema { ]); }, '~full.graphql', + null, ], ]; } /** - * @return array,?string}>|array}> + * @return array */ - public function dataProviderGetClauses(): array { - return [ - 'no conditions' => [ - [], - [], - ], - 'empty' => [ - new SortClauseEmpty(0, []), - [ - [], - ], - ], - 'empty nested' => [ - new SortClauseEmpty(0, ['a' => []]), - [ + public function dataProviderHandleBuilder(): array { + return (new CompositeDataProvider( + new BuilderDataProvider(), + new ArrayDataProvider([ + 'empty' => [ [ - 'a' => [], + 'query' => <<<'SQL' + select + * + from + "tmp" + SQL + , + 'bindings' => [], ], - ], - ], - 'empty nested nested' => [ - new SortClauseEmpty(0, ['a' => ['b' => []]]), - [ [ - 'a' => ['b' => []], + // empty ], ], - ], - 'more than one' => [ - new SortClauseTooManyProperties(1, [ - [ - 'a' => 'desc', - 'b' => 'desc', - ], - ]), - [ + 'empty operators' => [ [ - 'a' => 'desc', + 'query' => <<<'SQL' + select + * + from + "tmp" + SQL + , + 'bindings' => [], ], [ - 'a' => 'desc', - 'b' => 'desc', + [ + // empty + ], ], ], - ], - 'more than one nested' => [ - new SortClauseTooManyProperties(1, [ - 'a' => [ - 'a' => 'desc', - 'b' => 'desc', - ], - ]), - [ + 'too many properties' => [ + new ConditionTooManyProperties(['a', 'b']), [ - 'a' => 'desc', - ], - [ - 'a' => [ - 'a' => 'desc', + [ + 'a' => 'asc', 'b' => 'desc', ], ], ], - ], - 'clause' => [ - [ - new Clause(['a'], null), - new Clause(['a'], 'desc'), - new Clause(['b', 'c'], 'asc'), - new Clause(['b', 'd', 'e'], 'desc'), - ], - [ + 'null' => [ [ - 'a' => null, + 'query' => <<<'SQL' + select + * + from + "tmp" + SQL + , + 'bindings' => [], ], + null, + ], + 'valid condition' => [ [ - 'a' => 'desc', + 'query' => <<<'SQL' + select + * + from + "tmp" + order by + "a" asc, + "b" desc + SQL + , + 'bindings' => [], ], [ - 'b' => [ - 'c' => 'asc', + [ + 'a' => 'asc', ], - ], - [ - 'b' => [ - 'd' => [ - 'e' => 'desc', - ], + [ + 'b' => 'desc', ], ], ], - ], - ]; + ]), + ))->getData(); } // } diff --git a/packages/graphql/src/SortBy/Directives/DirectiveTest~full-expected.graphql b/packages/graphql/src/SortBy/Directives/DirectiveTest~full-expected.graphql index aecad1d3f..5ed8c3642 100644 --- a/packages/graphql/src/SortBy/Directives/DirectiveTest~full-expected.graphql +++ b/packages/graphql/src/SortBy/Directives/DirectiveTest~full-expected.graphql @@ -1,7 +1,7 @@ """ Sort direction. """ -enum SortByDirection { +enum SortByTypeDirection { asc desc } @@ -14,11 +14,13 @@ input SortByClauseNested { Property clause. """ nested: SortByClauseNested + @sortByProperty """ Property clause. """ - value: SortByDirection + value: SortByTypeDirection + @sortByOperatorOrderBy } """ @@ -28,32 +30,38 @@ input SortByClauseProperties { """ Property clause. """ - enum: SortByDirection + enum: SortByTypeDirection + @sortByOperatorOrderBy """ Property clause. """ - enumNotNull: SortByDirection + enumNotNull: SortByTypeDirection + @sortByOperatorOrderBy """ Property clause. """ - idScalar: SortByDirection + idScalar: SortByTypeDirection + @sortByOperatorOrderBy """ Property clause. """ - idScalarNotNull: SortByDirection + idScalarNotNull: SortByTypeDirection + @sortByOperatorOrderBy """ Property clause. """ nested: SortByClauseNested + @sortByProperty """ Property clause. """ nestedNotNull: SortByClauseNested + @sortByProperty } type Query { @@ -70,3 +78,11 @@ Convert Input into Sort Clause. directive @sortBy on | ARGUMENT_DEFINITION + +directive @sortByOperatorOrderBy +on + | INPUT_FIELD_DEFINITION + +directive @sortByProperty +on + | INPUT_FIELD_DEFINITION diff --git a/packages/graphql/src/SortBy/Directives/DirectiveTest~registry-expected.graphql b/packages/graphql/src/SortBy/Directives/DirectiveTest~registry-expected.graphql index 03f002662..d0ecf3d7e 100644 --- a/packages/graphql/src/SortBy/Directives/DirectiveTest~registry-expected.graphql +++ b/packages/graphql/src/SortBy/Directives/DirectiveTest~registry-expected.graphql @@ -1,7 +1,7 @@ """ Sort direction. """ -enum SortByDirection { +enum SortByTypeDirection { asc desc } @@ -13,12 +13,14 @@ input SortByClauseA { """ Property clause. """ - flag: SortByDirection + flag: SortByTypeDirection + @sortByOperatorOrderBy """ Property clause. """ - name: SortByDirection + name: SortByTypeDirection + @sortByOperatorOrderBy } """ @@ -29,11 +31,13 @@ input SortByClauseB { Property clause. """ child: SortByClauseA + @sortByProperty """ Property clause. """ - name: SortByDirection + name: SortByTypeDirection + @sortByOperatorOrderBy } """ @@ -43,12 +47,14 @@ input SortByClauseC { """ Property clause. """ - flag: SortByDirection + flag: SortByTypeDirection + @sortByOperatorOrderBy """ Property clause. """ - name: SortByDirection + name: SortByTypeDirection + @sortByOperatorOrderBy } """ @@ -59,6 +65,7 @@ input SortByClauseD { Property clause. """ child: SortByClauseC + @sortByProperty } type C { @@ -97,3 +104,11 @@ Convert Input into Sort Clause. directive @sortBy on | ARGUMENT_DEFINITION + +directive @sortByOperatorOrderBy +on + | INPUT_FIELD_DEFINITION + +directive @sortByProperty +on + | INPUT_FIELD_DEFINITION diff --git a/packages/graphql/src/SortBy/Exceptions/BuilderUnsupported.php b/packages/graphql/src/SortBy/Exceptions/BuilderUnsupported.php deleted file mode 100644 index 97ac9fe9a..000000000 --- a/packages/graphql/src/SortBy/Exceptions/BuilderUnsupported.php +++ /dev/null @@ -1,29 +0,0 @@ -builder, - ), $previous); - } - - /** - * @return class-string - */ - public function getBuilder(): string { - return $this->builder; - } -} diff --git a/packages/graphql/src/SortBy/Exceptions/Client/SortClauseEmpty.php b/packages/graphql/src/SortBy/Exceptions/Client/SortClauseEmpty.php deleted file mode 100644 index 903cb75f5..000000000 --- a/packages/graphql/src/SortBy/Exceptions/Client/SortClauseEmpty.php +++ /dev/null @@ -1,14 +0,0 @@ -getIndex(), - ); - } -} diff --git a/packages/graphql/src/SortBy/Exceptions/Client/SortClauseInvalid.php b/packages/graphql/src/SortBy/Exceptions/Client/SortClauseInvalid.php deleted file mode 100644 index 10dfe2753..000000000 --- a/packages/graphql/src/SortBy/Exceptions/Client/SortClauseInvalid.php +++ /dev/null @@ -1,31 +0,0 @@ - $clause - */ - public function __construct( - protected int|string $index, - protected array $clause, - Throwable $previous = null, - ) { - parent::__construct($this->getReason(), $previous); - } - - abstract protected function getReason(): string; - - public function getIndex(): int|string { - return $this->index; - } - - /** - * @return array - */ - public function getClause(): array { - return $this->clause; - } -} diff --git a/packages/graphql/src/SortBy/Exceptions/Client/SortClauseTooManyProperties.php b/packages/graphql/src/SortBy/Exceptions/Client/SortClauseTooManyProperties.php deleted file mode 100644 index c71260d6d..000000000 --- a/packages/graphql/src/SortBy/Exceptions/Client/SortClauseTooManyProperties.php +++ /dev/null @@ -1,14 +0,0 @@ -getIndex(), - ); - } -} diff --git a/packages/graphql/src/SortBy/Exceptions/Client/SortLogicException.php b/packages/graphql/src/SortBy/Exceptions/Client/SortLogicException.php deleted file mode 100644 index 99c0ac1c7..000000000 --- a/packages/graphql/src/SortBy/Exceptions/Client/SortLogicException.php +++ /dev/null @@ -1,17 +0,0 @@ -field, - $this->type, - ), $previous); - } - - public function getType(): string { - return $this->type; - } - - public function getField(): string { - return $this->field; - } -} diff --git a/packages/graphql/src/SortBy/Exceptions/SortByException.php b/packages/graphql/src/SortBy/Exceptions/SortByException.php index 4459f5f39..15264ec25 100644 --- a/packages/graphql/src/SortBy/Exceptions/SortByException.php +++ b/packages/graphql/src/SortBy/Exceptions/SortByException.php @@ -2,8 +2,8 @@ namespace LastDragon_ru\LaraASP\GraphQL\SortBy\Exceptions; -use LastDragon_ru\LaraASP\GraphQL\PackageException; +use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\BuilderException; -abstract class SortByException extends PackageException { +abstract class SortByException extends BuilderException { // empty } diff --git a/packages/graphql/src/SortBy/Manipulator.php b/packages/graphql/src/SortBy/Manipulator.php index 66b3f7afe..7bc7a8bd1 100644 --- a/packages/graphql/src/SortBy/Manipulator.php +++ b/packages/graphql/src/SortBy/Manipulator.php @@ -14,11 +14,14 @@ use GraphQL\Type\Definition\InputObjectType; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; -use LastDragon_ru\LaraASP\GraphQL\AstManipulator; +use Illuminate\Support\Str; +use LastDragon_ru\LaraASP\GraphQL\Builder\Manipulator as BuilderManipulator; use LastDragon_ru\LaraASP\GraphQL\SortBy\Contracts\Unsortable; use LastDragon_ru\LaraASP\GraphQL\SortBy\Directives\Directive; use LastDragon_ru\LaraASP\GraphQL\SortBy\Exceptions\FailedToCreateSortClause; -use LastDragon_ru\LaraASP\GraphQL\SortBy\Exceptions\FailedToCreateSortClauseForField; +use LastDragon_ru\LaraASP\GraphQL\SortBy\Operators\OrderBy; +use LastDragon_ru\LaraASP\GraphQL\SortBy\Operators\Property; +use LastDragon_ru\LaraASP\GraphQL\SortBy\Types\Direction; use Nuwave\Lighthouse\Pagination\PaginateDirective; use Nuwave\Lighthouse\Pagination\PaginationType; use Nuwave\Lighthouse\Support\Contracts\FieldResolver; @@ -28,7 +31,7 @@ use function mb_substr; use function str_starts_with; -class Manipulator extends AstManipulator { +class Manipulator extends BuilderManipulator { // // ========================================================================= public function update(InputValueDefinitionNode $node, FieldDefinitionNode $query): void { @@ -74,30 +77,34 @@ protected function getInputType( InputObjectTypeDefinitionNode|ObjectTypeDefinitionNode|InputObjectType|ObjectType $node, ): ?string { // Exists? - $name = $this->getTypeName($node); + $name = $this->getInputTypeName($node); if ($this->isTypeDefinitionExists($name)) { return $name; } // Add type - $type = $this->addTypeDefinition(Parser::inputObjectTypeDefinition( - <<getNodeTypeFullName($node)} (only one property allowed at a time). - """ - input {$name} { + $type = $this->addTypeDefinition( + Parser::inputObjectTypeDefinition( + <<getNodeTypeFullName($node)} (only one property allowed at a time). """ - dummy: ID - } - DEF, - )); + input {$name} { + """ + If you see this probably something wrong. Please contact to developer. + """ + dummy: ID + } + DEF, + ), + ); // Add sortable fields - $description = 'Property clause.'; - $fields = $node instanceof InputObjectType || $node instanceof ObjectType + $direction = $this->getType(Direction::class); + $operator = $this->getContainer()->make(OrderBy::class); + $property = $this->getContainer()->make(Property::class); + $fields = $node instanceof InputObjectType || $node instanceof ObjectType ? $node->getFields() : $node->fields; @@ -121,22 +128,30 @@ protected function getInputType( } // Is supported? - $fieldDefinition = Directive::TypeDirection; - $fieldTypeNode = $this->getTypeDefinitionNode($field); - $isNested = $fieldTypeNode instanceof InputObjectTypeDefinitionNode + $fieldType = $direction; + $fieldOperator = $operator; + $fieldTypeNode = $this->getTypeDefinitionNode($field); + $isNested = $fieldTypeNode instanceof InputObjectTypeDefinitionNode || $fieldTypeNode instanceof ObjectTypeDefinitionNode || $fieldTypeNode instanceof InputObjectType || $fieldTypeNode instanceof ObjectType; if ($isNested) { - $fieldDefinition = $this->getInputType($fieldTypeNode); + $fieldType = $this->getInputType($fieldTypeNode); + $fieldOperator = $property; } else { // empty } // Create new Field - if (!$fieldDefinition || !$this->copyFieldToType($type, $field, $fieldDefinition, $description)) { - throw new FailedToCreateSortClauseForField($this->getNodeName($node), $this->getNodeName($field)); + if ($fieldType) { + $type->fields[] = Parser::inputValueDefinition( + $this->getOperatorField( + $fieldOperator, + $this->getTypeDefinitionNode($fieldType), + $this->getNodeName($field), + ), + ); } } @@ -155,28 +170,6 @@ protected function getInputType( } // - // - // ========================================================================= - protected function addDefaultTypeDefinitions(): void { - $name = Directive::TypeDirection; - - if (!$this->isTypeDefinitionExists($name)) { - $this->addTypeDefinition(Parser::enumTypeDefinition( - /** @lang GraphQL */ - << - // // ========================================================================= protected function isTypeName( @@ -185,7 +178,11 @@ protected function isTypeName( return str_starts_with($this->getNodeTypeName($node), Directive::Name); } - protected function getTypeName( + protected function getTypeName(string $name, string $scalar = null, bool $nullable = null): string { + return Directive::Name.'Type'.Str::studly($name); + } + + protected function getInputTypeName( InputObjectTypeDefinitionNode|ObjectTypeDefinitionNode|InputObjectType|ObjectType $node, ): string { return Directive::Name."Clause{$this->getNodeName($node)}"; @@ -207,11 +204,11 @@ public function getPaginationType(PaginateDirective $directive): PaginationType })->getPaginationType($paginate); if ($pagination->isPaginator()) { - $type = mb_substr($type, 0, - mb_strlen('Paginator')); + $type = mb_substr($type, 0, -mb_strlen('Paginator')); } elseif ($pagination->isSimple()) { - $type = mb_substr($type, 0, - mb_strlen('SimplePaginator')); + $type = mb_substr($type, 0, -mb_strlen('SimplePaginator')); } elseif ($pagination->isConnection()) { - $type = mb_substr($type, 0, - mb_strlen('Connection')); + $type = mb_substr($type, 0, -mb_strlen('Connection')); } else { // empty } diff --git a/packages/graphql/src/SortBy/Operators/BaseOperator.php b/packages/graphql/src/SortBy/Operators/BaseOperator.php new file mode 100644 index 000000000..d5aeda18d --- /dev/null +++ b/packages/graphql/src/SortBy/Operators/BaseOperator.php @@ -0,0 +1,29 @@ +getType(Direction::class); + } + + public function getFieldDescription(): string { + return 'Property clause.'; + } + + public function call(Handler $handler, object $builder, Property $property, Argument $argument): object { + $direction = Cast::toString($argument->value); + $clauses = [new Clause($property->getPath(), $direction)]; + + if ($builder instanceof EloquentBuilder) { + $this->eloquent->handle($builder, $clauses); + } elseif ($builder instanceof QueryBuilder) { + $this->query->handle($builder, $clauses); + } elseif ($builder instanceof ScoutBuilder) { + $this->scout->handle($builder, $clauses); + } else { + throw new OperatorUnsupportedBuilder($this, $builder); + } + + return $builder; + } +} diff --git a/packages/graphql/src/SortBy/Operators/OrderByTest.php b/packages/graphql/src/SortBy/Operators/OrderByTest.php new file mode 100644 index 000000000..03d704749 --- /dev/null +++ b/packages/graphql/src/SortBy/Operators/OrderByTest.php @@ -0,0 +1,171 @@ + + // ========================================================================= + /** + * @covers ::call + * + * @dataProvider dataProviderCall + * + * @param array{query: string, bindings: array} $expected + * @param BuilderFactory $builderFactory + * @param Closure(static): Argument $argumentFactory + */ + public function testCall( + array $expected, + Closure $builderFactory, + Property $property, + Closure $argumentFactory, + ): void { + $operator = $this->app->make(OrderBy::class); + $argument = $argumentFactory($this); + $directive = $this->app->make(Directive::class); + $builder = $builderFactory($this); + $builder = $operator->call($directive, $builder, $property, $argument); + + self::assertDatabaseQueryEquals($expected, $builder); + } + + public function testCallEloquentBuilder(): void { + $this->useGraphQLSchema('type Query { test: String! @mock}'); + + $this->override(EloquentHandler::class, static function (MockInterface $mock): void { + $mock + ->shouldReceive('handle') + ->once(); + }); + $this->override(QueryHandler::class); + $this->override(ScoutHandler::class); + + $directive = $this->app->make(Directive::class); + $property = new Property(); + $operator = $this->app->make(OrderBy::class); + $argument = $this->getGraphQLArgument( + 'Test', + 'asc', + 'enum Test { asc }', + ); + $builder = Mockery::mock(EloquentBuilder::class); + + $operator->call($directive, $builder, $property, $argument); + } + + public function testCallQueryBuilder(): void { + $this->useGraphQLSchema('type Query { test: String! @mock}'); + + $this->override(EloquentHandler::class); + $this->override(QueryHandler::class, static function (MockInterface $mock): void { + $mock + ->shouldReceive('handle') + ->once(); + }); + $this->override(ScoutHandler::class); + + $directive = $this->app->make(Directive::class); + $property = new Property(); + $operator = $this->app->make(OrderBy::class); + $argument = $this->getGraphQLArgument( + 'Test', + 'asc', + ); + $builder = Mockery::mock(QueryBuilder::class); + + $operator->call($directive, $builder, $property, $argument); + } + + public function testCallScoutBuilder(): void { + $this->useGraphQLSchema('type Query { test: String! @mock}'); + + $this->override(EloquentHandler::class); + $this->override(QueryHandler::class); + $this->override(ScoutHandler::class, static function (MockInterface $mock): void { + $mock + ->shouldReceive('handle') + ->once(); + }); + + $directive = $this->app->make(Directive::class); + $property = new Property(); + $operator = $this->app->make(OrderBy::class); + $argument = $this->getGraphQLArgument( + 'Test', + 'asc', + 'enum Test { asc }', + ); + $builder = Mockery::mock(ScoutBuilder::class); + + $operator->call($directive, $builder, $property, $argument); + } + // + + // + // ========================================================================= + /** + * @return array + */ + public function dataProviderCall(): array { + $factory = static function (self $test): Argument { + $schema = (string) $test->printGraphQLSchema( + /** @lang GraphQL */ + <<<'GRAPHQL' + type Query { + test(input: Test @sortBy): String! @mock + } + + input Test { + a: Int! + b: String + } + GRAPHQL, + ); + $argument = $test->getGraphQLArgument( + 'SortByTypeDirection!', + 'desc', + $schema, + ); + + return $argument; + }; + + return (new CompositeDataProvider( + new BuilderDataProvider(), + new ArrayDataProvider([ + 'property' => [ + [ + 'query' => 'select * from "tmp" order by "a" desc', + 'bindings' => [], + ], + new Property('a'), + $factory, + ], + ]), + ))->getData(); + } + // +} diff --git a/packages/graphql/src/SortBy/Operators/Property.php b/packages/graphql/src/SortBy/Operators/Property.php new file mode 100644 index 000000000..52713cfd9 --- /dev/null +++ b/packages/graphql/src/SortBy/Operators/Property.php @@ -0,0 +1,23 @@ +\\|string\\>\\|string\\>\\>, mixed given\\.$#" - count: 3 - path: packages/graphql/src/SortBy/Directives/Directive.php + message: "#^Call to an undefined method Nuwave\\\\Lighthouse\\\\Schema\\\\Directives\\\\BaseDirective\\:\\:handleBuilder\\(\\)\\.$#" + count: 1 + path: packages/graphql/src/SortBy/Directives/DirectiveTest.php - - message: "#^Parameter \\#1 \\$clauses of method class@anonymous/graphql/src/SortBy/Directives/DirectiveTest\\.php\\:158\\:\\:getClauses\\(\\) expects array\\\\|string\\>\\|string\\>\\>, array given\\.$#" + message: "#^Parameter \\#1 \\$path of class LastDragon_ru\\\\LaraASP\\\\GraphQL\\\\SortBy\\\\Builders\\\\Clause constructor expects non\\-empty\\-array\\, array\\ given\\.$#" count: 1 - path: packages/graphql/src/SortBy/Directives/DirectiveTest.php + path: packages/graphql/src/SortBy/Operators/OrderBy.php - message: "#^Method LastDragon_ru\\\\LaraASP\\\\GraphQL\\\\Testing\\\\Package\\\\Models\\\\Image\\:\\:imageable\\(\\) should return Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\MorphTo\\ but returns Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\MorphTo\\\\.$#" From b6f30ebae107d6e5239178d9e433d030aeaf4737 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 2 Jul 2022 09:06:13 +0400 Subject: [PATCH 07/13] `AstManipulator::copyFieldToType()` removed. --- packages/graphql/src/AstManipulator.php | 35 ------------------------- 1 file changed, 35 deletions(-) diff --git a/packages/graphql/src/AstManipulator.php b/packages/graphql/src/AstManipulator.php index 4ea5fa046..ec6bd5797 100644 --- a/packages/graphql/src/AstManipulator.php +++ b/packages/graphql/src/AstManipulator.php @@ -15,7 +15,6 @@ use GraphQL\Language\AST\ScalarTypeDefinitionNode; use GraphQL\Language\AST\TypeDefinitionNode; use GraphQL\Language\AST\UnionTypeDefinitionNode; -use GraphQL\Language\Parser; use GraphQL\Type\Definition\EnumType; use GraphQL\Type\Definition\FieldDefinition; use GraphQL\Type\Definition\InputObjectField; @@ -210,39 +209,5 @@ public function getNodeTypeFullName( return trim("{$prefix} {$name}"); } - - protected function copyFieldToType( - InputObjectTypeDefinitionNode $type, - InputValueDefinitionNode|FieldDefinitionNode|InputObjectField|FieldDefinition $field, - string $newFieldType, - string $newFieldDescription, - ): bool { - $newField = null; - - if ($field instanceof InputValueDefinitionNode) { - $clone = $field->cloneDeep(); - - if ($clone instanceof InputValueDefinitionNode) { - $clone->type = Parser::typeReference($newFieldType); - $clone->description = Parser::description("\"\"\"{$newFieldDescription}\"\"\""); - $newField = $clone; - } - } else { - $newField = Parser::inputValueDefinition( - <<getNodeName($field)}: {$newFieldType} - DEF, - ); - } - - if ($newField) { - $type->fields[] = $newField; - } - - return (bool) $newField; - } // } From 63b0935c4cb11ddada1a99c7af19a64907b5c669 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 2 Jul 2022 09:28:51 +0400 Subject: [PATCH 08/13] Removed `Clause`. --- .../graphql/src/SortBy/Builders/Clause.php | 26 --- .../src/SortBy/Builders/Eloquent/Builder.php | 35 ++- .../SortBy/Builders/Eloquent/BuilderTest.php | 212 ++++-------------- .../src/SortBy/Builders/Query/Builder.php | 26 +-- .../src/SortBy/Builders/Query/BuilderTest.php | 29 +-- .../src/SortBy/Builders/Scout/Builder.php | 30 +-- .../src/SortBy/Builders/Scout/BuilderTest.php | 43 +--- .../graphql/src/SortBy/Operators/OrderBy.php | 8 +- phpstan-baseline.neon | 5 - 9 files changed, 108 insertions(+), 306 deletions(-) delete mode 100644 packages/graphql/src/SortBy/Builders/Clause.php diff --git a/packages/graphql/src/SortBy/Builders/Clause.php b/packages/graphql/src/SortBy/Builders/Clause.php deleted file mode 100644 index ba5e602f7..000000000 --- a/packages/graphql/src/SortBy/Builders/Clause.php +++ /dev/null @@ -1,26 +0,0 @@ - $path - */ - public function __construct( - protected array $path, - protected ?string $direction, - ) { - // empty - } - - /** - * @return non-empty-array - */ - public function getPath(): array { - return $this->path; - } - - public function getDirection(): ?string { - return $this->direction; - } -} diff --git a/packages/graphql/src/SortBy/Builders/Eloquent/Builder.php b/packages/graphql/src/SortBy/Builders/Eloquent/Builder.php index bea1f7fc8..e478975e6 100644 --- a/packages/graphql/src/SortBy/Builders/Eloquent/Builder.php +++ b/packages/graphql/src/SortBy/Builders/Eloquent/Builder.php @@ -16,8 +16,9 @@ use Illuminate\Database\Eloquent\Relations\MorphToMany; use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Database\Query\JoinClause; +use LastDragon_ru\LaraASP\Core\Utils\Cast; use LastDragon_ru\LaraASP\Eloquent\ModelHelper; -use LastDragon_ru\LaraASP\GraphQL\SortBy\Builders\Clause; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\SortBy\Exceptions\RelationUnsupported; use LogicException; @@ -51,28 +52,24 @@ public function __construct() { // ========================================================================= /** * @param EloquentBuilder $builder - * @param array $clauses * * @return EloquentBuilder */ - public function handle(EloquentBuilder $builder, array $clauses): EloquentBuilder { - foreach ($clauses as $clause) { - // Column - $path = $clause->getPath(); - $column = end($path); - $relation = array_slice($path, 0, -1); - $direction = $clause->getDirection(); - - if ($relation) { - $column = $this->processRelation($builder, $relation, $column, $direction); - } + public function handle(EloquentBuilder $builder, Property $property, string $direction): EloquentBuilder { + // Column + $path = $property->getPath(); + $column = Cast::toString(end($path)); + $relation = array_slice($path, 0, -1); + + if ($relation) { + $column = $this->processRelation($builder, $relation, $column, $direction); + } - // Order - if ($direction) { - $builder = $builder->orderBy($column, $direction); - } else { - $builder = $builder->orderBy($column); - } + // Order + if ($direction) { + $builder = $builder->orderBy($column, $direction); + } else { + $builder = $builder->orderBy($column); } return $builder; diff --git a/packages/graphql/src/SortBy/Builders/Eloquent/BuilderTest.php b/packages/graphql/src/SortBy/Builders/Eloquent/BuilderTest.php index 57bf38eed..0fb62d0a9 100644 --- a/packages/graphql/src/SortBy/Builders/Eloquent/BuilderTest.php +++ b/packages/graphql/src/SortBy/Builders/Eloquent/BuilderTest.php @@ -15,7 +15,7 @@ use Illuminate\Database\Eloquent\Relations\MorphOne; use Illuminate\Database\Eloquent\Relations\MorphToMany; use LastDragon_ru\LaraASP\Eloquent\Exceptions\PropertyIsNotRelation; -use LastDragon_ru\LaraASP\GraphQL\SortBy\Builders\Clause; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\SortBy\Exceptions\RelationUnsupported; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\EloquentBuilderDataProvider; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\Models\Car; @@ -45,15 +45,19 @@ class BuilderTest extends TestCase { * * @param array{query: string, bindings: array}|Exception $expected * @param BuilderFactory $builder - * @param array $clauses */ - public function testHandle(array|Exception $expected, Closure $builder, array $clauses): void { + public function testHandle( + array|Exception $expected, + Closure $builder, + Property $property, + string $direction, + ): void { if ($expected instanceof Exception) { self::expectExceptionObject($expected); } $builder = $builder($this); - $builder = $this->app->make(Builder::class)->handle($builder, $clauses); + $builder = $this->app->make(Builder::class)->handle($builder, $property, $direction); if (is_array($expected)) { self::assertDatabaseQueryEquals($expected, $builder); @@ -73,12 +77,13 @@ public function dataProviderHandle(): array { 'Both' => (new CompositeDataProvider( new EloquentBuilderDataProvider(), new ArrayDataProvider([ - 'empty' => [ + 'simple condition' => [ [ - 'query' => 'select * from "tmp"', + 'query' => 'select * from "tmp" order by "name" desc', 'bindings' => [], ], - [], + new Property('name'), + 'desc', ], ]), )), @@ -88,9 +93,8 @@ public function dataProviderHandle(): array { static function (): EloquentBuilder { return User::query(); }, - [ - new Clause(['unknown', 'name'], 'asc'), - ], + new Property('unknown', 'name'), + 'asc', ], 'unsupported' => [ new RelationUnsupported( @@ -111,22 +115,8 @@ static function (): EloquentBuilder { static function (): EloquentBuilder { return User::query(); }, - [ - new Clause(['unsupported', 'id'], 'asc'), - ], - ], - 'simple condition' => [ - [ - 'query' => 'select * from "users" order by "name" desc, "id" asc', - 'bindings' => [], - ], - static function (): EloquentBuilder { - return User::query(); - }, - [ - new Clause(['name'], 'desc'), - new Clause(['id'], 'asc'), - ], + new Property('unsupported', 'id'), + 'asc', ], BelongsTo::class => [ [ @@ -136,19 +126,6 @@ static function (): EloquentBuilder { from "cars" order by - ( - select - "users"."name" - from - "users" - where - "cars"."foreignKey" = "users"."ownerKey" - and "deleted_at" is null - order by - "users"."name" asc - limit - 1 - ) asc, ( select "sort_by_organization"."name" @@ -168,8 +145,7 @@ static function (): EloquentBuilder { "sort_by_organization"."name" desc limit 1 - ) desc, - "name" asc + ) desc SQL , 'bindings' => [ @@ -179,11 +155,8 @@ static function (): EloquentBuilder { static function (): EloquentBuilder { return Car::query(); }, - [ - new Clause(['user', 'name'], 'asc'), - new Clause(['user', 'organization', 'name'], 'desc'), - new Clause(['name'], 'asc'), - ], + new Property('user', 'organization', 'name'), + 'desc', ], HasOne::class => [ [ @@ -193,19 +166,6 @@ static function (): EloquentBuilder { from "users" order by - ( - select - "cars"."name" - from - "cars" - where - "users"."localKey" = "cars"."foreignKey" - and "favorite" = ? - order by - "cars"."name" desc - limit - 1 - ) desc, ( select "sort_by_engine"."id" @@ -226,24 +186,19 @@ static function (): EloquentBuilder { "sort_by_engine"."id" asc limit 1 - ) asc, - "name" asc + ) asc SQL , 'bindings' => [ 1, 1, - 1, ], ], static function (): EloquentBuilder { return User::query(); }, - [ - new Clause(['car', 'name'], 'desc'), - new Clause(['car', 'engine', 'id'], 'asc'), - new Clause(['name'], 'asc'), - ], + new Property('car', 'engine', 'id'), + 'asc', ], HasMany::class => [ [ @@ -265,21 +220,7 @@ static function (): EloquentBuilder { "cars"."name" asc limit 1 - ) asc, - ( - select - "cars"."engines" - from - "cars" - where - "users"."localKey" = "cars"."foreignKey" - and "deleted_at" is null - order by - "cars"."engines" desc - limit - 1 - ) desc, - "name" asc + ) asc SQL , 'bindings' => [ @@ -289,11 +230,8 @@ static function (): EloquentBuilder { static function (): EloquentBuilder { return User::query(); }, - [ - new Clause(['cars', 'name'], 'asc'), - new Clause(['cars', 'engines'], 'desc'), - new Clause(['name'], 'asc'), - ], + new Property('cars', 'name'), + 'asc', ], MorphOne::class => [ [ @@ -316,8 +254,7 @@ static function (): EloquentBuilder { "images"."id" asc limit 1 - ) asc, - "name" asc + ) asc SQL , 'bindings' => [ @@ -327,10 +264,8 @@ static function (): EloquentBuilder { static function (): EloquentBuilder { return User::query(); }, - [ - new Clause(['avatar', 'id'], 'asc'), - new Clause(['name'], 'asc'), - ], + new Property('avatar', 'id'), + 'asc', ], HasOneThrough::class => [ [ @@ -340,20 +275,6 @@ static function (): EloquentBuilder { from "users" order by - ( - select - "roles"."name" - from - "roles" - inner join "user_roles" on "user_roles"."secondLocalKey" = "roles"."secondKey" - where - "users"."localKey" = "user_roles"."firstKey" - and "deleted_at" is null - order by - "roles"."name" asc - limit - 1 - ) asc, ( select "sort_by_user"."name" @@ -376,8 +297,7 @@ static function (): EloquentBuilder { "sort_by_user"."name" desc limit 1 - ) desc, - "name" desc + ) desc SQL , 'bindings' => [ @@ -387,11 +307,8 @@ static function (): EloquentBuilder { static function (): EloquentBuilder { return User::query(); }, - [ - new Clause(['role', 'name'], 'asc'), - new Clause(['role', 'user', 'name'], 'desc'), - new Clause(['name'], 'desc'), - ], + new Property('role', 'user', 'name'), + 'desc', ], BelongsToMany::class => [ [ @@ -401,20 +318,6 @@ static function (): EloquentBuilder { from "users" order by - ( - select - "roles"."name" - from - "roles" - inner join "user_roles" on "roles"."relatedKey" = "user_roles"."relatedPivotKey" - where - "users"."parentKey" = "user_roles"."foreignPivotKey" - and "deleted_at" is null - order by - "roles"."name" asc - limit - 1 - ) asc, ( select "sort_by_users"."name" @@ -438,8 +341,7 @@ static function (): EloquentBuilder { "sort_by_users"."name" desc limit 1 - ) desc, - "name" desc + ) desc SQL , 'bindings' => [ @@ -449,11 +351,8 @@ static function (): EloquentBuilder { static function (): EloquentBuilder { return User::query(); }, - [ - new Clause(['roles', 'name'], 'asc'), - new Clause(['roles', 'users', 'name'], 'desc'), - new Clause(['name'], 'desc'), - ], + new Property('roles', 'users', 'name'), + 'desc', ], MorphToMany::class => [ [ @@ -463,20 +362,6 @@ static function (): EloquentBuilder { from "users" order by - ( - select - "tags"."id" - from - "tags" - inner join "taggables" on "tags"."relatedKey" = "taggables"."relatedPivotKey" - where - "users"."parentKey" = "taggables"."foreignPivotKey" - and "taggables"."taggable_type" = ? - order by - "tags"."id" asc - limit - 1 - ) asc, ( select "sort_by_users"."name" @@ -498,23 +383,18 @@ static function (): EloquentBuilder { "sort_by_users"."name" asc limit 1 - ) asc, - "name" desc + ) asc SQL , 'bindings' => [ User::class, - User::class, ], ], static function (): EloquentBuilder { return User::query(); }, - [ - new Clause(['tags', 'id'], 'asc'), - new Clause(['tags', 'users', 'name'], 'asc'), - new Clause(['name'], 'desc'), - ], + new Property('tags', 'users', 'name'), + 'asc', ], HasManyThrough::class => [ [ @@ -537,8 +417,7 @@ static function (): EloquentBuilder { "users"."name" asc limit 1 - ) asc, - "id" desc + ) asc SQL , 'bindings' => [ @@ -548,10 +427,8 @@ static function (): EloquentBuilder { static function (): EloquentBuilder { return CarEngine::query(); }, - [ - new Clause(['users', 'name'], 'asc'), - new Clause(['id'], 'desc'), - ], + new Property('users', 'name'), + 'asc', ], MorphMany::class => [ [ @@ -574,8 +451,7 @@ static function (): EloquentBuilder { "images"."id" asc limit 1 - ) asc, - "name" desc + ) asc SQL , 'bindings' => [ @@ -585,10 +461,8 @@ static function (): EloquentBuilder { static function (): EloquentBuilder { return User::query(); }, - [ - new Clause(['images', 'id'], 'asc'), - new Clause(['name'], 'desc'), - ], + new Property('images', 'id'), + 'asc', ], ])), ]))->getData(); diff --git a/packages/graphql/src/SortBy/Builders/Query/Builder.php b/packages/graphql/src/SortBy/Builders/Query/Builder.php index b6eb7d5ec..18a34b891 100644 --- a/packages/graphql/src/SortBy/Builders/Query/Builder.php +++ b/packages/graphql/src/SortBy/Builders/Query/Builder.php @@ -3,7 +3,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SortBy\Builders\Query; use Illuminate\Database\Query\Builder as QueryBuilder; -use LastDragon_ru\LaraASP\GraphQL\SortBy\Builders\Clause; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use function implode; @@ -12,22 +12,16 @@ public function __construct() { // empty } - /** - * @param array $clauses - */ - public function handle(QueryBuilder $builder, array $clauses): QueryBuilder { - foreach ($clauses as $clause) { - // Column - $path = $clause->getPath(); - $column = implode('.', $path); - $direction = $clause->getDirection(); + public function handle(QueryBuilder $builder, Property $property, string $direction): QueryBuilder { + // Column + $path = $property->getPath(); + $column = implode('.', $path); - // Order - if ($direction) { - $builder = $builder->orderBy($column, $direction); - } else { - $builder = $builder->orderBy($column); - } + // Order + if ($direction) { + $builder = $builder->orderBy($column, $direction); + } else { + $builder = $builder->orderBy($column); } return $builder; diff --git a/packages/graphql/src/SortBy/Builders/Query/BuilderTest.php b/packages/graphql/src/SortBy/Builders/Query/BuilderTest.php index 372728cc8..4ae694cd8 100644 --- a/packages/graphql/src/SortBy/Builders/Query/BuilderTest.php +++ b/packages/graphql/src/SortBy/Builders/Query/BuilderTest.php @@ -4,7 +4,7 @@ use Closure; use Exception; -use LastDragon_ru\LaraASP\GraphQL\SortBy\Builders\Clause; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\QueryBuilderDataProvider; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; use LastDragon_ru\LaraASP\Testing\Providers\ArrayDataProvider; @@ -28,15 +28,19 @@ class BuilderTest extends TestCase { * * @param array{query: string, bindings: array}|Exception $expected * @param BuilderFactory $builder - * @param array $clauses */ - public function testHandle(array|Exception $expected, Closure $builder, array $clauses): void { + public function testHandle( + array|Exception $expected, + Closure $builder, + Property $property, + string $direction, + ): void { if ($expected instanceof Exception) { self::expectExceptionObject($expected); } $builder = $builder($this); - $builder = $this->app->make(Builder::class)->handle($builder, $clauses); + $builder = $this->app->make(Builder::class)->handle($builder, $property, $direction); if (is_array($expected)) { self::assertDatabaseQueryEquals($expected, $builder); @@ -55,30 +59,21 @@ public function dataProviderHandle(): array { return (new CompositeDataProvider( new QueryBuilderDataProvider(), new ArrayDataProvider([ - 'empty' => [ - [ - 'query' => 'select * from "tmp"', - 'bindings' => [], - ], - [], - ], 'simple condition' => [ [ 'query' => 'select * from "tmp" order by "a" asc', 'bindings' => [], ], - [ - new Clause(['a'], 'asc'), - ], + new Property('a'), + 'asc', ], 'nested not supported' => [ [ 'query' => 'select * from "tmp" order by "test"."name" asc', 'bindings' => [], ], - [ - new Clause(['test', 'name'], 'asc'), - ], + new Property('test', 'name'), + 'asc', ], ]), ))->getData(); diff --git a/packages/graphql/src/SortBy/Builders/Scout/Builder.php b/packages/graphql/src/SortBy/Builders/Scout/Builder.php index 65fb9c06f..8e3a1ff01 100644 --- a/packages/graphql/src/SortBy/Builders/Scout/Builder.php +++ b/packages/graphql/src/SortBy/Builders/Scout/Builder.php @@ -3,7 +3,7 @@ namespace LastDragon_ru\LaraASP\GraphQL\SortBy\Builders\Scout; use Laravel\Scout\Builder as ScoutBuilder; -use LastDragon_ru\LaraASP\GraphQL\SortBy\Builders\Clause; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use function implode; @@ -14,24 +14,18 @@ public function __construct( // empty } - /** - * @param array $clauses - */ - public function handle(ScoutBuilder $builder, array $clauses): ScoutBuilder { - foreach ($clauses as $clause) { - // Column - $path = $clause->getPath(); - $direction = $clause->getDirection(); - $column = $this->columnResolver - ? $this->columnResolver->getColumn($builder->model, $path) - : implode('.', $path); + public function handle(ScoutBuilder $builder, Property $property, string $direction): ScoutBuilder { + // Column + $path = $property->getPath(); + $column = $this->columnResolver + ? $this->columnResolver->getColumn($builder->model, $path) + : implode('.', $path); - // Order - if ($direction) { - $builder = $builder->orderBy($column, $direction); - } else { - $builder = $builder->orderBy($column); - } + // Order + if ($direction) { + $builder = $builder->orderBy($column, $direction); + } else { + $builder = $builder->orderBy($column); } return $builder; diff --git a/packages/graphql/src/SortBy/Builders/Scout/BuilderTest.php b/packages/graphql/src/SortBy/Builders/Scout/BuilderTest.php index 5be533a8b..9dc6538ee 100644 --- a/packages/graphql/src/SortBy/Builders/Scout/BuilderTest.php +++ b/packages/graphql/src/SortBy/Builders/Scout/BuilderTest.php @@ -6,7 +6,7 @@ use Exception; use Illuminate\Database\Eloquent\Model; use Laravel\Scout\Builder as ScoutBuilder; -use LastDragon_ru\LaraASP\GraphQL\SortBy\Builders\Clause; +use LastDragon_ru\LaraASP\GraphQL\Builder\Property; use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase; use function implode; @@ -27,10 +27,14 @@ class BuilderTest extends TestCase { * @dataProvider dataProviderHandle * * @param array|Exception $expected - * @param array $clauses * @param Closure():ColumnResolver $resolver */ - public function testHandle(array|Exception $expected, array $clauses, Closure $resolver = null): void { + public function testHandle( + array|Exception $expected, + Property $property, + string $direction, + Closure $resolver = null, + ): void { if ($expected instanceof Exception) { self::expectExceptionObject($expected); } @@ -45,7 +49,7 @@ public function testHandle(array|Exception $expected, array $clauses, Closure $r // empty }, ]); - $builder = $this->app->make(Builder::class)->handle($builder, $clauses); + $builder = $this->app->make(Builder::class)->handle($builder, $property, $direction); $actual = json_decode((string) json_encode($builder), true); $default = [ 'model' => [], @@ -72,39 +76,17 @@ public function testHandle(array|Exception $expected, array $clauses, Closure $r */ public function dataProviderHandle(): array { return [ - 'empty' => [ - [ - // empty - ], - [], - ], 'clause' => [ [ 'orders' => [ - [ - 'column' => 'a', - 'direction' => 'asc', - ], - [ - 'column' => 'b', - 'direction' => 'desc', - ], [ 'column' => 'c.d.e', 'direction' => 'desc', ], - [ - 'column' => 'null', - 'direction' => 'asc', - ], ], ], - [ - new Clause(['a'], 'asc'), - new Clause(['b'], 'desc'), - new Clause(['c', 'd', 'e'], 'desc'), - new Clause(['null'], null), - ], + new Property('c', 'd', 'e'), + 'desc', ], 'clause with resolver' => [ [ @@ -115,9 +97,8 @@ public function dataProviderHandle(): array { ], ], ], - [ - new Clause(['a', 'b'], 'asc'), - ], + new Property('a', 'b'), + 'asc', static function (): ColumnResolver { return new class() implements ColumnResolver { /** diff --git a/packages/graphql/src/SortBy/Operators/OrderBy.php b/packages/graphql/src/SortBy/Operators/OrderBy.php index 5d4b2b46d..9e7fe12d4 100644 --- a/packages/graphql/src/SortBy/Operators/OrderBy.php +++ b/packages/graphql/src/SortBy/Operators/OrderBy.php @@ -10,7 +10,6 @@ use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeProvider; use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\OperatorUnsupportedBuilder; use LastDragon_ru\LaraASP\GraphQL\Builder\Property; -use LastDragon_ru\LaraASP\GraphQL\SortBy\Builders\Clause; use LastDragon_ru\LaraASP\GraphQL\SortBy\Builders\Eloquent\Builder as EloquentHandler; use LastDragon_ru\LaraASP\GraphQL\SortBy\Builders\Query\Builder as QueryHandler; use LastDragon_ru\LaraASP\GraphQL\SortBy\Builders\Scout\Builder as ScoutHandler; @@ -40,14 +39,13 @@ public function getFieldDescription(): string { public function call(Handler $handler, object $builder, Property $property, Argument $argument): object { $direction = Cast::toString($argument->value); - $clauses = [new Clause($property->getPath(), $direction)]; if ($builder instanceof EloquentBuilder) { - $this->eloquent->handle($builder, $clauses); + $this->eloquent->handle($builder, $property, $direction); } elseif ($builder instanceof QueryBuilder) { - $this->query->handle($builder, $clauses); + $this->query->handle($builder, $property, $direction); } elseif ($builder instanceof ScoutBuilder) { - $this->scout->handle($builder, $clauses); + $this->scout->handle($builder, $property, $direction); } else { throw new OperatorUnsupportedBuilder($this, $builder); } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index ce7e00c4d..4045dab64 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -100,11 +100,6 @@ parameters: count: 1 path: packages/graphql/src/SortBy/Directives/DirectiveTest.php - - - message: "#^Parameter \\#1 \\$path of class LastDragon_ru\\\\LaraASP\\\\GraphQL\\\\SortBy\\\\Builders\\\\Clause constructor expects non\\-empty\\-array\\, array\\ given\\.$#" - count: 1 - path: packages/graphql/src/SortBy/Operators/OrderBy.php - - message: "#^Method LastDragon_ru\\\\LaraASP\\\\GraphQL\\\\Testing\\\\Package\\\\Models\\\\Image\\:\\:imageable\\(\\) should return Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\MorphTo\\ but returns Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\MorphTo\\\\.$#" count: 1 From 739db37e889eb3b39bb31cb0f8554ad3a6b9c5c0 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 2 Jul 2022 09:31:44 +0400 Subject: [PATCH 09/13] `AstManipulator` moved to `Utils`. --- packages/graphql/src/Builder/Manipulator.php | 3 ++- .../graphql/src/{ => Utils}/AstManipulator.php | 2 +- phpstan-baseline.neon | 14 +++++++------- 3 files changed, 10 insertions(+), 9 deletions(-) rename packages/graphql/src/{ => Utils}/AstManipulator.php (99%) diff --git a/packages/graphql/src/Builder/Manipulator.php b/packages/graphql/src/Builder/Manipulator.php index 76f8b5601..043990f0a 100644 --- a/packages/graphql/src/Builder/Manipulator.php +++ b/packages/graphql/src/Builder/Manipulator.php @@ -11,14 +11,15 @@ use GraphQL\Type\Definition\InputObjectField; use GraphQL\Type\Definition\Type; use Illuminate\Contracts\Container\Container; -use LastDragon_ru\LaraASP\GraphQL\AstManipulator; use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Operator; use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeProvider; use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\TypeDefinitionImpossibleToCreateType; use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\TypeDefinitionInvalidTypeName; +use LastDragon_ru\LaraASP\GraphQL\Utils\AstManipulator; use Nuwave\Lighthouse\Schema\AST\DocumentAST; use Nuwave\Lighthouse\Schema\DirectiveLocator; use Nuwave\Lighthouse\Schema\TypeRegistry; + use function array_map; use function implode; diff --git a/packages/graphql/src/AstManipulator.php b/packages/graphql/src/Utils/AstManipulator.php similarity index 99% rename from packages/graphql/src/AstManipulator.php rename to packages/graphql/src/Utils/AstManipulator.php index ec6bd5797..641c87b82 100644 --- a/packages/graphql/src/AstManipulator.php +++ b/packages/graphql/src/Utils/AstManipulator.php @@ -1,6 +1,6 @@ Date: Sat, 2 Jul 2022 09:54:07 +0400 Subject: [PATCH 10/13] `@sortByOperatorOrderBy` renamed to `@sortByOperatorProperty`. --- ...ctive.php => SortByOperatorPropertyDirective.php} | 4 ++-- .../Directives/DirectiveTest~full-expected.graphql | 12 ++++++------ .../DirectiveTest~registry-expected.graphql | 12 ++++++------ packages/graphql/src/SortBy/Manipulator.php | 5 ++--- .../Operators/{OrderBy.php => PropertyOperator.php} | 4 ++-- .../{OrderByTest.php => PropertyOperatorTest.php} | 12 ++++++------ 6 files changed, 24 insertions(+), 25 deletions(-) rename packages/graphql/src/SortBy/Definitions/{SortByOperatorOrderByDirective.php => SortByOperatorPropertyDirective.php} (69%) rename packages/graphql/src/SortBy/Operators/{OrderBy.php => PropertyOperator.php} (96%) rename packages/graphql/src/SortBy/Operators/{OrderByTest.php => PropertyOperatorTest.php} (94%) diff --git a/packages/graphql/src/SortBy/Definitions/SortByOperatorOrderByDirective.php b/packages/graphql/src/SortBy/Definitions/SortByOperatorPropertyDirective.php similarity index 69% rename from packages/graphql/src/SortBy/Definitions/SortByOperatorOrderByDirective.php rename to packages/graphql/src/SortBy/Definitions/SortByOperatorPropertyDirective.php index d962d631b..74fe79e12 100644 --- a/packages/graphql/src/SortBy/Definitions/SortByOperatorOrderByDirective.php +++ b/packages/graphql/src/SortBy/Definitions/SortByOperatorPropertyDirective.php @@ -2,9 +2,9 @@ namespace LastDragon_ru\LaraASP\GraphQL\SortBy\Definitions; -use LastDragon_ru\LaraASP\GraphQL\SortBy\Operators\OrderBy; +use LastDragon_ru\LaraASP\GraphQL\SortBy\Operators\PropertyOperator; -class SortByOperatorOrderByDirective extends OrderBy { +class SortByOperatorPropertyDirective extends PropertyOperator { // Lighthouse loads all classes from directive namespace this leads to // 'Class "Orchestra\Testbench\TestCase" not found' error for our *Test // classes. This class required to avoid this error. diff --git a/packages/graphql/src/SortBy/Directives/DirectiveTest~full-expected.graphql b/packages/graphql/src/SortBy/Directives/DirectiveTest~full-expected.graphql index 5ed8c3642..d187fc716 100644 --- a/packages/graphql/src/SortBy/Directives/DirectiveTest~full-expected.graphql +++ b/packages/graphql/src/SortBy/Directives/DirectiveTest~full-expected.graphql @@ -20,7 +20,7 @@ input SortByClauseNested { Property clause. """ value: SortByTypeDirection - @sortByOperatorOrderBy + @sortByOperatorProperty } """ @@ -31,25 +31,25 @@ input SortByClauseProperties { Property clause. """ enum: SortByTypeDirection - @sortByOperatorOrderBy + @sortByOperatorProperty """ Property clause. """ enumNotNull: SortByTypeDirection - @sortByOperatorOrderBy + @sortByOperatorProperty """ Property clause. """ idScalar: SortByTypeDirection - @sortByOperatorOrderBy + @sortByOperatorProperty """ Property clause. """ idScalarNotNull: SortByTypeDirection - @sortByOperatorOrderBy + @sortByOperatorProperty """ Property clause. @@ -79,7 +79,7 @@ directive @sortBy on | ARGUMENT_DEFINITION -directive @sortByOperatorOrderBy +directive @sortByOperatorProperty on | INPUT_FIELD_DEFINITION diff --git a/packages/graphql/src/SortBy/Directives/DirectiveTest~registry-expected.graphql b/packages/graphql/src/SortBy/Directives/DirectiveTest~registry-expected.graphql index d0ecf3d7e..11e5fa263 100644 --- a/packages/graphql/src/SortBy/Directives/DirectiveTest~registry-expected.graphql +++ b/packages/graphql/src/SortBy/Directives/DirectiveTest~registry-expected.graphql @@ -14,13 +14,13 @@ input SortByClauseA { Property clause. """ flag: SortByTypeDirection - @sortByOperatorOrderBy + @sortByOperatorProperty """ Property clause. """ name: SortByTypeDirection - @sortByOperatorOrderBy + @sortByOperatorProperty } """ @@ -37,7 +37,7 @@ input SortByClauseB { Property clause. """ name: SortByTypeDirection - @sortByOperatorOrderBy + @sortByOperatorProperty } """ @@ -48,13 +48,13 @@ input SortByClauseC { Property clause. """ flag: SortByTypeDirection - @sortByOperatorOrderBy + @sortByOperatorProperty """ Property clause. """ name: SortByTypeDirection - @sortByOperatorOrderBy + @sortByOperatorProperty } """ @@ -105,7 +105,7 @@ directive @sortBy on | ARGUMENT_DEFINITION -directive @sortByOperatorOrderBy +directive @sortByOperatorProperty on | INPUT_FIELD_DEFINITION diff --git a/packages/graphql/src/SortBy/Manipulator.php b/packages/graphql/src/SortBy/Manipulator.php index 7bc7a8bd1..17640e41d 100644 --- a/packages/graphql/src/SortBy/Manipulator.php +++ b/packages/graphql/src/SortBy/Manipulator.php @@ -19,13 +19,12 @@ use LastDragon_ru\LaraASP\GraphQL\SortBy\Contracts\Unsortable; use LastDragon_ru\LaraASP\GraphQL\SortBy\Directives\Directive; use LastDragon_ru\LaraASP\GraphQL\SortBy\Exceptions\FailedToCreateSortClause; -use LastDragon_ru\LaraASP\GraphQL\SortBy\Operators\OrderBy; use LastDragon_ru\LaraASP\GraphQL\SortBy\Operators\Property; +use LastDragon_ru\LaraASP\GraphQL\SortBy\Operators\PropertyOperator; use LastDragon_ru\LaraASP\GraphQL\SortBy\Types\Direction; use Nuwave\Lighthouse\Pagination\PaginateDirective; use Nuwave\Lighthouse\Pagination\PaginationType; use Nuwave\Lighthouse\Support\Contracts\FieldResolver; - use function count; use function mb_strlen; use function mb_substr; @@ -102,7 +101,7 @@ protected function getInputType( // Add sortable fields $direction = $this->getType(Direction::class); - $operator = $this->getContainer()->make(OrderBy::class); + $operator = $this->getContainer()->make(PropertyOperator::class); $property = $this->getContainer()->make(Property::class); $fields = $node instanceof InputObjectType || $node instanceof ObjectType ? $node->getFields() diff --git a/packages/graphql/src/SortBy/Operators/OrderBy.php b/packages/graphql/src/SortBy/Operators/PropertyOperator.php similarity index 96% rename from packages/graphql/src/SortBy/Operators/OrderBy.php rename to packages/graphql/src/SortBy/Operators/PropertyOperator.php index 9e7fe12d4..92ddd8016 100644 --- a/packages/graphql/src/SortBy/Operators/OrderBy.php +++ b/packages/graphql/src/SortBy/Operators/PropertyOperator.php @@ -16,7 +16,7 @@ use LastDragon_ru\LaraASP\GraphQL\SortBy\Types\Direction; use Nuwave\Lighthouse\Execution\Arguments\Argument; -class OrderBy extends BaseOperator { +class PropertyOperator extends BaseOperator { public function __construct( private EloquentHandler $eloquent, private QueryHandler $query, @@ -26,7 +26,7 @@ public function __construct( } public static function getName(): string { - return 'orderBy'; + return 'property'; } public function getFieldType(TypeProvider $provider, string $type): ?string { diff --git a/packages/graphql/src/SortBy/Operators/OrderByTest.php b/packages/graphql/src/SortBy/Operators/PropertyOperatorTest.php similarity index 94% rename from packages/graphql/src/SortBy/Operators/OrderByTest.php rename to packages/graphql/src/SortBy/Operators/PropertyOperatorTest.php index 03d704749..213f9fabb 100644 --- a/packages/graphql/src/SortBy/Operators/OrderByTest.php +++ b/packages/graphql/src/SortBy/Operators/PropertyOperatorTest.php @@ -21,11 +21,11 @@ /** * @internal - * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SortBy\Operators\OrderBy + * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\SortBy\Operators\PropertyOperator * * @phpstan-import-type BuilderFactory from BuilderDataProvider */ -class OrderByTest extends TestCase { +class PropertyOperatorTest extends TestCase { // // ========================================================================= /** @@ -43,7 +43,7 @@ public function testCall( Property $property, Closure $argumentFactory, ): void { - $operator = $this->app->make(OrderBy::class); + $operator = $this->app->make(PropertyOperator::class); $argument = $argumentFactory($this); $directive = $this->app->make(Directive::class); $builder = $builderFactory($this); @@ -65,7 +65,7 @@ public function testCallEloquentBuilder(): void { $directive = $this->app->make(Directive::class); $property = new Property(); - $operator = $this->app->make(OrderBy::class); + $operator = $this->app->make(PropertyOperator::class); $argument = $this->getGraphQLArgument( 'Test', 'asc', @@ -89,7 +89,7 @@ public function testCallQueryBuilder(): void { $directive = $this->app->make(Directive::class); $property = new Property(); - $operator = $this->app->make(OrderBy::class); + $operator = $this->app->make(PropertyOperator::class); $argument = $this->getGraphQLArgument( 'Test', 'asc', @@ -112,7 +112,7 @@ public function testCallScoutBuilder(): void { $directive = $this->app->make(Directive::class); $property = new Property(); - $operator = $this->app->make(OrderBy::class); + $operator = $this->app->make(PropertyOperator::class); $argument = $this->getGraphQLArgument( 'Test', 'asc', From e4c0e7808939828aa4ecc55a8c88b7e0177223f4 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 2 Jul 2022 09:59:58 +0400 Subject: [PATCH 11/13] `@sortByUnsortable` renamed to `@sortByIgnored`. --- packages/graphql/README.md | 4 ++-- .../src/SortBy/Contracts/{Unsortable.php => Ignored.php} | 2 +- ...ByUnsortableDirective.php => SortByIgnoredDirective.php} | 4 ++-- .../src/SortBy/Directives/DirectiveTest~full.graphql | 2 +- .../src/SortBy/Directives/{Unsortable.php => Ignored.php} | 6 +++--- packages/graphql/src/SortBy/Manipulator.php | 6 +++--- 6 files changed, 12 insertions(+), 12 deletions(-) rename packages/graphql/src/SortBy/Contracts/{Unsortable.php => Ignored.php} (84%) rename packages/graphql/src/SortBy/Definitions/{SortByUnsortableDirective.php => SortByIgnoredDirective.php} (72%) rename packages/graphql/src/SortBy/Directives/{Unsortable.php => Ignored.php} (58%) diff --git a/packages/graphql/README.md b/packages/graphql/README.md index 2970f3b76..5e91d3974 100644 --- a/packages/graphql/README.md +++ b/packages/graphql/README.md @@ -358,8 +358,8 @@ As you can see in the example above you can use the special placeholder `_` inst - with list/array type - with `@field` directive -- with `@sortByUnsortable` directive -- with any directive that implements [`Unsortable`](./src/SortBy/Contracts/Unsortable.php) +- with `@sortByIgnored` directive +- with any directive that implements [`Ignored`](./src/SortBy/Contracts/Ignored.php) # Relations diff --git a/packages/graphql/src/SortBy/Contracts/Unsortable.php b/packages/graphql/src/SortBy/Contracts/Ignored.php similarity index 84% rename from packages/graphql/src/SortBy/Contracts/Unsortable.php rename to packages/graphql/src/SortBy/Contracts/Ignored.php index 6d8eadcda..7651bf17f 100644 --- a/packages/graphql/src/SortBy/Contracts/Unsortable.php +++ b/packages/graphql/src/SortBy/Contracts/Ignored.php @@ -7,6 +7,6 @@ /** * Marks that field should be excluded from sort. */ -interface Unsortable extends Directive { +interface Ignored extends Directive { // empty } diff --git a/packages/graphql/src/SortBy/Definitions/SortByUnsortableDirective.php b/packages/graphql/src/SortBy/Definitions/SortByIgnoredDirective.php similarity index 72% rename from packages/graphql/src/SortBy/Definitions/SortByUnsortableDirective.php rename to packages/graphql/src/SortBy/Definitions/SortByIgnoredDirective.php index 2e0ffdece..0423ada27 100644 --- a/packages/graphql/src/SortBy/Definitions/SortByUnsortableDirective.php +++ b/packages/graphql/src/SortBy/Definitions/SortByIgnoredDirective.php @@ -2,9 +2,9 @@ namespace LastDragon_ru\LaraASP\GraphQL\SortBy\Definitions; -use LastDragon_ru\LaraASP\GraphQL\SortBy\Directives\Unsortable; +use LastDragon_ru\LaraASP\GraphQL\SortBy\Directives\Ignored; -class SortByUnsortableDirective extends Unsortable { +class SortByIgnoredDirective extends Ignored { // Lighthouse loads all classes from directive namespace this leads to // 'Class "Orchestra\Testbench\TestCase" not found' error for our *Test // classes. This class required to avoid this error. diff --git a/packages/graphql/src/SortBy/Directives/DirectiveTest~full.graphql b/packages/graphql/src/SortBy/Directives/DirectiveTest~full.graphql index dbc317c43..204851b84 100644 --- a/packages/graphql/src/SortBy/Directives/DirectiveTest~full.graphql +++ b/packages/graphql/src/SortBy/Directives/DirectiveTest~full.graphql @@ -9,7 +9,7 @@ input Properties { nestedNotNull: Nested! enum: Value enumNotNull: Value! - unsortable: ID! @sortByUnsortable + ignored: ID! @sortByIgnored } input Nested { diff --git a/packages/graphql/src/SortBy/Directives/Unsortable.php b/packages/graphql/src/SortBy/Directives/Ignored.php similarity index 58% rename from packages/graphql/src/SortBy/Directives/Unsortable.php rename to packages/graphql/src/SortBy/Directives/Ignored.php index 518f01843..e194590ce 100644 --- a/packages/graphql/src/SortBy/Directives/Unsortable.php +++ b/packages/graphql/src/SortBy/Directives/Ignored.php @@ -2,15 +2,15 @@ namespace LastDragon_ru\LaraASP\GraphQL\SortBy\Directives; -use LastDragon_ru\LaraASP\GraphQL\SortBy\Contracts\Unsortable as UnsortableContract; +use LastDragon_ru\LaraASP\GraphQL\SortBy\Contracts\Ignored as IgnoredContract; -class Unsortable implements UnsortableContract { +class Ignored implements IgnoredContract { public static function definition(): string { return /** @lang GraphQL */ <<<'GRAPHQL' """ Marks that field should be excluded from sort. """ - directive @sortByUnsortable on FIELD_DEFINITION | INPUT_FIELD_DEFINITION, + directive @sortByIgnored on FIELD_DEFINITION | INPUT_FIELD_DEFINITION, GRAPHQL; } } diff --git a/packages/graphql/src/SortBy/Manipulator.php b/packages/graphql/src/SortBy/Manipulator.php index 17640e41d..dfdb5f546 100644 --- a/packages/graphql/src/SortBy/Manipulator.php +++ b/packages/graphql/src/SortBy/Manipulator.php @@ -16,7 +16,7 @@ use GraphQL\Type\Definition\Type; use Illuminate\Support\Str; use LastDragon_ru\LaraASP\GraphQL\Builder\Manipulator as BuilderManipulator; -use LastDragon_ru\LaraASP\GraphQL\SortBy\Contracts\Unsortable; +use LastDragon_ru\LaraASP\GraphQL\SortBy\Contracts\Ignored; use LastDragon_ru\LaraASP\GraphQL\SortBy\Directives\Directive; use LastDragon_ru\LaraASP\GraphQL\SortBy\Exceptions\FailedToCreateSortClause; use LastDragon_ru\LaraASP\GraphQL\SortBy\Operators\Property; @@ -121,8 +121,8 @@ protected function getInputType( continue; } - // Unsortable? - if ($this->getNodeDirective($field, Unsortable::class)) { + // Ignored? + if ($this->getNodeDirective($field, Ignored::class)) { continue; } From e715bfbba439bb299b3d4753ea3bb62c87589069 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 2 Jul 2022 10:01:24 +0400 Subject: [PATCH 12/13] `Directive::TypeDirection` removed. --- packages/graphql/src/SortBy/Directives/Directive.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/graphql/src/SortBy/Directives/Directive.php b/packages/graphql/src/SortBy/Directives/Directive.php index de3564409..3ecd74602 100644 --- a/packages/graphql/src/SortBy/Directives/Directive.php +++ b/packages/graphql/src/SortBy/Directives/Directive.php @@ -17,8 +17,7 @@ use Nuwave\Lighthouse\Support\Contracts\ArgManipulator; class Directive extends HandlerDirective implements ArgManipulator, ArgBuilderDirective, ScoutBuilderDirective { - public const Name = 'SortBy'; - public const TypeDirection = 'SortByDirection'; + public const Name = 'SortBy'; public static function definition(): string { return /** @lang GraphQL */ <<<'GRAPHQL' From dd669b6b95da88370d168cf82c6e7d8aca819bda Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 2 Jul 2022 10:07:30 +0400 Subject: [PATCH 13/13] Documentation update. --- packages/graphql/README.md | 66 +---------- .../src/SortBy/Directives/DirectiveTest.php | 11 +- .../DirectiveTest~example-expected.graphql | 103 ++++++++++++++++++ .../Directives/DirectiveTest~example.graphql | 22 ++++ 4 files changed, 136 insertions(+), 66 deletions(-) create mode 100644 packages/graphql/src/SortBy/Directives/DirectiveTest~example-expected.graphql create mode 100644 packages/graphql/src/SortBy/Directives/DirectiveTest~example.graphql diff --git a/packages/graphql/README.md b/packages/graphql/README.md index 5e91d3974..2cd9949b4 100644 --- a/packages/graphql/README.md +++ b/packages/graphql/README.md @@ -234,7 +234,7 @@ The main feature - the ability to sort results by relation properties, at the mo - `HasOneThrough` (https://laravel.com/docs/eloquent-relationships#has-one-through) -How to use: +How to use (and [generated GraphQL schema](./src/SortBy/Directives/DirectiveTest~example-expected.graphql)): ```graphql type Query { @@ -273,70 +273,6 @@ query { } ``` -
-Generated GraphQL schema - -```graphql -type Comment { - text: String - user: User -} - -type Query { - """You can use normal input type""" - users(order: [SortByClauseUsersSort!]): ID! - - """or `_` to generate type automatically 😛""" - comments(order: [SortByClauseComment!]): [Comment!]! -} - -"""Sort clause for type Comment (only one property allowed at a time).""" -input SortByClauseComment { - """Property clause.""" - text: SortByDirection - - """Property clause.""" - user: SortByClauseUser -} - -"""Sort clause for type User (only one property allowed at a time).""" -input SortByClauseUser { - """Property clause.""" - id: SortByDirection - - """Property clause.""" - name: SortByDirection -} - -""" -Sort clause for input UsersSort (only one property allowed at a time). -""" -input SortByClauseUsersSort { - """Property clause.""" - id: SortByDirection - - """Property clause.""" - name: SortByDirection -} - -"""Sort direction.""" -enum SortByDirection { - asc - desc -} - -type User { - id: ID! - name: String! -} - -input UsersSort { - id: ID! - name: String! -} -``` -
- ## Scout diff --git a/packages/graphql/src/SortBy/Directives/DirectiveTest.php b/packages/graphql/src/SortBy/Directives/DirectiveTest.php index fa441d971..cb11c0aff 100644 --- a/packages/graphql/src/SortBy/Directives/DirectiveTest.php +++ b/packages/graphql/src/SortBy/Directives/DirectiveTest.php @@ -196,7 +196,7 @@ public function testHandleBuilder( */ public function dataProviderManipulateArgDefinition(): array { return [ - 'full' => [ + 'full' => [ static function (self $test): GraphQLExpectedSchema { return (new GraphQLExpectedSchema( $test->getTestData()->file('~full-expected.graphql'), @@ -214,6 +214,15 @@ static function (self $test): GraphQLExpectedSchema { '~full.graphql', null, ], + 'example' => [ + static function (self $test): GraphQLExpectedSchema { + return (new GraphQLExpectedSchema( + $test->getTestData()->file('~example-expected.graphql'), + )); + }, + '~example.graphql', + null, + ], ]; } diff --git a/packages/graphql/src/SortBy/Directives/DirectiveTest~example-expected.graphql b/packages/graphql/src/SortBy/Directives/DirectiveTest~example-expected.graphql new file mode 100644 index 000000000..11ce6bdd9 --- /dev/null +++ b/packages/graphql/src/SortBy/Directives/DirectiveTest~example-expected.graphql @@ -0,0 +1,103 @@ +""" +Sort direction. +""" +enum SortByTypeDirection { + asc + desc +} + +""" +Sort clause for type Comment (only one property allowed at a time). +""" +input SortByClauseComment { + """ + Property clause. + """ + text: SortByTypeDirection + @sortByOperatorProperty + + """ + Property clause. + """ + user: SortByClauseUser + @sortByProperty +} + +""" +Sort clause for type User (only one property allowed at a time). +""" +input SortByClauseUser { + """ + Property clause. + """ + id: SortByTypeDirection + @sortByOperatorProperty + + """ + Property clause. + """ + name: SortByTypeDirection + @sortByOperatorProperty +} + +""" +Sort clause for input UsersSort (only one property allowed at a time). +""" +input SortByClauseUsersSort { + """ + Property clause. + """ + id: SortByTypeDirection + @sortByOperatorProperty + + """ + Property clause. + """ + name: SortByTypeDirection + @sortByOperatorProperty +} + +type Comment { + text: String + user: User +} + +type Query { + """ + or `_` to generate type automatically 😛 + """ + comments( + order: [SortByClauseComment!] + @sortBy + ): [Comment!]! + @all + + """ + You can use normal input type + """ + users( + order: [SortByClauseUsersSort!] + @sortBy + ): ID! + @all +} + +type User { + id: ID! + name: String! +} + +""" +Convert Input into Sort Clause. +""" +directive @sortBy +on + | ARGUMENT_DEFINITION + +directive @sortByOperatorProperty +on + | INPUT_FIELD_DEFINITION + +directive @sortByProperty +on + | INPUT_FIELD_DEFINITION diff --git a/packages/graphql/src/SortBy/Directives/DirectiveTest~example.graphql b/packages/graphql/src/SortBy/Directives/DirectiveTest~example.graphql new file mode 100644 index 000000000..aa9d8fffb --- /dev/null +++ b/packages/graphql/src/SortBy/Directives/DirectiveTest~example.graphql @@ -0,0 +1,22 @@ +type Query { + "You can use normal input type" + users(order: UsersSort @sortBy): ID! @all + + "or `_` to generate type automatically 😛" + comments(order: _ @sortBy): [Comment!]! @all +} + +input UsersSort { + id: ID! + name: String! +} + +type Comment { + text: String + user: User +} + +type User { + id: ID! + name: String! +}