Skip to content

Commit

Permalink
Rename Type for Nodes (root) to ClassName
Browse files Browse the repository at this point in the history
Next step in differentiate between types and class names
  • Loading branch information
jerowork committed Jan 6, 2025
1 parent 967bd93 commit 5860c16
Show file tree
Hide file tree
Showing 31 changed files with 90 additions and 91 deletions.
5 changes: 2 additions & 3 deletions src/Parser/Ast.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Jerowork\GraphqlAttributeSchema\Parser;

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

final readonly class Ast
{
Expand All @@ -31,10 +30,10 @@ public function getNodesByNodeType(string $nodeType): array
return array_values(array_filter($this->nodes, fn($node) => $node instanceof $nodeType));
}

public function getNodeByType(Type $type): ?Node
public function getNodeByClassName(string $className): ?Node
{
foreach ($this->nodes as $node) {
if (!$node->getType()->equals($type)) {
if ($node->getClassName() !== $className) {
continue;
}

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

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

public function getType(): Type
public function getClassName(): string
{
return $this->type;
return $this->className;
}
}
7 changes: 4 additions & 3 deletions src/Parser/Node/MutationNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
final readonly class MutationNode implements Node
{
/**
* @param class-string $className
* @param list<ArgNode> $argNodes
*/
public function __construct(
public Type $type,
public string $className,
public string $name,
public ?string $description,
public array $argNodes,
Expand All @@ -21,8 +22,8 @@ public function __construct(
public string $methodName,
) {}

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

interface Node
{
public function getType(): Type;
/**
* @return class-string
*/
public function getClassName(): string;
}
7 changes: 4 additions & 3 deletions src/Parser/Node/QueryNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
final readonly class QueryNode implements Node
{
/**
* @param class-string $className
* @param list<ArgNode> $argNodes
*/
public function __construct(
public Type $type,
public string $className,
public string $name,
public ?string $description,
public array $argNodes,
Expand All @@ -21,8 +22,8 @@ public function __construct(
public string $methodName,
) {}

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

public function getType(): Type
public function getClassName(): string
{
return $this->type;
return $this->className;
}
}
3 changes: 1 addition & 2 deletions src/Parser/NodeParser/EnumNodeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Jerowork\GraphqlAttributeSchema\Parser\Node\EnumNode;
use Jerowork\GraphqlAttributeSchema\Parser\Node\EnumValueNode;
use Jerowork\GraphqlAttributeSchema\Parser\Node\Node;
use Jerowork\GraphqlAttributeSchema\Parser\Node\Type;
use ReflectionClass;
use BackedEnum;
use Override;
Expand Down Expand Up @@ -48,7 +47,7 @@ public function parse(ReflectionClass $class): Node

/** @var ReflectionClass<UnitEnum> $class */
return new EnumNode(
Type::createObject($className),
$className,
$this->retrieveNameForType($class, $attribute),
$attribute->getDescription(),
$this->getValues($class),
Expand Down
3 changes: 1 addition & 2 deletions src/Parser/NodeParser/InputTypeNodeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Jerowork\GraphqlAttributeSchema\Attribute\InputType;
use Jerowork\GraphqlAttributeSchema\Parser\Node\InputTypeNode;
use Jerowork\GraphqlAttributeSchema\Parser\Node\Node;
use Jerowork\GraphqlAttributeSchema\Parser\Node\Type;
use Jerowork\GraphqlAttributeSchema\Parser\NodeParser\Child\ClassFieldNodesParser;
use ReflectionClass;
use Override;
Expand All @@ -33,7 +32,7 @@ public function parse(ReflectionClass $class): Node
$attribute = $this->getClassAttribute($class, InputType::class);

return new InputTypeNode(
Type::createObject($class->getName()),
$class->getName(),
$this->retrieveNameForType($class, $attribute),
$attribute->getDescription(),
$this->classFieldNodesParser->parse($class),
Expand Down
2 changes: 1 addition & 1 deletion src/Parser/NodeParser/MutationNodeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function parse(ReflectionClass $class): Node
}

return new MutationNode(
Type::createObject($class->getName()),
$class->getName(),
$this->retrieveNameForResolver($class, $attribute, self::RESOLVER_SUFFIX),
$attribute->getDescription(),
$this->methodArgNodesParser->parse($method),
Expand Down
2 changes: 1 addition & 1 deletion src/Parser/NodeParser/QueryNodeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function parse(ReflectionClass $class): Node
}

return new QueryNode(
Type::createObject($class->getName()),
$class->getName(),
$this->retrieveNameForResolver($class, $attribute, self::RESOLVER_SUFFIX),
$attribute->getDescription(),
$this->methodArgNodesParser->parse($method),
Expand Down
3 changes: 1 addition & 2 deletions src/Parser/NodeParser/TypeNodeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Jerowork\GraphqlAttributeSchema\Attribute\Type;
use Jerowork\GraphqlAttributeSchema\Parser\Node\Node;
use Jerowork\GraphqlAttributeSchema\Parser\Node\Type as NodeType;
use Jerowork\GraphqlAttributeSchema\Parser\Node\TypeNode;
use Jerowork\GraphqlAttributeSchema\Parser\NodeParser\Child\ClassFieldNodesParser;
use ReflectionClass;
Expand All @@ -33,7 +32,7 @@ public function parse(ReflectionClass $class): Node
$attribute = $this->getClassAttribute($class, Type::class);

return new TypeNode(
NodeType::createObject($class->getName()),
$class->getName(),
$this->retrieveNameForType($class, $attribute),
$attribute->getDescription(),
$this->classFieldNodesParser->parse($class),
Expand Down
18 changes: 9 additions & 9 deletions src/TypeBuilder/TypeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,29 +71,29 @@ private function buildScalar(Type $type): WebonyxType
*/
private function buildObject(Type $type, Ast $ast): WebonyxType
{
/** @var class-string $typeName */
$typeName = $type->id;
/** @var class-string $className */
$className = $type->id;

if (array_key_exists($typeName, $this->builtTypes)) {
return $this->builtTypes[$typeName];
if (array_key_exists($className, $this->builtTypes)) {
return $this->builtTypes[$className];
}

$node = $ast->getNodeByType($type);
$node = $ast->getNodeByClassName($className);

if ($node === null) {
throw BuildException::logicError(sprintf('No node found for type: %s', $typeName));
throw BuildException::logicError(sprintf('No node found for class: %s', $className));
}

foreach ($this->objectTypeBuilders as $objectTypeBuilder) {
if (!$objectTypeBuilder->supports($node)) {
continue;
}

$this->builtTypes[$typeName] = $objectTypeBuilder->build($node, $this, $ast);
$this->builtTypes[$className] = $objectTypeBuilder->build($node, $this, $ast);

return $this->builtTypes[$typeName];
return $this->builtTypes[$className];
}

throw BuildException::logicError(sprintf('Invalid object type: %s', $typeName));
throw BuildException::logicError(sprintf('Invalid object class %s', $className));
}
}
2 changes: 1 addition & 1 deletion src/TypeResolver/FieldResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private function resolveChild(FieldNode $field, callable $fieldCallback, Ast $as
return $fieldCallback();
}

$node = $ast->getNodeByType($field->type);
$node = $ast->getNodeByClassName($field->type->id);

if ($node instanceof EnumNode) {
/** @var BackedEnum $enum */
Expand Down
5 changes: 2 additions & 3 deletions src/TypeResolver/ResolveException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Jerowork\GraphqlAttributeSchema\TypeResolver;

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

final class ResolveException extends Exception
{
Expand All @@ -14,8 +13,8 @@ public static function logicError(string $error): self
return new self(sprintf('Logic error: %s', $error));
}

public static function nodeTypeNotInContainer(Type $type): self
public static function nodeClassNameNotInContainer(string $className): self
{
return new self(sprintf('Node type ID %s is not in a container (or not publicly accessible)', $type->id));
return new self(sprintf('Node type ID %s is not in a container (or not publicly accessible)', $className));
}
}
20 changes: 10 additions & 10 deletions src/TypeResolver/RootTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ public function __construct(
*/
public function resolve(MutationNode|QueryNode $node, Ast $ast): callable
{
if (!$this->container->has($node->getType()->id)) {
throw ResolveException::nodeTypeNotInContainer($node->getType());
if (!$this->container->has($node->getClassName())) {
throw ResolveException::nodeClassNameNotInContainer($node->getClassName());
}

return function ($rootValue, array $args) use ($node, $ast) {
/** @var array<string, mixed> $args */
return $this->container->get($node->getType()->id)->{$node->methodName}(
return $this->container->get($node->getClassName())->{$node->methodName}(
...array_map(
fn($arg) => $this->resolveChild($arg, $args, $ast),
$node->argNodes,
Expand All @@ -51,32 +51,32 @@ public function resolveChild(ArgNode|FieldNode $child, array $args, Ast $ast): m
return $args[$child->name];
}

$node = $ast->getNodeByType($child->type);
$node = $ast->getNodeByClassName($child->type->id);

if ($node === null) {
throw ResolveException::logicError(sprintf('Node not found for typeId %s', $child->type->id));
}

if ($node instanceof EnumNode) {
/** @var class-string<BackedEnum> $type */
$type = $node->getType()->id;
/** @var class-string<BackedEnum> $className */
$className = $node->getClassName();
/** @var string $value */
$value = $args[$child->name];

return $type::from($value);
return $className::from($value);
}

if ($node instanceof InputTypeNode) {
/** @var array<string, mixed> $childArgs */
$childArgs = $args[$child->name];
$type = $child->type->id;
$className = $child->type->id;

return new $type(...array_map(
return new $className(...array_map(
fn($fieldNode) => $this->resolveChild($fieldNode, $childArgs, $ast),
$node->fieldNodes,
));
}

throw ResolveException::logicError(sprintf('Node %s cannot be handled', $node->getType()->id));
throw ResolveException::logicError(sprintf('Node %s cannot be handled', $node->getClassName()));
}
}
13 changes: 6 additions & 7 deletions tests/Parser/AstTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Jerowork\GraphqlAttributeSchema\Parser\Node\EnumNode;
use Jerowork\GraphqlAttributeSchema\Parser\Node\EnumValueNode;
use Jerowork\GraphqlAttributeSchema\Parser\Node\InputTypeNode;
use Jerowork\GraphqlAttributeSchema\Parser\Node\Type;
use Jerowork\GraphqlAttributeSchema\Parser\Node\TypeNode;
use Jerowork\GraphqlAttributeSchema\Test\Doubles\Enum\TestAnotherEnumType;
use Jerowork\GraphqlAttributeSchema\Test\Doubles\Enum\TestEnumType;
Expand All @@ -35,7 +34,7 @@ protected function setUp(): void

$this->ast = new Ast(
$this->enumNode1 = new EnumNode(
Type::createObject(TestEnumType::class),
TestEnumType::class,
'enum1',
null,
[
Expand All @@ -44,13 +43,13 @@ protected function setUp(): void
],
),
$this->typeNode = new TypeNode(
Type::createObject(TestType::class),
TestType::class,
'type',
null,
[],
),
$this->enumNode2 =new EnumNode(
Type::createObject(TestAnotherEnumType::class),
TestAnotherEnumType::class,
'enum2',
null,
[
Expand All @@ -59,7 +58,7 @@ protected function setUp(): void
],
),
new InputTypeNode(
Type::createObject(TestInputType::class),
TestInputType::class,
'inputType',
null,
[],
Expand All @@ -77,7 +76,7 @@ public function itShouldGetNodesByType(): void
#[Test]
public function itShouldGetNodeByTypeId(): void
{
self::assertSame($this->typeNode, $this->ast->getNodeByType(Type::createObject(TestType::class)));
self::assertSame($this->enumNode2, $this->ast->getNodeByType(Type::createObject(TestAnotherEnumType::class)));
self::assertSame($this->typeNode, $this->ast->getNodeByClassName(TestType::class));
self::assertSame($this->enumNode2, $this->ast->getNodeByClassName(TestAnotherEnumType::class));
}
}
Loading

0 comments on commit 5860c16

Please sign in to comment.