Skip to content

Commit

Permalink
release: @rename support πŸ±β€πŸ
Browse files Browse the repository at this point in the history
  • Loading branch information
LastDragon-ru committed Jun 9, 2023
2 parents 51a512b + 9b38a95 commit fe32601
Show file tree
Hide file tree
Showing 20 changed files with 1,821 additions and 908 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@
"ext-pdo_sqlite": "*",
"laravel/scout": "^9.2.1|^10.0.0",
"mockery/mockery": "^1.4.2",
"nunomaduro/larastan": "2.6.0",
"nunomaduro/larastan": "2.6.1",
"orchestra/testbench": "^6.9.0|^7.0.0|^8.0.0",
"phpstan/phpstan": "1.10.15",
"phpstan/phpstan": "1.10.18",
"phpstan/phpstan-mockery": "^1.0.0",
"phpstan/phpstan-phpunit": "^1.0.0",
"phpstan/phpstan-strict-rules": "^1.1.0",
Expand Down
2,385 changes: 1,531 additions & 854 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"devDependencies": {
"@release-it/bumper": "^4.0.0",
"@release-it/conventional-changelog": "^5.0.0",
"markdownlint-cli": "^0.33.0",
"release-it": "~15.10.1"
"markdownlint-cli": "^0.34.0",
"release-it": "~15.11.0"
}
}
4 changes: 4 additions & 0 deletions packages/graphql/src/Builder/Contracts/Operator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public function getFieldType(TypeProvider $provider, TypeSource $source): string

public function getFieldDescription(): string;

/**
* @deprecated 4.2.1 The actual `Directive` node will be used. So the method
* is not needed anymore and will be removed in the next major version.
*/
public function getFieldDirective(): ?DirectiveNode;

/**
Expand Down
5 changes: 5 additions & 0 deletions packages/graphql/src/Builder/Directives/HandlerDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
use Nuwave\Lighthouse\Schema\Directives\FindDirective;
use Nuwave\Lighthouse\Schema\Directives\FirstDirective;
use Nuwave\Lighthouse\Schema\Directives\RelationDirective;
use Nuwave\Lighthouse\Schema\Directives\RenameDirective;
use Nuwave\Lighthouse\Schema\Directives\WithRelationDirective;
use Nuwave\Lighthouse\Scout\SearchDirective;
use Nuwave\Lighthouse\Support\Contracts\Directive;
Expand Down Expand Up @@ -175,6 +176,10 @@ protected function call(object $builder, Property $property, ArgumentSet $operat
if ($directive instanceof Operator) {
$operators[] = $directive;
}

if ($directive instanceof RenameDirective) {
$name = $directive->attributeArgValue();
}
}

$property = $property->getChild($name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ protected static function getDirectiveLocations(): array {
}

public function getFieldDirective(): ?DirectiveNode {
return $this->directiveNode ?? null;
return null;
}
}
36 changes: 31 additions & 5 deletions packages/graphql/src/Builder/Manipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

use function array_map;
use function array_push;
use function array_unshift;
use function array_values;
use function count;
use function implode;
Expand Down Expand Up @@ -215,25 +216,50 @@ public function getTypeOperators(string $scope, string $type, string ...$extras)
return $unique;
}

/**
* @param array<DirectiveNode> $directives
*/
public function getOperatorField(
Operator $operator,
TypeSource $source,
?string $field,
?string $description = null,
array $directives = [],
): string {
// Operator already added?
$added = false;
$locator = $this->getDirectives();

foreach ($directives as $directive) {
if ($locator->resolve($directive->name->value) === $operator::class) {
$added = true;
break;
}
}

if (!$added) {
array_unshift($directives, Parser::directive('@'.DirectiveLocator::directiveName($operator::class)));
}

// Definition
$type = $operator->getFieldType($this, $source);
$field = $field ?: $operator::getName();
$directive = $operator->getFieldDirective();
$directive = $directive instanceof DirectiveNode
? Printer::doPrint($directive)
: '@'.DirectiveLocator::directiveName($operator::class);
$directives = implode(
"\n",
array_map(
static function (DirectiveNode $node): string {
return Printer::doPrint($node);
},
$directives,
),
);
$description = $description ?: $operator->getFieldDescription();
$description = BlockString::print($description);

return <<<DEF
{$description}
{$field}: {$type}
{$directive}
{$directives}
DEF;
}

Expand Down
35 changes: 23 additions & 12 deletions packages/graphql/src/Builder/ManipulatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace LastDragon_ru\LaraASP\GraphQL\Builder;

use GraphQL\Type\Definition\CustomScalarType;
use GraphQL\Type\Definition\ObjectType;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Operator;
Expand Down Expand Up @@ -93,18 +94,6 @@ protected function getTypes(): TypeRegistry {
}

public function testGetTypeOperators(): void {
// Schema
$this->useGraphQLSchema(
<<<'GRAPHQL'
scalar TestScalar @aOperator @bOperator @cOperator
scalar TestOperators @operators(type: "TestScalar")
type Query {
test: Int @all
}
GRAPHQL,
);

// Operators
$scope = new class() implements Scope {
// empty;
Expand All @@ -114,6 +103,16 @@ public function testGetTypeOperators(): void {
$bOperator = ManipulatorTest_OperatorB::class;
$cOperator = ManipulatorTest_OperatorC::class;

// Types
$types = $this->app->make(TypeRegistry::class);

$types->register(new CustomScalarType([
'name' => 'TestScalar',
]));
$types->register(new CustomScalarType([
'name' => 'TestOperators',
]));

// Directives
$directives = $this->app->make(DirectiveLocator::class);

Expand All @@ -122,6 +121,18 @@ public function testGetTypeOperators(): void {
$directives->setResolved('bOperator', $bOperator);
$directives->setResolved('cOperator', $cOperator);

// Schema
$this->useGraphQLSchema(
<<<'GRAPHQL'
scalar TestScalar @aOperator @bOperator @cOperator
scalar TestOperators @operators(type: "TestScalar")
type Query {
test: Int @all
}
GRAPHQL,
);

// Manipulator
$document = $this->app->make(ASTBuilder::class)->documentAST();
$operators = new class() extends Operators {
Expand Down
7 changes: 5 additions & 2 deletions packages/graphql/src/Builder/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
use function array_slice;
use function array_values;
use function end;
use function explode;
use function implode;

class Property implements Stringable {
protected const Separator = '.';

/**
* @var list<string>
*/
Expand All @@ -33,7 +36,7 @@ public function getPath(): array {
}

public function getChild(string $name): static {
return new static(...[...$this->path, $name]);
return new static(...$this->path, ...explode(static::Separator, $name));
}

public function getParent(): static {
Expand All @@ -44,6 +47,6 @@ public function getParent(): static {
}

public function __toString(): string {
return implode('.', $this->path);
return implode(static::Separator, $this->path);
}
}
37 changes: 35 additions & 2 deletions packages/graphql/src/Builder/Types/InputObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace LastDragon_ru\LaraASP\GraphQL\Builder\Types;

use GraphQL\Language\AST\DirectiveNode;
use GraphQL\Language\AST\InputValueDefinitionNode;
use GraphQL\Language\AST\StringValueNode;
use GraphQL\Language\AST\TypeDefinitionNode;
Expand All @@ -20,6 +21,8 @@
use LastDragon_ru\LaraASP\GraphQL\Builder\Sources\InterfaceSource;
use LastDragon_ru\LaraASP\GraphQL\Builder\Sources\ObjectFieldSource;
use LastDragon_ru\LaraASP\GraphQL\Builder\Sources\ObjectSource;
use Nuwave\Lighthouse\Schema\Directives\RenameDirective;
use Nuwave\Lighthouse\Support\Contracts\Directive;
use Nuwave\Lighthouse\Support\Contracts\FieldResolver;

use function count;
Expand Down Expand Up @@ -139,7 +142,9 @@ protected function isFieldConvertable(
}

// Resolver?
if ($manipulator->getNodeDirective($field->getField(), FieldResolver::class)) {
$resolver = $manipulator->getNodeDirective($field->getField(), FieldResolver::class);

if ($resolver !== null && !$this->isFieldDirectiveAllowed($manipulator, $resolver)) {
return false;
}

Expand All @@ -163,7 +168,8 @@ protected function getFieldDefinition(

$fieldName = $manipulator->getNodeName($field->getField());
$fieldDesc = $this->getFieldDescription($manipulator, $field);
$fieldDefinition = $manipulator->getOperatorField($operator, $type, $fieldName, $fieldDesc);
$fieldDirectives = $this->getFieldDirectives($manipulator, $field);
$fieldDefinition = $manipulator->getOperatorField($operator, $type, $fieldName, $fieldDesc, $fieldDirectives);

return Parser::inputValueDefinition($fieldDefinition);
}
Expand Down Expand Up @@ -231,4 +237,31 @@ protected function getFieldDescription(

return $description;
}

/**
* @return array<DirectiveNode>
*/
protected function getFieldDirectives(
Manipulator $manipulator,
InputFieldSource|ObjectFieldSource|InterfaceFieldSource $field,
): array {
$directives = [];

foreach ($manipulator->getNodeDirectives($field->getField()) as $directive) {
if ($this->isFieldDirectiveAllowed($manipulator, $directive)) {
$node = $manipulator->getDirectiveNode($directive);

if ($node) {
$directives[] = $node;
}
}
}

return $directives;
}

protected function isFieldDirectiveAllowed(Manipulator $manipulator, Directive $directive): bool {
return $directive instanceof Operator
|| $directive instanceof RenameDirective;
}
}
14 changes: 8 additions & 6 deletions packages/graphql/src/SearchBy/Directives/DirectiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use GraphQL\Type\Definition\EnumType;
use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\Type;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Model as EloquentModel;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -76,7 +78,7 @@ class DirectiveTest extends TestCase {
* @dataProvider dataProviderManipulateArgDefinition
*
* @param Closure(static): GraphQLExpectedSchema $expected
* @param Closure(static): void $prepare
* @param Closure(static): void|null $prepare
*/
public function testManipulateArgDefinition(Closure $expected, string $graphql, ?Closure $prepare = null): void {
$directives = $this->app->make(DirectiveLocator::class);
Expand Down Expand Up @@ -238,8 +240,8 @@ public function testDirective(
/**
* @dataProvider dataProviderHandleBuilder
*
* @param array{query: string, bindings: array<mixed>}|Exception $expected
* @param Closure(static): object $builderFactory
* @param array{query: string, bindings: array<mixed>}|Exception $expected
* @param Closure(static): (QueryBuilder|EloquentBuilder<EloquentModel>) $builderFactory
*/
public function testHandleBuilder(
array|Exception $expected,
Expand Down Expand Up @@ -311,7 +313,7 @@ public function testHandleScoutBuilder(
input Test {
a: Int!
b: String
b: String @rename(attribute: "renamed")
c: Test
}
GRAPHQL,
Expand Down Expand Up @@ -703,7 +705,7 @@ public static function dataProviderHandleScoutBuilder(): array {
'c.a' => 2,
],
'whereIns' => [
'b' => [1, 2, 3],
'renamed' => [1, 2, 3],
],
],
[
Expand Down Expand Up @@ -736,7 +738,7 @@ public static function dataProviderHandleScoutBuilder(): array {
'properties/c/a' => 2,
],
'whereIns' => [
'properties/b' => [1, 2, 3],
'properties/renamed' => [1, 2, 3],
],
],
[
Expand Down
Loading

0 comments on commit fe32601

Please sign in to comment.