Skip to content

Commit

Permalink
Introduce Option\Type to define scalar types in options
Browse files Browse the repository at this point in the history
  • Loading branch information
jerowork committed Jan 3, 2025
1 parent 930b372 commit 86c3d47
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 17 deletions.
8 changes: 6 additions & 2 deletions src/Attribute/Arg.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
namespace Jerowork\GraphqlAttributeSchema\Attribute;

use Attribute;
use Jerowork\GraphqlAttributeSchema\Attribute\Option\ScalarType;

#[Attribute(Attribute::TARGET_PARAMETER)]
final readonly class Arg implements BaseAttribute, TypedAttribute
{
/**
* @param class-string|ScalarType|null $type
*/
public function __construct(
public ?string $name = null,
public ?string $description = null,
public ?string $type = null,
public string|ScalarType|null $type = null,
public bool $isRequired = true,
) {}

Expand All @@ -26,7 +30,7 @@ public function getDescription(): ?string
return $this->description;
}

public function getType(): ?string
public function getType(): string|ScalarType|null
{
return $this->type;
}
Expand Down
8 changes: 6 additions & 2 deletions src/Attribute/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
namespace Jerowork\GraphqlAttributeSchema\Attribute;

use Attribute;
use Jerowork\GraphqlAttributeSchema\Attribute\Option\ScalarType;

#[Attribute(Attribute::TARGET_PROPERTY|Attribute::TARGET_METHOD)]
final readonly class Field implements BaseAttribute, TypedAttribute
{
/**
* @param class-string|ScalarType|null $type
*/
public function __construct(
public ?string $name = null,
public ?string $description = null,
public ?string $type = null,
public string|ScalarType|null $type = null,
public bool $isRequired = true,
) {}

Expand All @@ -26,7 +30,7 @@ public function getDescription(): ?string
return $this->description;
}

public function getType(): ?string
public function getType(): string|ScalarType|null
{
return $this->type;
}
Expand Down
8 changes: 6 additions & 2 deletions src/Attribute/Mutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
namespace Jerowork\GraphqlAttributeSchema\Attribute;

use Attribute;
use Jerowork\GraphqlAttributeSchema\Attribute\Option\ScalarType;

#[Attribute(Attribute::TARGET_CLASS)]
final readonly class Mutation implements BaseAttribute, TypedAttribute
{
/**
* @param class-string|ScalarType|null $type
*/
public function __construct(
public ?string $name = null,
public ?string $description = null,
public ?string $type = null,
public string|ScalarType|null $type = null,
public bool $isRequired = true,
) {}

Expand All @@ -26,7 +30,7 @@ public function getDescription(): ?string
return $this->description;
}

public function getType(): ?string
public function getType(): string|ScalarType|null
{
return $this->type;
}
Expand Down
13 changes: 13 additions & 0 deletions src/Attribute/Option/ScalarType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Jerowork\GraphqlAttributeSchema\Attribute\Option;

enum ScalarType: string
{
case String = 'string';
case Int = 'int';
case Float = 'float';
case Bool = 'bool';
}
8 changes: 6 additions & 2 deletions src/Attribute/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
namespace Jerowork\GraphqlAttributeSchema\Attribute;

use Attribute;
use Jerowork\GraphqlAttributeSchema\Attribute\Option\ScalarType;

#[Attribute(Attribute::TARGET_CLASS)]
final readonly class Query implements BaseAttribute, TypedAttribute
{
/**
* @param class-string|ScalarType|null $type
*/
public function __construct(
public ?string $name = null,
public ?string $description = null,
public ?string $type = null,
public string|ScalarType|null $type = null,
public bool $isRequired = true,
) {}

Expand All @@ -26,7 +30,7 @@ public function getDescription(): ?string
return $this->description;
}

public function getType(): ?string
public function getType(): string|ScalarType|null
{
return $this->type;
}
Expand Down
7 changes: 6 additions & 1 deletion src/Attribute/TypedAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@

namespace Jerowork\GraphqlAttributeSchema\Attribute;

use Jerowork\GraphqlAttributeSchema\Attribute\Option\ScalarType;

interface TypedAttribute
{
public function getType(): ?string;
/**
* @return class-string|ScalarType|null
*/
public function getType(): string|ScalarType|null;

public function isRequired(): bool;
}
13 changes: 8 additions & 5 deletions src/Parser/NodeParser/GetTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@

namespace Jerowork\GraphqlAttributeSchema\Parser\NodeParser;

use Jerowork\GraphqlAttributeSchema\Attribute\Option\ScalarType;
use Jerowork\GraphqlAttributeSchema\Attribute\TypedAttribute;
use Jerowork\GraphqlAttributeSchema\Parser\Node\Type;
use ReflectionNamedType;

trait GetTypeTrait
{
private const array SCALAR_TYPES = ['string', 'int', 'float', 'bool'];

public function getType(ReflectionNamedType $type, ?TypedAttribute $attribute): Type
{
// Retrieve from attribute if set
if ($attribute?->getType() !== null) {
return in_array($attribute->getType(), self::SCALAR_TYPES, true) ?
Type::createScalar($attribute->getType()) :
Type::createObject($attribute->getType());
$attributeType = $attribute->getType();

if ($attributeType instanceof ScalarType) {
return Type::createScalar($attributeType->value);
}

return Type::createObject($attributeType);
}

// Retrieve from class
Expand Down
3 changes: 2 additions & 1 deletion tests/Parser/NodeParser/GetTypeTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Jerowork\GraphqlAttributeSchema\Test\Parser\NodeParser;

use Jerowork\GraphqlAttributeSchema\Attribute\Mutation;
use Jerowork\GraphqlAttributeSchema\Attribute\Option\ScalarType;
use Jerowork\GraphqlAttributeSchema\Parser\Node\Type;
use Jerowork\GraphqlAttributeSchema\Parser\NodeParser\GetTypeTrait;
use Jerowork\GraphqlAttributeSchema\Test\Doubles\Mutation\TestMutation;
Expand Down Expand Up @@ -65,7 +66,7 @@ public function itShouldReturnScalarFromAttribute(): void
$type = $parameters[1]->getType();

self::assertInstanceOf(ReflectionNamedType::class, $type);
self::assertTrue($trait->getType($type, new Mutation(type: 'int'))->equals(Type::createScalar('int')));
self::assertTrue($trait->getType($type, new Mutation(type: ScalarType::Int))->equals(Type::createScalar('int')));
}

#[Test]
Expand Down
5 changes: 3 additions & 2 deletions tests/Parser/NodeParser/IsRequiredTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Jerowork\GraphqlAttributeSchema\Test\Parser\NodeParser;

use Jerowork\GraphqlAttributeSchema\Attribute\Mutation;
use Jerowork\GraphqlAttributeSchema\Attribute\Option\ScalarType;
use Jerowork\GraphqlAttributeSchema\Parser\NodeParser\IsRequiredTrait;
use Jerowork\GraphqlAttributeSchema\Test\Doubles\Mutation\TestMutation;
use PHPUnit\Framework\Attributes\Test;
Expand Down Expand Up @@ -50,9 +51,9 @@ public function itShouldReturnIfRequiredFromType(): void
$type2 = $parameters[1]->getType();

self::assertInstanceOf(ReflectionNamedType::class, $type1);
self::assertFalse($trait->isRequired($type1, new Mutation(type: 'int', isRequired: false)));
self::assertFalse($trait->isRequired($type1, new Mutation(type: ScalarType::Int, isRequired: false)));
self::assertInstanceOf(ReflectionNamedType::class, $type2);
self::assertTrue($trait->isRequired($type2, new Mutation(type: 'int', isRequired: true)));
self::assertTrue($trait->isRequired($type2, new Mutation(type: ScalarType::Int, isRequired: true)));
}

#[Test]
Expand Down

0 comments on commit 86c3d47

Please sign in to comment.