Skip to content

Commit 9ac9c77

Browse files
committed
Differentiate NodeType by class instead of constant
Which makes it possible to allow more custom parameters per type. (e.g. Connections)
1 parent 9c3c1e9 commit 9ac9c77

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+714
-427
lines changed

src/Parser/Ast.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public function getNodesByNodeType(string $nodeType): array
4040
return array_values(array_filter($this->nodes, fn($node) => $node instanceof $nodeType));
4141
}
4242

43+
/**
44+
* @param class-string $className
45+
*/
4346
public function getNodeByClassName(string $className): ?Node
4447
{
4548
foreach ($this->nodes as $node) {

src/Parser/Node/Child/ArgNode.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
namespace Jerowork\GraphqlAttributeSchema\Parser\Node\Child;
66

77
use Jerowork\GraphqlAttributeSchema\Parser\Node\ArraySerializable;
8-
use Jerowork\GraphqlAttributeSchema\Parser\Node\Type;
8+
use Jerowork\GraphqlAttributeSchema\Parser\Node\Type\NodeType;
99

1010
/**
11-
* @phpstan-import-type TypePayload from Type
12-
*
1311
* @phpstan-type ArgNodePayload array{
14-
* type: TypePayload,
12+
* type: array{
13+
* type: class-string<NodeType>,
14+
* payload: array<string, mixed>
15+
* },
1516
* name: string,
1617
* description: null|string,
1718
* propertyName: string
@@ -22,16 +23,20 @@
2223
final readonly class ArgNode implements ArraySerializable
2324
{
2425
public function __construct(
25-
public Type $type,
26+
public NodeType $type,
2627
public string $name,
2728
public ?string $description,
2829
public string $propertyName,
2930
) {}
3031

3132
public function toArray(): array
3233
{
34+
// @phpstan-ignore-next-line
3335
return [
34-
'type' => $this->type->toArray(),
36+
'type' => [
37+
'type' => $this->type::class,
38+
'payload' => $this->type->toArray(),
39+
],
3540
'name' => $this->name,
3641
'description' => $this->description,
3742
'propertyName' => $this->propertyName,
@@ -40,8 +45,11 @@ public function toArray(): array
4045

4146
public static function fromArray(array $payload): ArgNode
4247
{
48+
/** @var class-string<NodeType> $type */
49+
$type = $payload['type']['type'];
50+
4351
return new self(
44-
Type::fromArray($payload['type']),
52+
$type::fromArray($payload['type']['payload']), // @phpstan-ignore-line
4553
$payload['name'],
4654
$payload['description'],
4755
$payload['propertyName'],

src/Parser/Node/Child/FieldNode.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55
namespace Jerowork\GraphqlAttributeSchema\Parser\Node\Child;
66

77
use Jerowork\GraphqlAttributeSchema\Parser\Node\ArraySerializable;
8-
use Jerowork\GraphqlAttributeSchema\Parser\Node\Type;
8+
use Jerowork\GraphqlAttributeSchema\Parser\Node\Type\NodeType;
99

1010
/**
1111
* @phpstan-import-type ArgNodePayload from ArgNode
1212
* @phpstan-import-type AutowireNodePayload from AutowireNode
13-
* @phpstan-import-type TypePayload from Type
1413
*
1514
* @phpstan-type FieldNodePayload array{
16-
* type: TypePayload,
15+
* type: array{
16+
* type: class-string<NodeType>,
17+
* payload: array<string, mixed>
18+
* },
1719
* name: string,
1820
* description: null|string,
1921
* argumentNodes: list<array{
@@ -34,7 +36,7 @@
3436
* @param list<ArgNode|AutowireNode> $argumentNodes
3537
*/
3638
public function __construct(
37-
public Type $type,
39+
public NodeType $type,
3840
public string $name,
3941
public ?string $description,
4042
public array $argumentNodes,
@@ -54,8 +56,12 @@ public function toArray(): array
5456
];
5557
}
5658

59+
// @phpstan-ignore-next-line
5760
return [
58-
'type' => $this->type->toArray(),
61+
'type' => [
62+
'type' => $this->type::class,
63+
'payload' => $this->type->toArray(),
64+
],
5965
'name' => $this->name,
6066
'description' => $this->description,
6167
'argumentNodes' => $argumentNodes,
@@ -80,8 +86,11 @@ public static function fromArray(array $payload): FieldNode
8086
}
8187
}
8288

89+
/** @var class-string<NodeType> $type */
90+
$type = $payload['type']['type'];
91+
8392
return new self(
84-
Type::fromArray($payload['type']),
93+
$type::fromArray($payload['type']['payload']), // @phpstan-ignore-line
8594
$payload['name'],
8695
$payload['description'],
8796
$argumentNodes,

src/Parser/Node/Class/EnumNode.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
namespace Jerowork\GraphqlAttributeSchema\Parser\Node\Class;
66

77
use Jerowork\GraphqlAttributeSchema\Parser\Node\Node;
8+
use BackedEnum;
89

910
/**
1011
* @phpstan-import-type EnumValueNodePayload from EnumValueNode
1112
*
1213
* @phpstan-type EnumNodePayload array{
13-
* className: class-string,
14+
* className: class-string<BackedEnum>,
1415
* name: string,
1516
* description: null|string,
1617
* cases: list<EnumValueNodePayload>
@@ -19,7 +20,7 @@
1920
final readonly class EnumNode implements Node
2021
{
2122
/**
22-
* @param class-string $className
23+
* @param class-string<BackedEnum> $className
2324
* @param list<EnumValueNode> $cases
2425
*/
2526
public function __construct(
@@ -29,6 +30,9 @@ public function __construct(
2930
public array $cases,
3031
) {}
3132

33+
/**
34+
* @return class-string<BackedEnum>
35+
*/
3236
public function getClassName(): string
3337
{
3438
return $this->className;

src/Parser/Node/Method/MutationNode.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@
66

77
use Jerowork\GraphqlAttributeSchema\Parser\Node\Child\ArgNode;
88
use Jerowork\GraphqlAttributeSchema\Parser\Node\Node;
9-
use Jerowork\GraphqlAttributeSchema\Parser\Node\Type;
9+
use Jerowork\GraphqlAttributeSchema\Parser\Node\Type\NodeType;
1010

1111
/**
1212
* @phpstan-import-type ArgNodePayload from ArgNode
13-
* @phpstan-import-type TypePayload from Type
1413
*
1514
* @phpstan-type MutationNodePayload array{
1615
* className: class-string,
1716
* name: string,
1817
* description: null|string,
1918
* argNodes: list<ArgNodePayload>,
20-
* outputType: TypePayload,
19+
* outputType: array{
20+
* type: class-string,
21+
* payload: array<string, mixed>
22+
* },
2123
* methodName: string,
2224
* deprecationReason: null|string
2325
* }
@@ -33,7 +35,7 @@ public function __construct(
3335
public string $name,
3436
public ?string $description,
3537
public array $argNodes,
36-
public Type $outputType,
38+
public NodeType $outputType,
3739
public string $methodName,
3840
public ?string $deprecationReason,
3941
) {}
@@ -48,12 +50,16 @@ public function getClassName(): string
4850
*/
4951
public function toArray(): array
5052
{
53+
// @phpstan-ignore-next-line
5154
return [
5255
'className' => $this->className,
5356
'name' => $this->name,
5457
'description' => $this->description,
5558
'argNodes' => array_map(fn($argNode) => $argNode->toArray(), $this->argNodes),
56-
'outputType' => $this->outputType->toArray(),
59+
'outputType' => [
60+
'type' => $this->outputType::class,
61+
'payload' => $this->outputType->toArray(),
62+
],
5763
'methodName' => $this->methodName,
5864
'deprecationReason' => $this->deprecationReason,
5965
];
@@ -64,12 +70,15 @@ public function toArray(): array
6470
*/
6571
public static function fromArray(array $payload): MutationNode
6672
{
73+
/** @var class-string<NodeType> $type */
74+
$type = $payload['outputType']['type'];
75+
6776
return new self(
6877
$payload['className'],
6978
$payload['name'],
7079
$payload['description'],
7180
array_map(fn($argNodePayload) => ArgNode::fromArray($argNodePayload), $payload['argNodes']),
72-
Type::fromArray($payload['outputType']),
81+
$type::fromArray($payload['outputType']['payload']), // @phpstan-ignore-line
7382
$payload['methodName'],
7483
$payload['deprecationReason'],
7584
);

src/Parser/Node/Method/QueryNode.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@
66

77
use Jerowork\GraphqlAttributeSchema\Parser\Node\Child\ArgNode;
88
use Jerowork\GraphqlAttributeSchema\Parser\Node\Node;
9-
use Jerowork\GraphqlAttributeSchema\Parser\Node\Type;
9+
use Jerowork\GraphqlAttributeSchema\Parser\Node\Type\NodeType;
1010

1111
/**
1212
* @phpstan-import-type ArgNodePayload from ArgNode
13-
* @phpstan-import-type TypePayload from Type
1413
*
1514
* @phpstan-type QueryNodePayload array{
1615
* className: class-string,
1716
* name: string,
1817
* description: null|string,
1918
* argNodes: list<ArgNodePayload>,
20-
* outputType: TypePayload,
19+
* outputType: array{
20+
* type: class-string,
21+
* payload: array<string, mixed>
22+
* },
2123
* methodName: string,
2224
* deprecationReason: null|string
2325
* }
@@ -33,7 +35,7 @@ public function __construct(
3335
public string $name,
3436
public ?string $description,
3537
public array $argNodes,
36-
public Type $outputType,
38+
public NodeType $outputType,
3739
public string $methodName,
3840
public ?string $deprecationReason,
3941
) {}
@@ -48,12 +50,16 @@ public function getClassName(): string
4850
*/
4951
public function toArray(): array
5052
{
53+
// @phpstan-ignore-next-line
5154
return [
5255
'className' => $this->className,
5356
'name' => $this->name,
5457
'description' => $this->description,
5558
'argNodes' => array_map(fn($argNode) => $argNode->toArray(), $this->argNodes),
56-
'outputType' => $this->outputType->toArray(),
59+
'outputType' => [
60+
'type' => $this->outputType::class,
61+
'payload' => $this->outputType->toArray(),
62+
],
5763
'methodName' => $this->methodName,
5864
'deprecationReason' => $this->deprecationReason,
5965
];
@@ -64,12 +70,15 @@ public function toArray(): array
6470
*/
6571
public static function fromArray(array $payload): QueryNode
6672
{
73+
/** @var class-string<NodeType> $type */
74+
$type = $payload['outputType']['type'];
75+
6776
return new self(
6877
$payload['className'],
6978
$payload['name'],
7079
$payload['description'],
7180
array_map(fn($argNodePayload) => ArgNode::fromArray($argNodePayload), $payload['argNodes']),
72-
Type::fromArray($payload['outputType']),
81+
$type::fromArray($payload['outputType']['payload']), // @phpstan-ignore-line
7382
$payload['methodName'],
7483
$payload['deprecationReason'],
7584
);

0 commit comments

Comments
 (0)