Skip to content

Commit

Permalink
refactor(graphql)!: Better names for (internal) directives (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
LastDragon-ru authored Feb 2, 2024
2 parents 53f01f7 + 70c2b9e commit abef051
Show file tree
Hide file tree
Showing 82 changed files with 1,323 additions and 836 deletions.
20 changes: 10 additions & 10 deletions packages/graphql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,12 @@ on
| INPUT_FIELD_DEFINITION
| SCALAR

directive @searchByOperatorField
on
| ENUM
| INPUT_FIELD_DEFINITION
| SCALAR

directive @searchByOperatorIn
on
| ENUM
Expand Down Expand Up @@ -336,12 +342,6 @@ on
| INPUT_FIELD_DEFINITION
| SCALAR

directive @searchByOperatorProperty
on
| ENUM
| INPUT_FIELD_DEFINITION
| SCALAR

directive @searchByOperatorStartsWith
on
| ENUM
Expand All @@ -365,16 +365,16 @@ input SearchByConditionUser {
@searchByOperatorAnyOf

"""
Property condition.
Field condition.
"""
id: SearchByScalarID
@searchByOperatorProperty
@searchByOperatorField

"""
Property condition.
Field condition.
"""
name: SearchByScalarString
@searchByOperatorProperty
@searchByOperatorField

"""
Not.
Expand Down
16 changes: 15 additions & 1 deletion packages/graphql/UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ Please also see [changelog](https://github.com/LastDragon-ru/lara-asp/releases)

* [ ] `@searchByOperatorRelation` => `@searchByOperatorRelationship` (and class too; generated types will be named as `SearchByRelationship*` instead of `SearchByComplex*`).

* [ ] `@searchByOperatorProperty` => `@searchByOperatorField` (and class too)

* [ ] `@searchByOperatorCondition` => `@searchByOperatorFieldObject`

* [ ] `LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators::Condition` => `LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators::Object`.

* [ ] `scalar SearchByCondition` => `scalar SearchByObject`.
Expand Down Expand Up @@ -82,6 +86,8 @@ Please also see [changelog](https://github.com/LastDragon-ru/lara-asp/releases)

* [ ] If you are using `LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Scout\FieldResolver`, use `LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\BuilderPropertyResolver` instead. 🤝

* [ ] `@sortByOperatorProperty` => `@sortByOperatorFieldObject` (and class too)

## API

This section is actual only if you are extending the package. Please review and update (listed the most significant changes only):
Expand All @@ -108,10 +114,18 @@ This section is actual only if you are extending the package. Please review and

* [ ] `LastDragon_ru\LaraASP\GraphQL\Builder\Directives\HandlerDirective`

* [ ] `LastDragon_ru\LaraASP\GraphQL\Builder\Directives\PropertyDirective`
* [ ] Removed `LastDragon_ru\LaraASP\GraphQL\Builder\Directives\PropertyDirective`

* [ ] `LastDragon_ru\LaraASP\GraphQL\Builder\Sources\*`

* [ ] `LastDragon_ru\LaraASP\GraphQL\Builder\Traits\PropertyOperator` => `LastDragon_ru\LaraASP\GraphQL\Builder\Traits\HandlerOperator`

* [ ] `LastDragon_ru\LaraASP\GraphQL\Builder\Types\InputObject`

* [ ] `LastDragon_ru\LaraASP\GraphQL\SortBy\Builders\*` => `LastDragon_ru\LaraASP\GraphQL\SortBy\Sorters\*`

* [ ] `LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\BaseOperator` => `LastDragon_ru\LaraASP\GraphQL\SortBy\Operators\Operator`

* [ ] `LastDragon_ru\LaraASP\GraphQL\SortBy\Operators\BaseOperator` => `LastDragon_ru\LaraASP\GraphQL\SortBy\Operators\Operator`

* [ ] `LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Traits\ScoutSupport` => `LastDragon_ru\LaraASP\GraphQL\Builder\Traits\WithScoutSupport`
16 changes: 12 additions & 4 deletions packages/graphql/src/Builder/Directives/HandlerDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,23 @@ protected function getArgumentTypeDefinitionNode(
Manipulator $manipulator,
DocumentAST $document,
ObjectFieldArgumentSource|InterfaceFieldArgumentSource $argument,
string $operator,
ContextContract $context,
string $operator,
): ListTypeNode|NamedTypeNode|NonNullTypeNode|null {
// Supported?
$operator = $manipulator->getOperator(static::getScope(), $operator);
$builder = $context->get(HandlerContextBuilderInfo::class)?->value->getBuilder();

if (!$builder || !$operator->isAvailable($builder, $context)) {
return null;
}

// Type
$definition = $context->get(HandlerContextImplicit::class)?->value
? $manipulator->getTypeDefinition($manipulator->getOriginType($argument->getField()))
: $argument->getTypeDefinition();
$operator = $manipulator->getOperator(static::getScope(), $operator);
$node = $manipulator->getTypeSource($definition);
$type = $operator->getFieldType($manipulator, $node, $context);
$source = $manipulator->getTypeSource($definition);
$type = $operator->getFieldType($manipulator, $source, $context);
$type = Parser::typeReference($type);

return $type;
Expand Down
42 changes: 0 additions & 42 deletions packages/graphql/src/Builder/Directives/PropertyDirective.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,41 @@

use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Context;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Operator;
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;
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 Override;

use function count;

trait PropertyOperator {
/**
* @phpstan-require-implements Operator
*/
trait HandlerOperator {
#[Override]
public function call(
Handler $handler,
object $builder,
Property $property,
Argument $argument,
Context $context,
): object {
return $this->handle($handler, $builder, $property, $argument, $context);
}

/**
* @template TBuilder of object
*
* @param TBuilder $builder
*
* @return TBuilder
*/
protected function handle(
private function handle(
Handler $handler,
object $builder,
Property $property,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Traits;
namespace LastDragon_ru\LaraASP\GraphQL\Builder\Traits;

use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
use Laravel\Scout\Builder as ScoutBuilder;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Context;
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Definitions\SearchByOperatorPropertyDirective;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Operator;
use Override;

use function is_a;

/**
* @mixin SearchByOperatorPropertyDirective
* @mixin Operator
*/
trait ScoutSupport {
trait WithScoutSupport {
#[Override]
public function isAvailable(string $builder, Context $context): bool {
return parent::isAvailable($builder, $context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace LastDragon_ru\LaraASP\GraphQL\SearchBy\Definitions;

use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Property;
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Field;

class SearchByOperatorPropertyDirective extends Property {
class SearchByOperatorFieldDirective extends Field {
// 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace LastDragon_ru\LaraASP\GraphQL\SearchBy\Definitions;

use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Condition;
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\FieldObject;

class SearchByOperatorConditionDirective extends Condition {
class SearchByOperatorFieldObjectDirective extends FieldObject {
// 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.
Expand Down
10 changes: 2 additions & 8 deletions packages/graphql/src/SearchBy/Directives/Directive.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
use LastDragon_ru\LaraASP\GraphQL\Builder\Sources\InterfaceFieldArgumentSource;
use LastDragon_ru\LaraASP\GraphQL\Builder\Sources\ObjectFieldArgumentSource;
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Scope;
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Definitions\SearchByOperatorConditionDirective;
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\FailedToCreateSearchCondition;
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Root;
use Nuwave\Lighthouse\Execution\Arguments\ArgumentSet;
use Nuwave\Lighthouse\Schema\AST\DocumentAST;
use Nuwave\Lighthouse\Schema\DirectiveLocator;
Expand Down Expand Up @@ -62,13 +62,7 @@ protected function getArgDefinitionType(
ObjectFieldArgumentSource|InterfaceFieldArgumentSource $argument,
Context $context,
): ListTypeNode|NamedTypeNode|NonNullTypeNode {
$type = $this->getArgumentTypeDefinitionNode(
$manipulator,
$document,
$argument,
SearchByOperatorConditionDirective::class,
$context,
);
$type = $this->getArgumentTypeDefinitionNode($manipulator, $document, $argument, $context, Root::class);

if (!$type) {
throw new FailedToCreateSearchCondition($argument);
Expand Down
19 changes: 17 additions & 2 deletions packages/graphql/src/SearchBy/Directives/DirectiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Definitions\SearchByOperatorBetweenDirective;
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Definitions\SearchByOperatorEqualDirective;
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators;
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\BaseOperator;
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Operator;
use LastDragon_ru\LaraASP\GraphQL\Testing\Package\Data\Models\WithTestObject;
use LastDragon_ru\LaraASP\GraphQL\Testing\Package\DataProviders\BuilderDataProvider;
use LastDragon_ru\LaraASP\GraphQL\Testing\Package\DataProviders\EloquentBuilderDataProvider;
Expand Down Expand Up @@ -339,6 +339,11 @@ static function (MockInterface $mock) use ($resolver): void {
*/
public static function dataProviderManipulateArgDefinition(): array {
return [
'Query' => [
'Query.expected.graphql',
'Query.schema.graphql',
null,
],
'Explicit' => [
'Explicit.expected.graphql',
'Explicit.schema.graphql',
Expand Down Expand Up @@ -882,7 +887,17 @@ public function __invoke(): mixed {
* @internal
* @noinspection PhpMultipleClassesDeclarationsInOneFile
*/
class DirectiveTest__CustomComplexOperator extends BaseOperator implements TypeDefinition {
class DirectiveTest__QueryBuilderResolver {
public function __invoke(): QueryBuilder {
throw new Exception('Should not be called.');
}
}

/**
* @internal
* @noinspection PhpMultipleClassesDeclarationsInOneFile
*/
class DirectiveTest__CustomComplexOperator extends Operator implements TypeDefinition {
#[Override]
public static function getName(): string {
return 'custom';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on
| INPUT_FIELD_DEFINITION
| SCALAR

directive @searchByOperatorProperty
directive @searchByOperatorField
on
| ENUM
| INPUT_FIELD_DEFINITION
Expand All @@ -22,10 +22,10 @@ Available conditions for `type A` (only one property allowed at a time).
"""
input SearchByConditionA {
"""
Property condition.
Field condition.
"""
a: SearchByScalarString
@searchByOperatorProperty
@searchByOperatorField
@rename(
attribute: "renamed"
)
Expand All @@ -36,10 +36,10 @@ Available conditions for `interface B` (only one property allowed at a time).
"""
input SearchByConditionB {
"""
Property condition.
Field condition.
"""
b: SearchByScalarString
@searchByOperatorProperty
@searchByOperatorField
@rename(
attribute: "renamed"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ on
| INPUT_FIELD_DEFINITION
| SCALAR

directive @searchByOperatorField
on
| ENUM
| INPUT_FIELD_DEFINITION
| SCALAR

directive @searchByOperatorGreaterThan
on
| ENUM
Expand Down Expand Up @@ -173,12 +179,6 @@ on
| INPUT_FIELD_DEFINITION
| SCALAR

directive @searchByOperatorProperty
on
| ENUM
| INPUT_FIELD_DEFINITION
| SCALAR

directive @searchByOperatorRelationship
on
| ENUM
Expand Down Expand Up @@ -231,10 +231,10 @@ input SearchByConditionChild {
@searchByOperatorNot

"""
Property condition.
Field condition.
"""
value: SearchByScalarStringOrNull
@searchByOperatorProperty
@searchByOperatorField
}

"""
Expand Down
Loading

0 comments on commit abef051

Please sign in to comment.