Skip to content

Commit

Permalink
Refactor: Replace type+typeId with DTO
Browse files Browse the repository at this point in the history
  • Loading branch information
jerowork committed Jan 3, 2025
1 parent 003b186 commit bbb0aa0
Show file tree
Hide file tree
Showing 50 changed files with 307 additions and 390 deletions.
14 changes: 6 additions & 8 deletions src/Parser/Ast.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Jerowork\GraphqlAttributeSchema\Parser;

use Jerowork\GraphqlAttributeSchema\Parser\Node\Node;
use Jerowork\GraphqlAttributeSchema\Parser\Node\Type;

final readonly class Ast
{
Expand All @@ -21,22 +22,19 @@ public function __construct(Node ...$nodes)
/**
* @template T of Node
*
* @param class-string<T> $type
* @param class-string<T> $nodeType
*
* @return list<T>
*/
public function getNodesByType(string $type): array
public function getNodesByNodeType(string $nodeType): array
{
return array_values(array_filter($this->nodes, fn($node) => $node instanceof $type));
return array_values(array_filter($this->nodes, fn($node) => $node instanceof $nodeType));
}

/**
* @param class-string $typeId
*/
public function getNodeByTypeId(string $typeId): ?Node
public function getNodeByType(Type $type): ?Node
{
foreach ($this->nodes as $node) {
if ($node->getTypeId() !== $typeId) {
if (!$node->getType()->equals($type)) {
continue;
}

Expand Down
10 changes: 3 additions & 7 deletions src/Parser/Node/ArgNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,16 @@

final readonly class ArgNode implements Node
{
/**
* @param class-string|null $typeId
*/
public function __construct(
public ?string $typeId,
public ?string $type,
public Type $type,
public string $name,
public ?string $description,
public bool $isRequired,
public string $propertyName,
) {}

public function getTypeId(): ?string
public function getType(): Type
{
return $this->typeId;
return $this->type;
}
}
7 changes: 3 additions & 4 deletions src/Parser/Node/EnumNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@
final readonly class EnumNode implements Node
{
/**
* @param class-string $typeId
* @param list<string> $cases
*/
public function __construct(
public string $typeId,
public Type $type,
public string $name,
public ?string $description,
public array $cases,
) {}

public function getTypeId(): string
public function getType(): Type
{
return $this->typeId;
return $this->type;
}
}
8 changes: 3 additions & 5 deletions src/Parser/Node/FieldNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
final readonly class FieldNode implements Node
{
/**
* @param class-string|null $typeId
* @param list<ArgNode> $argNodes
*/
public function __construct(
public ?string $typeId,
public ?string $type,
public Type $type,
public string $name,
public ?string $description,
public bool $isRequired,
Expand All @@ -22,8 +20,8 @@ public function __construct(
public ?string $propertyName,
) {}

public function getTypeId(): ?string
public function getType(): Type
{
return $this->typeId;
return $this->type;
}
}
7 changes: 3 additions & 4 deletions src/Parser/Node/InputTypeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@
final readonly class InputTypeNode implements Node
{
/**
* @param class-string $typeId
* @param list<FieldNode> $fieldNodes
*/
public function __construct(
public string $typeId,
public Type $type,
public string $name,
public ?string $description,
public array $fieldNodes,
) {}

public function getTypeId(): string
public function getType(): Type
{
return $this->typeId;
return $this->type;
}
}
11 changes: 4 additions & 7 deletions src/Parser/Node/MutationNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,20 @@
final readonly class MutationNode implements Node
{
/**
* @param class-string $typeId
* @param list<ArgNode> $argNodes
* @param class-string|null $outputTypeId
*/
public function __construct(
public string $typeId,
public Type $type,
public string $name,
public ?string $description,
public array $argNodes,
public ?string $outputTypeId,
public ?string $outputType,
public Type $outputType,
public bool $isRequired,
public string $methodName,
) {}

public function getTypeId(): string
public function getType(): Type
{
return $this->typeId;
return $this->type;
}
}
2 changes: 1 addition & 1 deletion src/Parser/Node/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

interface Node
{
public function getTypeId(): ?string;
public function getType(): Type;
}
11 changes: 4 additions & 7 deletions src/Parser/Node/QueryNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,20 @@
final readonly class QueryNode implements Node
{
/**
* @param class-string $typeId
* @param list<ArgNode> $argNodes
* @param class-string|null $outputTypeId
*/
public function __construct(
public string $typeId,
public Type $type,
public string $name,
public ?string $description,
public array $argNodes,
public ?string $outputTypeId,
public ?string $outputType,
public Type $outputType,
public bool $isRequired,
public string $methodName,
) {}

public function getTypeId(): string
public function getType(): Type
{
return $this->typeId;
return $this->type;
}
}
41 changes: 41 additions & 0 deletions src/Parser/Node/Type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Jerowork\GraphqlAttributeSchema\Parser\Node;

final readonly class Type
{
/**
* @param string|class-string $id
*/
public function __construct(
public string $id,
public TypeType $type,
) {}

public static function createScalar(string $name): self
{
return new self($name, TypeType::Scalar);
}

public static function createObject(string $name): self
{
return new self($name, TypeType::Object);
}

public function isScalar(): bool
{
return $this->type === TypeType::Scalar;
}

public function isObject(): bool
{
return $this->type === TypeType::Object;
}

public function equals(Type $type): bool
{
return $this->id === $type->id && $this->type === $type->type;
}
}
7 changes: 3 additions & 4 deletions src/Parser/Node/TypeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@
final readonly class TypeNode implements Node
{
/**
* @param class-string $typeId
* @param list<FieldNode> $fieldNodes
*/
public function __construct(
public string $typeId,
public Type $type,
public string $name,
public ?string $description,
public array $fieldNodes,
) {}

public function getTypeId(): string
public function getType(): Type
{
return $this->typeId;
return $this->type;
}
}
11 changes: 11 additions & 0 deletions src/Parser/Node/TypeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Jerowork\GraphqlAttributeSchema\Parser\Node;

enum TypeType: string
{
case Scalar = 'scalar';
case Object = 'object';
}
4 changes: 0 additions & 4 deletions src/Parser/NodeParser/Child/ClassFieldNodesParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Jerowork\GraphqlAttributeSchema\Attribute\Field;
use Jerowork\GraphqlAttributeSchema\Parser\Node\FieldNode;
use Jerowork\GraphqlAttributeSchema\Parser\Node\FieldNodeType;
use Jerowork\GraphqlAttributeSchema\Parser\NodeParser\GetTypeIdTrait;
use Jerowork\GraphqlAttributeSchema\Parser\NodeParser\GetTypeTrait;
use Jerowork\GraphqlAttributeSchema\Parser\NodeParser\ParseException;
use Jerowork\GraphqlAttributeSchema\Parser\NodeParser\RetrieveNameForFieldTrait;
Expand All @@ -20,7 +19,6 @@
{
use RetrieveNameForFieldTrait;
use GetTypeTrait;
use GetTypeIdTrait;

private const array RESERVED_METHOD_NAMES = ['__construct'];
private const string RETURN_TYPE_VOID = 'void';
Expand Down Expand Up @@ -52,7 +50,6 @@ public function parse(ReflectionClass $class): array
}

$fieldNodes[] = new FieldNode(
$this->getTypeId($propertyType),
$this->getType($propertyType),
$fieldAttribute->name ?? $property->getName(),
$fieldAttribute->description,
Expand Down Expand Up @@ -80,7 +77,6 @@ public function parse(ReflectionClass $class): array
}

$fieldNodes[] = new FieldNode(
$this->getTypeId($returnType),
$this->getType($returnType),
$this->retrieveNameForField($method, $fieldAttribute),
$fieldAttribute->description,
Expand Down
3 changes: 0 additions & 3 deletions src/Parser/NodeParser/Child/MethodArgNodesParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Jerowork\GraphqlAttributeSchema\Attribute\Arg;
use Jerowork\GraphqlAttributeSchema\Parser\Node\ArgNode;
use Jerowork\GraphqlAttributeSchema\Parser\NodeParser\GetTypeIdTrait;
use Jerowork\GraphqlAttributeSchema\Parser\NodeParser\GetTypeTrait;
use Jerowork\GraphqlAttributeSchema\Parser\NodeParser\ParseException;
use ReflectionMethod;
Expand All @@ -16,7 +15,6 @@
final readonly class MethodArgNodesParser
{
use GetTypeTrait;
use GetTypeIdTrait;

/**
* @throws ParseException
Expand All @@ -36,7 +34,6 @@ public function parse(ReflectionMethod $method): array
}

$argNodes[] = new ArgNode(
$this->getTypeId($parameterType),
$this->getType($parameterType),
$argAttribute->name ?? $parameter->getName(),
$argAttribute?->description,
Expand Down
3 changes: 2 additions & 1 deletion src/Parser/NodeParser/EnumNodeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Jerowork\GraphqlAttributeSchema\Attribute\Enum;
use Jerowork\GraphqlAttributeSchema\Parser\Node\EnumNode;
use Jerowork\GraphqlAttributeSchema\Parser\Node\Node;
use Jerowork\GraphqlAttributeSchema\Parser\Node\Type;
use ReflectionClass;
use BackedEnum;
use Override;
Expand Down Expand Up @@ -37,7 +38,7 @@ public function parse(ReflectionClass $class): Node
$attribute = $this->getClassAttribute($class, Enum::class);

return new EnumNode(
$className,
Type::createObject($className),
$this->retrieveNameForType($class, $attribute),
$attribute->getDescription(),
array_map(fn($case) => (string) $case->value, $className::cases()),
Expand Down
23 changes: 0 additions & 23 deletions src/Parser/NodeParser/GetTypeIdTrait.php

This file was deleted.

9 changes: 7 additions & 2 deletions src/Parser/NodeParser/GetTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@

namespace Jerowork\GraphqlAttributeSchema\Parser\NodeParser;

use Jerowork\GraphqlAttributeSchema\Parser\Node\Type;
use ReflectionNamedType;

trait GetTypeTrait
{
public function getType(ReflectionNamedType $type): ?string
public function getType(ReflectionNamedType $type): Type
{
return $type->isBuiltin() ? $type->getName() : null;
if ($type->isBuiltin()) {
return Type::createScalar($type->getName());
}

return Type::createObject($type->getName());
}
}
Loading

0 comments on commit bbb0aa0

Please sign in to comment.