Skip to content

Commit 930b372

Browse files
committed
Add isRequired to TypedAttributes
When type is set in an attribute, it will overrule both type as well as if it is required. IsRequired can be set with the new option `isRequired`
1 parent fb223ff commit 930b372

File tree

11 files changed

+135
-5
lines changed

11 files changed

+135
-5
lines changed

src/Attribute/Arg.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public function __construct(
1313
public ?string $name = null,
1414
public ?string $description = null,
1515
public ?string $type = null,
16+
public bool $isRequired = true,
1617
) {}
1718

1819
public function getName(): ?string
@@ -29,4 +30,9 @@ public function getType(): ?string
2930
{
3031
return $this->type;
3132
}
33+
34+
public function isRequired(): bool
35+
{
36+
return $this->isRequired;
37+
}
3238
}

src/Attribute/Field.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public function __construct(
1313
public ?string $name = null,
1414
public ?string $description = null,
1515
public ?string $type = null,
16+
public bool $isRequired = true,
1617
) {}
1718

1819
public function getName(): ?string
@@ -29,4 +30,9 @@ public function getType(): ?string
2930
{
3031
return $this->type;
3132
}
33+
34+
public function isRequired(): bool
35+
{
36+
return $this->isRequired;
37+
}
3238
}

src/Attribute/Mutation.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public function __construct(
1313
public ?string $name = null,
1414
public ?string $description = null,
1515
public ?string $type = null,
16+
public bool $isRequired = true,
1617
) {}
1718

1819
public function getName(): ?string
@@ -29,4 +30,9 @@ public function getType(): ?string
2930
{
3031
return $this->type;
3132
}
33+
34+
public function isRequired(): bool
35+
{
36+
return $this->isRequired;
37+
}
3238
}

src/Attribute/Query.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public function __construct(
1313
public ?string $name = null,
1414
public ?string $description = null,
1515
public ?string $type = null,
16+
public bool $isRequired = true,
1617
) {}
1718

1819
public function getName(): ?string
@@ -29,4 +30,9 @@ public function getType(): ?string
2930
{
3031
return $this->type;
3132
}
33+
34+
public function isRequired(): bool
35+
{
36+
return $this->isRequired;
37+
}
3238
}

src/Attribute/TypedAttribute.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77
interface TypedAttribute
88
{
99
public function getType(): ?string;
10+
11+
public function isRequired(): bool;
1012
}

src/Parser/NodeParser/Child/ClassFieldNodesParser.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Jerowork\GraphqlAttributeSchema\Parser\Node\FieldNode;
99
use Jerowork\GraphqlAttributeSchema\Parser\Node\FieldNodeType;
1010
use Jerowork\GraphqlAttributeSchema\Parser\NodeParser\GetTypeTrait;
11+
use Jerowork\GraphqlAttributeSchema\Parser\NodeParser\IsRequiredTrait;
1112
use Jerowork\GraphqlAttributeSchema\Parser\NodeParser\ParseException;
1213
use Jerowork\GraphqlAttributeSchema\Parser\NodeParser\RetrieveNameForFieldTrait;
1314
use ReflectionClass;
@@ -19,6 +20,7 @@
1920
{
2021
use RetrieveNameForFieldTrait;
2122
use GetTypeTrait;
23+
use IsRequiredTrait;
2224

2325
private const array RESERVED_METHOD_NAMES = ['__construct'];
2426
private const string RETURN_TYPE_VOID = 'void';
@@ -53,7 +55,7 @@ public function parse(ReflectionClass $class): array
5355
$this->getType($propertyType, $fieldAttribute),
5456
$fieldAttribute->name ?? $property->getName(),
5557
$fieldAttribute->description,
56-
!$propertyType->allowsNull(),
58+
$this->isRequired($propertyType, $fieldAttribute),
5759
[],
5860
FieldNodeType::Property,
5961
null,
@@ -80,7 +82,7 @@ public function parse(ReflectionClass $class): array
8082
$this->getType($returnType, $fieldAttribute),
8183
$this->retrieveNameForField($method, $fieldAttribute),
8284
$fieldAttribute->description,
83-
!$returnType->allowsNull(),
85+
$this->isRequired($returnType, $fieldAttribute),
8486
$this->methodArgNodesParser->parse($method),
8587
FieldNodeType::Method,
8688
$method->getName(),

src/Parser/NodeParser/Child/MethodArgNodesParser.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Jerowork\GraphqlAttributeSchema\Attribute\Arg;
88
use Jerowork\GraphqlAttributeSchema\Parser\Node\ArgNode;
99
use Jerowork\GraphqlAttributeSchema\Parser\NodeParser\GetTypeTrait;
10+
use Jerowork\GraphqlAttributeSchema\Parser\NodeParser\IsRequiredTrait;
1011
use Jerowork\GraphqlAttributeSchema\Parser\NodeParser\ParseException;
1112
use ReflectionMethod;
1213
use ReflectionParameter;
@@ -15,6 +16,7 @@
1516
final readonly class MethodArgNodesParser
1617
{
1718
use GetTypeTrait;
19+
use IsRequiredTrait;
1820

1921
/**
2022
* @throws ParseException
@@ -37,7 +39,7 @@ public function parse(ReflectionMethod $method): array
3739
$this->getType($parameterType, $argAttribute),
3840
$argAttribute->name ?? $parameter->getName(),
3941
$argAttribute?->description,
40-
!$parameterType->allowsNull(),
42+
$this->isRequired($parameterType, $argAttribute),
4143
$parameter->getName(),
4244
);
4345
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jerowork\GraphqlAttributeSchema\Parser\NodeParser;
6+
7+
use Jerowork\GraphqlAttributeSchema\Attribute\TypedAttribute;
8+
use ReflectionNamedType;
9+
10+
trait IsRequiredTrait
11+
{
12+
public function isRequired(ReflectionNamedType $type, ?TypedAttribute $attribute): bool
13+
{
14+
// Retrieve from attribute if set
15+
if ($attribute?->getType() !== null) {
16+
return $attribute->isRequired();
17+
}
18+
19+
// Retrieve from class
20+
return !$type->allowsNull();
21+
}
22+
}

src/Parser/NodeParser/MutationNodeParser.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use RetrieveNameForResolverTrait;
1919
use GetMethodFromClassTrait;
2020
use GetTypeTrait;
21+
use IsRequiredTrait;
2122
use GetClassAttributeTrait;
2223

2324
private const string RESOLVER_SUFFIX = 'Mutation';
@@ -55,7 +56,7 @@ public function parse(ReflectionClass $class): Node
5556
$attribute->getDescription(),
5657
$this->methodArgNodesParser->parse($method),
5758
$this->getType($returnType, $attribute),
58-
!$returnType->allowsNull(),
59+
$this->isRequired($returnType, $attribute),
5960
$method->getName(),
6061
);
6162
}

src/Parser/NodeParser/QueryNodeParser.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use RetrieveNameForResolverTrait;
1919
use GetMethodFromClassTrait;
2020
use GetTypeTrait;
21+
use IsRequiredTrait;
2122
use GetClassAttributeTrait;
2223

2324
private const string RESOLVER_SUFFIX = 'Query';
@@ -55,7 +56,7 @@ public function parse(ReflectionClass $class): Node
5556
$attribute->getDescription(),
5657
$this->methodArgNodesParser->parse($method),
5758
$this->getType($returnType, $attribute),
58-
!$returnType->allowsNull(),
59+
$this->isRequired($returnType, $attribute),
5960
$method->getName(),
6061
);
6162
}

0 commit comments

Comments
 (0)