Skip to content

Commit 86c3d47

Browse files
committed
Introduce Option\Type to define scalar types in options
1 parent 930b372 commit 86c3d47

File tree

9 files changed

+56
-17
lines changed

9 files changed

+56
-17
lines changed

src/Attribute/Arg.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55
namespace Jerowork\GraphqlAttributeSchema\Attribute;
66

77
use Attribute;
8+
use Jerowork\GraphqlAttributeSchema\Attribute\Option\ScalarType;
89

910
#[Attribute(Attribute::TARGET_PARAMETER)]
1011
final readonly class Arg implements BaseAttribute, TypedAttribute
1112
{
13+
/**
14+
* @param class-string|ScalarType|null $type
15+
*/
1216
public function __construct(
1317
public ?string $name = null,
1418
public ?string $description = null,
15-
public ?string $type = null,
19+
public string|ScalarType|null $type = null,
1620
public bool $isRequired = true,
1721
) {}
1822

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

29-
public function getType(): ?string
33+
public function getType(): string|ScalarType|null
3034
{
3135
return $this->type;
3236
}

src/Attribute/Field.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55
namespace Jerowork\GraphqlAttributeSchema\Attribute;
66

77
use Attribute;
8+
use Jerowork\GraphqlAttributeSchema\Attribute\Option\ScalarType;
89

910
#[Attribute(Attribute::TARGET_PROPERTY|Attribute::TARGET_METHOD)]
1011
final readonly class Field implements BaseAttribute, TypedAttribute
1112
{
13+
/**
14+
* @param class-string|ScalarType|null $type
15+
*/
1216
public function __construct(
1317
public ?string $name = null,
1418
public ?string $description = null,
15-
public ?string $type = null,
19+
public string|ScalarType|null $type = null,
1620
public bool $isRequired = true,
1721
) {}
1822

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

29-
public function getType(): ?string
33+
public function getType(): string|ScalarType|null
3034
{
3135
return $this->type;
3236
}

src/Attribute/Mutation.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55
namespace Jerowork\GraphqlAttributeSchema\Attribute;
66

77
use Attribute;
8+
use Jerowork\GraphqlAttributeSchema\Attribute\Option\ScalarType;
89

910
#[Attribute(Attribute::TARGET_CLASS)]
1011
final readonly class Mutation implements BaseAttribute, TypedAttribute
1112
{
13+
/**
14+
* @param class-string|ScalarType|null $type
15+
*/
1216
public function __construct(
1317
public ?string $name = null,
1418
public ?string $description = null,
15-
public ?string $type = null,
19+
public string|ScalarType|null $type = null,
1620
public bool $isRequired = true,
1721
) {}
1822

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

29-
public function getType(): ?string
33+
public function getType(): string|ScalarType|null
3034
{
3135
return $this->type;
3236
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jerowork\GraphqlAttributeSchema\Attribute\Option;
6+
7+
enum ScalarType: string
8+
{
9+
case String = 'string';
10+
case Int = 'int';
11+
case Float = 'float';
12+
case Bool = 'bool';
13+
}

src/Attribute/Query.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55
namespace Jerowork\GraphqlAttributeSchema\Attribute;
66

77
use Attribute;
8+
use Jerowork\GraphqlAttributeSchema\Attribute\Option\ScalarType;
89

910
#[Attribute(Attribute::TARGET_CLASS)]
1011
final readonly class Query implements BaseAttribute, TypedAttribute
1112
{
13+
/**
14+
* @param class-string|ScalarType|null $type
15+
*/
1216
public function __construct(
1317
public ?string $name = null,
1418
public ?string $description = null,
15-
public ?string $type = null,
19+
public string|ScalarType|null $type = null,
1620
public bool $isRequired = true,
1721
) {}
1822

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

29-
public function getType(): ?string
33+
public function getType(): string|ScalarType|null
3034
{
3135
return $this->type;
3236
}

src/Attribute/TypedAttribute.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44

55
namespace Jerowork\GraphqlAttributeSchema\Attribute;
66

7+
use Jerowork\GraphqlAttributeSchema\Attribute\Option\ScalarType;
8+
79
interface TypedAttribute
810
{
9-
public function getType(): ?string;
11+
/**
12+
* @return class-string|ScalarType|null
13+
*/
14+
public function getType(): string|ScalarType|null;
1015

1116
public function isRequired(): bool;
1217
}

src/Parser/NodeParser/GetTypeTrait.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,24 @@
44

55
namespace Jerowork\GraphqlAttributeSchema\Parser\NodeParser;
66

7+
use Jerowork\GraphqlAttributeSchema\Attribute\Option\ScalarType;
78
use Jerowork\GraphqlAttributeSchema\Attribute\TypedAttribute;
89
use Jerowork\GraphqlAttributeSchema\Parser\Node\Type;
910
use ReflectionNamedType;
1011

1112
trait GetTypeTrait
1213
{
13-
private const array SCALAR_TYPES = ['string', 'int', 'float', 'bool'];
14-
1514
public function getType(ReflectionNamedType $type, ?TypedAttribute $attribute): Type
1615
{
1716
// Retrieve from attribute if set
1817
if ($attribute?->getType() !== null) {
19-
return in_array($attribute->getType(), self::SCALAR_TYPES, true) ?
20-
Type::createScalar($attribute->getType()) :
21-
Type::createObject($attribute->getType());
18+
$attributeType = $attribute->getType();
19+
20+
if ($attributeType instanceof ScalarType) {
21+
return Type::createScalar($attributeType->value);
22+
}
23+
24+
return Type::createObject($attributeType);
2225
}
2326

2427
// Retrieve from class

tests/Parser/NodeParser/GetTypeTraitTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Jerowork\GraphqlAttributeSchema\Test\Parser\NodeParser;
66

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

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

7172
#[Test]

tests/Parser/NodeParser/IsRequiredTraitTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Jerowork\GraphqlAttributeSchema\Test\Parser\NodeParser;
66

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

5253
self::assertInstanceOf(ReflectionNamedType::class, $type1);
53-
self::assertFalse($trait->isRequired($type1, new Mutation(type: 'int', isRequired: false)));
54+
self::assertFalse($trait->isRequired($type1, new Mutation(type: ScalarType::Int, isRequired: false)));
5455
self::assertInstanceOf(ReflectionNamedType::class, $type2);
55-
self::assertTrue($trait->isRequired($type2, new Mutation(type: 'int', isRequired: true)));
56+
self::assertTrue($trait->isRequired($type2, new Mutation(type: ScalarType::Int, isRequired: true)));
5657
}
5758

5859
#[Test]

0 commit comments

Comments
 (0)