Skip to content

Commit

Permalink
refactor(graphql)!: AstManipulator more methods, deprecated methods…
Browse files Browse the repository at this point in the history
… removal and fixes.
  • Loading branch information
LastDragon-ru committed Aug 15, 2023
2 parents 26a6165 + ff0bce1 commit 9b62997
Show file tree
Hide file tree
Showing 15 changed files with 1,280 additions and 163 deletions.
47 changes: 10 additions & 37 deletions packages/graphql/src/Builder/Directives/HandlerDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
use LastDragon_ru\LaraASP\GraphQL\Builder\Sources\InterfaceFieldArgumentSource;
use LastDragon_ru\LaraASP\GraphQL\Builder\Sources\ObjectFieldArgumentSource;
use LastDragon_ru\LaraASP\GraphQL\Builder\Traits\WithBuilderInfo;
use LastDragon_ru\LaraASP\GraphQL\Exceptions\NotImplemented;
use LastDragon_ru\LaraASP\GraphQL\Utils\ArgumentFactory;
use Nuwave\Lighthouse\Execution\Arguments\Argument;
use Nuwave\Lighthouse\Execution\Arguments\ArgumentSet;
Expand Down Expand Up @@ -195,46 +194,20 @@ public function manipulateArgDefinition(
$builder = $this->getFieldArgumentBuilderInfo($documentAST, $parentType, $parentField, $argDefinition);
$manipulator = $this->getManipulator($documentAST, $builder);

if ($this->isTypeName($manipulator->getNodeTypeName($argDefinition))) {
if ($this->isTypeName($manipulator->getTypeName($argDefinition))) {
return;
}

// Argument
$argSource = $this->getFieldArgumentSource($manipulator, $parentType, $parentField, $argDefinition);
$argDefinition->type = $this->getArgDefinitionType($manipulator, $documentAST, $argSource);

// Interfaces
$interfaces = $manipulator->getNodeInterfaces($parentType);
$fieldName = $manipulator->getNodeName($parentField);
$argumentName = $manipulator->getNodeName($argDefinition);

foreach ($interfaces as $interface) {
// Field?
$field = $manipulator->getNodeField($interface, $fieldName);

if (!$field) {
continue;
}

// Argument?
$argument = $manipulator->getNodeArgument($field, $argumentName);

if ($argument === null) {
continue;
}

// Directive? (no need to update type here)
if ($manipulator->getNodeDirective($argument, self::class) !== null) {
continue;
}

// Update
if ($argument instanceof InputValueDefinitionNode) {
$argument->type = $argDefinition->type;
} else {
throw new NotImplemented($argument::class);
}
}
$source = $this->getFieldArgumentSource($manipulator, $parentType, $parentField, $argDefinition);
$type = $this->getArgDefinitionType($manipulator, $documentAST, $source);

$manipulator->setArgumentType(
$parentType,
$parentField,
$argDefinition,
$type,
);
}

/**
Expand Down
22 changes: 11 additions & 11 deletions packages/graphql/src/Builder/Manipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ public function getType(string $definition, TypeSource $source): string {
throw new TypeDefinitionImpossibleToCreateType($definition, $source);
}

if ($name !== $this->getNodeName($node)) {
throw new TypeDefinitionInvalidTypeName($definition, $name, $this->getNodeName($node));
if ($name !== $this->getName($node)) {
throw new TypeDefinitionInvalidTypeName($definition, $name, $this->getName($node));
}

// Save
Expand Down Expand Up @@ -168,8 +168,8 @@ public function getTypeOperators(string $scope, string $type, string ...$extras)
$operators = [];

if ($this->isTypeDefinitionExists($type)) {
$node = $this->getTypeDefinitionNode($type);
$directives = $this->getNodeDirectives($node, $scope);
$node = $this->getTypeDefinition($type);
$directives = $this->getDirectives($node, $scope);

foreach ($directives as $directive) {
if ($directive instanceof OperatorsDirective) {
Expand Down Expand Up @@ -235,7 +235,7 @@ public function getOperatorField(
): string {
// Operator already added?
$added = false;
$locator = $this->getDirectives();
$locator = $this->getDirectiveLocator();

foreach ($directives as $directive) {
if ($locator->resolve($directive->name->value) === $operator::class) {
Expand Down Expand Up @@ -305,13 +305,13 @@ protected function addFakeTypeDefinition(string $name): void {

protected function removeFakeTypeDefinition(string $name): void {
// Possible?
$fake = $this->getTypeDefinitionNode($name);
$fake = $this->getTypeDefinition($name);

if (!($fake instanceof InputObjectTypeDefinitionNode)) {
throw new FakeTypeDefinitionUnknown($name);
}

if (count($fake->fields) !== 1 || $this->getNodeName($fake->fields[0]) !== 'fake') {
if (count($fake->fields) !== 1 || $this->getName($fake->fields[0]) !== 'fake') {
throw new FakeTypeDefinitionIsNotFake($name);
}

Expand All @@ -326,10 +326,10 @@ public function getPlaceholderTypeDefinitionNode(
FieldDefinitionNode|FieldDefinition $field,
): TypeDefinitionNode|Type|null {
$node = null;
$paginate = $this->getNodeDirective($field, PaginateDirective::class);
$paginate = $this->getDirective($field, PaginateDirective::class);

if ($paginate) {
$type = $this->getNodeTypeName($this->getTypeDefinitionNode($field));
$type = $this->getTypeName($this->getTypeDefinition($field));
$pagination = (new class() extends PaginateDirective {
public function getPaginationType(PaginateDirective $directive): PaginationType {
return $directive->paginationType();
Expand All @@ -347,10 +347,10 @@ public function getPaginationType(PaginateDirective $directive): PaginationType
}

if ($type) {
$node = $this->getTypeDefinitionNode($type);
$node = $this->getTypeDefinition($type);
}
} else {
$node = $this->getTypeDefinitionNode($field);
$node = $this->getTypeDefinition($field);
}

return $node;
Expand Down
8 changes: 4 additions & 4 deletions packages/graphql/src/Builder/ManipulatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ public function testGetPlaceholderTypeDefinitionNode(?string $expected, string $
$manipulator = new class($directives, $types, $ast) extends Manipulator {
/** @noinspection PhpMissingParentConstructorInspection */
public function __construct(
protected DirectiveLocator $directives,
protected DirectiveLocator $directiveLocator,
protected TypeRegistry $types,
protected DocumentAST $document,
) {
// empty
}

protected function getDirectives(): DirectiveLocator {
return $this->directives;
protected function getDirectiveLocator(): DirectiveLocator {
return $this->directiveLocator;
}

public function getDocument(): DocumentAST {
Expand All @@ -87,7 +87,7 @@ protected function getTypes(): TypeRegistry {

if ($expected !== null) {
self::assertNotNull($type);
self::assertEquals($expected, $manipulator->getNodeName($type));
self::assertEquals($expected, $manipulator->getName($type));
} else {
self::assertNull($type);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/graphql/src/Builder/Sources/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function getType(): TypeDefinitionNode|NamedTypeNode|ListTypeNode|NonNull
}

public function getTypeName(): string {
return $this->getManipulator()->getNodeTypeName($this->getType());
return $this->getManipulator()->getTypeName($this->getType());
}

/**
Expand All @@ -51,7 +51,7 @@ public function getTypeName(): string {
public function getTypeDefinition(): TypeDefinitionNode|Type {
$type = $this->getType();
$definition = !($type instanceof TypeDefinitionNode)
? $this->getManipulator()->getTypeDefinitionNode($type)
? $this->getManipulator()->getTypeDefinition($type)
: $type;

return $definition;
Expand All @@ -66,7 +66,7 @@ public function isList(): bool {
}

public function __toString(): string {
return $this->getManipulator()->getNodeTypeFullName($this->getType());
return $this->getManipulator()->getTypeFullName($this->getType());
}
// </editor-fold>
}
4 changes: 2 additions & 2 deletions packages/graphql/src/Builder/Sources/Traits/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public function hasArguments(): bool {

public function __toString(): string {
$manipulator = $this->getManipulator();
$field = $manipulator->getNodeName($this->getField());
$type = $manipulator->getNodeTypeFullName($this->getObject());
$field = $manipulator->getName($this->getField());
$type = $manipulator->getTypeFullName($this->getObject());

return "{$type} { {$field} }";
}
Expand Down
6 changes: 3 additions & 3 deletions packages/graphql/src/Builder/Sources/Traits/FieldArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
trait FieldArgument {
public function __toString(): string {
$manipulator = $this->getManipulator();
$argument = $manipulator->getNodeName($this->getArgument());
$field = $manipulator->getNodeName($this->getField());
$type = $manipulator->getNodeTypeFullName($this->getObject());
$argument = $manipulator->getName($this->getArgument());
$field = $manipulator->getName($this->getField());
$type = $manipulator->getTypeFullName($this->getObject());

return "{$type} { {$field}({$argument}) }";
}
Expand Down
10 changes: 5 additions & 5 deletions packages/graphql/src/Builder/Types/InputObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function getTypeDefinition(

foreach ($fields as $field) {
// Name should be unique (may conflict with Type's operators)
$fieldName = $manipulator->getNodeName($field);
$fieldName = $manipulator->getName($field);

if (isset($definition->fields[$fieldName])) {
throw new TypeDefinitionFieldAlreadyDefined($fieldName);
Expand Down Expand Up @@ -142,7 +142,7 @@ protected function isFieldConvertable(
}

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

if ($resolver !== null && !$this->isFieldDirectiveAllowed($manipulator, $resolver)) {
return false;
Expand All @@ -166,7 +166,7 @@ protected function getFieldDefinition(
$type = $manipulator->getTypeSource($field->getTypeDefinition());
}

$fieldName = $manipulator->getNodeName($field->getField());
$fieldName = $manipulator->getName($field->getField());
$fieldDesc = $this->getFieldDescription($manipulator, $field);
$fieldDirectives = $this->getFieldDirectives($manipulator, $field);
$fieldDefinition = $manipulator->getOperatorField($operator, $type, $fieldName, $fieldDesc, $fieldDirectives);
Expand Down Expand Up @@ -200,7 +200,7 @@ protected function getFieldDirectiveOperator(
$nodes = [$field->getField(), $field->getTypeDefinition()];

foreach ($nodes as $node) {
$operator = $manipulator->getNodeDirective(
$operator = $manipulator->getDirective(
$node,
$directive,
static function (Operator $operator) use ($builder): bool {
Expand Down Expand Up @@ -247,7 +247,7 @@ protected function getFieldDirectives(
): array {
$directives = [];

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

Expand Down
27 changes: 27 additions & 0 deletions packages/graphql/src/Exceptions/ArgumentAlreadyDefined.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\GraphQL\Exceptions;

use Stringable;
use Throwable;

use function sprintf;

class ArgumentAlreadyDefined extends AstException {
public function __construct(
protected Stringable|string $source,
Throwable $previous = null,
) {
parent::__construct(
sprintf(
'Argument `%s` already defined.',
$this->source,
),
$previous,
);
}

public function getSource(): Stringable|string {
return $this->source;
}
}
33 changes: 33 additions & 0 deletions packages/graphql/src/Exceptions/TypeUnexpected.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\GraphQL\Exceptions;

use Stringable;
use Throwable;

use function sprintf;

class TypeUnexpected extends AstException {
public function __construct(
protected Stringable|string $source,
protected Stringable|string $expected,
Throwable $previous = null,
) {
parent::__construct(
sprintf(
'Type `%s` is not a `%s`.',
$this->source,
$this->expected,
),
$previous,
);
}

public function getSource(): Stringable|string {
return $this->source;
}

public function getExpected(): Stringable|string {
return $this->expected;
}
}
4 changes: 2 additions & 2 deletions packages/graphql/src/SearchBy/Types/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ protected function isFieldConvertable(
}

// Ignored field?
if ($manipulator->getNodeDirective($field->getField(), Ignored::class) !== null) {
if ($manipulator->getDirective($field->getField(), Ignored::class) !== null) {
return false;
}

// Ignored type?
$fieldType = $field->getTypeDefinition();

if ($fieldType instanceof Ignored || $manipulator->getNodeDirective($fieldType, Ignored::class) !== null) {
if ($fieldType instanceof Ignored || $manipulator->getDirective($fieldType, Ignored::class) !== null) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/graphql/src/SearchBy/Types/Enumeration.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function getTypeDefinition(

// Definition
$content = $manipulator->getOperatorsFields($operators, $source);
$typeName = $manipulator->getNodeTypeFullName($source->getType());
$typeName = $manipulator->getTypeFullName($source->getType());
$definition = Parser::inputObjectTypeDefinition(
<<<DEF
"""
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql/src/SearchBy/Types/Scalar.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function getTypeDefinition(

// Definition
$content = $manipulator->getOperatorsFields($operators, $source);
$typeName = $manipulator->getNodeTypeFullName($source->getType());
$typeName = $manipulator->getTypeFullName($source->getType());
$definition = Parser::inputObjectTypeDefinition(
<<<DEF
"""
Expand Down
4 changes: 2 additions & 2 deletions packages/graphql/src/SortBy/Types/Clause.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ protected function isFieldConvertable(
}

// Ignored field?
if ($manipulator->getNodeDirective($field->getField(), Ignored::class) !== null) {
if ($manipulator->getDirective($field->getField(), Ignored::class) !== null) {
return false;
}

// Ignored type?
$fieldType = $field->getTypeDefinition();

if ($fieldType instanceof Ignored || $manipulator->getNodeDirective($fieldType, Ignored::class) !== null) {
if ($fieldType instanceof Ignored || $manipulator->getDirective($fieldType, Ignored::class) !== null) {
return false;
}

Expand Down
Loading

0 comments on commit 9b62997

Please sign in to comment.