Skip to content

Commit

Permalink
refactor(graphql)!: Added TypeSource::isScalar(), `TypeSource::isEn…
Browse files Browse the repository at this point in the history
…um()`.
  • Loading branch information
LastDragon-ru committed Jan 31, 2024
1 parent 528a5c4 commit 0600d0d
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 51 deletions.
2 changes: 2 additions & 0 deletions packages/graphql/UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ This section is actual only if you are extending the package. Please review and

* [ ] `LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeProvider`

* [ ] `LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeSource`

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

* [ ] Removed `BuilderInfo`. To get `BuilderInfo` instance within Operator the `LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Context` should be used instead
Expand Down
4 changes: 4 additions & 0 deletions packages/graphql/src/Builder/Contracts/TypeSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ public function isList(): bool;
public function isUnion(): bool;

public function isObject(): bool;

public function isScalar(): bool;

public function isEnum(): bool;
}
10 changes: 10 additions & 0 deletions packages/graphql/src/Builder/Sources/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ public function isObject(): bool {
return $this->getManipulator()->isObject($this->getType());
}

#[Override]
public function isScalar(): bool {
return $this->getManipulator()->isScalar($this->getType());
}

#[Override]
public function isEnum(): bool {
return $this->getManipulator()->isEnum($this->getType());
}

#[Override]
public function __toString(): string {
return $this->getManipulator()->getTypeFullName($this->getType());
Expand Down
32 changes: 5 additions & 27 deletions packages/graphql/src/SearchBy/Types/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,7 @@

namespace LastDragon_ru\LaraASP\GraphQL\SearchBy\Types;

use GraphQL\Language\AST\EnumTypeDefinitionNode;
use GraphQL\Language\AST\InputObjectTypeDefinitionNode;
use GraphQL\Language\AST\InterfaceTypeDefinitionNode;
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
use GraphQL\Language\AST\ScalarTypeDefinitionNode;
use GraphQL\Language\Parser;
use GraphQL\Type\Definition\EnumType;
use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\InterfaceType;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\ScalarType;
use LastDragon_ru\LaraASP\GraphQL\Builder\Context\HandlerContextBuilderInfo;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Context;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Operator as OperatorContract;
Expand Down Expand Up @@ -102,23 +92,11 @@ protected function getFieldOperator(
InputFieldSource|ObjectFieldSource|InterfaceFieldSource $field,
Context $context,
): ?array {
$fieldType = $field->getTypeDefinition();
$operator = match (true) {
$fieldType instanceof ScalarTypeDefinitionNode,
$fieldType instanceof ScalarType
=> Scalar::class,
$fieldType instanceof EnumTypeDefinitionNode,
$fieldType instanceof EnumType
=> Enumeration::class,
$fieldType instanceof InputObjectTypeDefinitionNode,
$fieldType instanceof ObjectTypeDefinitionNode,
$fieldType instanceof InputObjectType,
$fieldType instanceof ObjectType,
$fieldType instanceof InterfaceTypeDefinitionNode,
$fieldType instanceof InterfaceType
=> $this->getObjectDefaultOperator($manipulator, $field, $context),
default
=> null,
$operator = match (true) {
$field->isScalar() => Scalar::class,
$field->isEnum() => Enumeration::class,
$field->isObject() => $this->getObjectDefaultOperator($manipulator, $field, $context),
default => null,
};

if (!$operator) {
Expand Down
21 changes: 4 additions & 17 deletions packages/graphql/src/SortBy/Types/Clause.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@

namespace LastDragon_ru\LaraASP\GraphQL\SortBy\Types;

use GraphQL\Language\AST\InputObjectTypeDefinitionNode;
use GraphQL\Language\AST\InterfaceTypeDefinitionNode;
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
use GraphQL\Language\Parser;
use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\InterfaceType;
use GraphQL\Type\Definition\ObjectType;
use LastDragon_ru\LaraASP\GraphQL\Builder\Context\HandlerContextBuilderInfo;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Context;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Operator as OperatorContract;
Expand Down Expand Up @@ -118,17 +112,10 @@ protected function getFieldOperator(
InputFieldSource|ObjectFieldSource|InterfaceFieldSource $field,
Context $context,
): ?array {
$fieldType = $field->getTypeDefinition();
$isNested = $fieldType instanceof InputObjectTypeDefinitionNode
|| $fieldType instanceof ObjectTypeDefinitionNode
|| $fieldType instanceof InputObjectType
|| $fieldType instanceof ObjectType
|| $fieldType instanceof InterfaceTypeDefinitionNode
|| $fieldType instanceof InterfaceType;
$operator = null;
$source = null;

if ($isNested) {
$operator = null;
$source = null;

if ($field->isObject()) {
$operator = $this->getObjectDefaultOperator($manipulator, $field, $context);
} else {
$type = $manipulator->getType(Direction::class, $field, $context);
Expand Down
40 changes: 33 additions & 7 deletions packages/graphql/src/Utils/AstManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,30 @@ public function isObject(
|| $type instanceof InputObjectType;
}

/**
* @param Node|Type|InputObjectField|FieldDefinition|(TypeDefinitionNode&Node) $node
*/
public function isScalar(
Node|Type|InputObjectField|FieldDefinition|TypeDefinitionNode $node,
): bool {
$type = $this->getType($node);

return $type instanceof ScalarTypeDefinitionNode
|| $type instanceof ScalarType;
}

/**
* @param Node|Type|InputObjectField|FieldDefinition|(TypeDefinitionNode&Node) $node
*/
public function isEnum(
Node|Type|InputObjectField|FieldDefinition|TypeDefinitionNode $node,
): bool {
$type = $this->getType($node);

return $type instanceof EnumTypeDefinitionNode
|| $type instanceof EnumType;
}

public function isDeprecated(
Node|Argument|EnumValueDefinition|FieldDefinition|InputObjectField $node,
): bool {
Expand Down Expand Up @@ -926,21 +950,23 @@ private function setType(
private function getType(
Node|Type|InputObjectField|FieldDefinition|TypeDefinitionNode $node,
): TypeDefinitionNode|Type|null {
$type = null;

if ($node instanceof WrappingType) {
$type = $node->getInnermostType();
if ($node instanceof InputObjectField || $node instanceof FieldDefinition) {
$node = $node->getType();
} elseif ($node instanceof Node) {
try {
$type = $this->getTypeDefinition($node);
$node = $this->getTypeDefinition($node);
} catch (TypeDefinitionUnknown) {
// empty
$node = null;
}
} else {
// empty
}

return $type;
if ($node instanceof WrappingType) {
$node = $node->getInnermostType();
}

return $node;
}
// </editor-fold>
}

0 comments on commit 0600d0d

Please sign in to comment.