Skip to content

Commit b343f4b

Browse files
committed
Keep possible types sorted
1 parent 9af8807 commit b343f4b

File tree

7 files changed

+66
-71
lines changed

7 files changed

+66
-71
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Ruudk\GraphQLCodeGenerator\GraphQL;
6+
7+
use GraphQL\Error\InvariantViolation;
8+
use GraphQL\Type\Definition\InterfaceType;
9+
use GraphQL\Type\Definition\NonNull;
10+
use GraphQL\Type\Definition\Type;
11+
use GraphQL\Type\Definition\UnionType;
12+
use GraphQL\Type\Schema;
13+
14+
final readonly class PossibleTypesFinder
15+
{
16+
public function __construct(
17+
private Schema $schema,
18+
) {}
19+
20+
/**
21+
* @throws InvariantViolation
22+
* @return list<string>
23+
*/
24+
public function find(Type $type) : array
25+
{
26+
if ($type instanceof NonNull) {
27+
$type = $type->getWrappedType();
28+
}
29+
30+
if ($type instanceof UnionType) {
31+
$possible = [];
32+
foreach ($type->getTypes() as $possibleType) {
33+
$possible[] = $possibleType->name;
34+
}
35+
36+
sort($possible);
37+
38+
return $possible;
39+
}
40+
41+
if ($type instanceof InterfaceType) {
42+
$possible = [];
43+
foreach ($this->schema->getImplementations($type)->objects() as $possibleType) {
44+
$possible[] = $possibleType->name;
45+
}
46+
47+
sort($possible);
48+
49+
return $possible;
50+
}
51+
52+
return [];
53+
}
54+
}

src/Planner.php

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
use GraphQL\Language\Printer;
1414
use GraphQL\Type\Definition\EnumType;
1515
use GraphQL\Type\Definition\InputObjectType;
16-
use GraphQL\Type\Definition\InterfaceType;
17-
use GraphQL\Type\Definition\NonNull;
1816
use GraphQL\Type\Definition\Type;
19-
use GraphQL\Type\Definition\UnionType;
2017
use GraphQL\Type\Schema;
2118
use GraphQL\Validator\DocumentValidator;
2219
use GraphQL\Validator\Rules\ExecutableDefinitions;
@@ -56,6 +53,7 @@
5653
use GraphQL\Validator\Rules\VariablesAreInputTypes;
5754
use GraphQL\Validator\Rules\VariablesInAllowedPosition;
5855
use JsonException;
56+
use Ruudk\GraphQLCodeGenerator\GraphQL\PossibleTypesFinder;
5957
use Ruudk\GraphQLCodeGenerator\Planner\OperationPlan;
6058
use Ruudk\GraphQLCodeGenerator\Planner\Plan\DataClassPlan;
6159
use Ruudk\GraphQLCodeGenerator\Planner\Plan\EnumClassPlan;
@@ -110,6 +108,7 @@ final class Planner
110108
private TypeMapper $typeMapper;
111109
private DirectiveProcessor $directiveProcessor;
112110
private VariableParser $variableParser;
111+
private PossibleTypesFinder $possibleTypesFinder;
113112

114113
/**
115114
* @throws \GraphQL\Error\Error
@@ -137,6 +136,7 @@ public function __construct(
137136
$this->schemaLoader = new SchemaLoader(new Filesystem());
138137
$this->schema = $this->schemaLoader->load($config->schema, $config->indexByDirective);
139138
$this->optimizer = new Optimizer($this->schema);
139+
$this->possibleTypesFinder = new PossibleTypesFinder($this->schema);
140140

141141
$this->validatorRules = [
142142
ExecutableDefinitions::class => new ExecutableDefinitions(),
@@ -396,7 +396,7 @@ public function plan() : PlannerResult
396396
parentType: $type,
397397
fields: $planResult->fields,
398398
payloadShape: $planResult->payloadShape,
399-
possibleTypes: $this->getPossibleTypes($type),
399+
possibleTypes: $this->possibleTypesFinder->find($type),
400400
definitionNode: $fragment,
401401
nodesType: null,
402402
inlineFragmentRequiredFields: $planResult->inlineFragmentRequiredFields,
@@ -573,35 +573,4 @@ private function fullyQualified(string $part, string ...$moreParts) : string
573573

574574
return implode('\\', array_filter([$this->config->namespace, $part, ...$moreParts], fn($part) => $part !== ''));
575575
}
576-
577-
/**
578-
* @throws InvariantViolation
579-
* @return list<string>
580-
*/
581-
private function getPossibleTypes(Type $type) : array
582-
{
583-
if ($type instanceof NonNull) {
584-
$type = $type->getWrappedType();
585-
}
586-
587-
if ($type instanceof UnionType) {
588-
$possible = [];
589-
foreach ($type->getTypes() as $possibleType) {
590-
$possible[] = $possibleType->name;
591-
}
592-
593-
return $possible;
594-
}
595-
596-
if ($type instanceof InterfaceType) {
597-
$possible = [];
598-
foreach ($this->schema->getImplementations($type)->objects() as $possibleType) {
599-
$possible[] = $possibleType->name;
600-
}
601-
602-
return $possible;
603-
}
604-
605-
return [];
606-
}
607576
}

src/Planner/SelectionSetPlanner.php

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use LogicException;
2929
use Ruudk\GraphQLCodeGenerator\Config;
3030
use Ruudk\GraphQLCodeGenerator\DirectiveProcessor;
31+
use Ruudk\GraphQLCodeGenerator\GraphQL\PossibleTypesFinder;
3132
use Ruudk\GraphQLCodeGenerator\Planner\Plan\DataClassPlan;
3233
use Ruudk\GraphQLCodeGenerator\RecursiveTypeFinder;
3334
use Ruudk\GraphQLCodeGenerator\Type\FragmentObjectType;
@@ -70,6 +71,7 @@ final class SelectionSetPlanner
7071
* @var array<string, FragmentDefinitionNode> Fragment name to definition mapping
7172
*/
7273
public private(set) array $fragmentDefinitions = [];
74+
private PossibleTypesFinder $possibleTypesFinder;
7375

7476
public function __construct(
7577
public private(set) readonly Config $config,
@@ -79,6 +81,7 @@ public function __construct(
7981
private readonly EnglishInflector $inflector,
8082
) {
8183
$this->result = new PlannerResult();
84+
$this->possibleTypesFinder = new PossibleTypesFinder($this->schema);
8285
}
8386

8487
/**
@@ -1013,7 +1016,7 @@ private function createDataClassPlan(
10131016
parentType: $parentType,
10141017
fields: $fields,
10151018
payloadShape: $payloadShape,
1016-
possibleTypes: $this->getPossibleTypes($fieldType),
1019+
possibleTypes: $this->possibleTypesFinder->find($fieldType),
10171020
definitionNode: new InlineFragmentNode([
10181021
'typeCondition' => new NamedTypeNode([
10191022
'name' => new NameNode([
@@ -1129,37 +1132,6 @@ private function fullyQualified(string $part, string ...$moreParts) : string
11291132
return implode('\\', array_filter([$this->config->namespace, $part, ...$moreParts], fn($part) => $part !== ''));
11301133
}
11311134

1132-
/**
1133-
* @throws InvariantViolation
1134-
* @return list<string>
1135-
*/
1136-
private function getPossibleTypes(Type $type) : array
1137-
{
1138-
if ($type instanceof NonNull) {
1139-
$type = $type->getWrappedType();
1140-
}
1141-
1142-
if ($type instanceof UnionType) {
1143-
$possible = [];
1144-
foreach ($type->getTypes() as $possibleType) {
1145-
$possible[] = $possibleType->name;
1146-
}
1147-
1148-
return $possible;
1149-
}
1150-
1151-
if ($type instanceof InterfaceType) {
1152-
$possible = [];
1153-
foreach ($this->schema->getImplementations($type)->objects() as $possibleType) {
1154-
$possible[] = $possibleType->name;
1155-
}
1156-
1157-
return $possible;
1158-
}
1159-
1160-
return [];
1161-
}
1162-
11631135
public function setFragmentPayloadShape(string $name, SymfonyType $shape) : void
11641136
{
11651137
$this->fragmentPayloadShapes[$name] = $shape;

tests/Fragments/Generated/Fragment/ViewerName.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ final class ViewerName
1111
/**
1212
* @var list<string>
1313
*/
14-
public const array POSSIBLE_TYPES = ['User', 'Application'];
14+
public const array POSSIBLE_TYPES = ['Application', 'User'];
1515

1616
public string $name {
1717
get => $this->name ??= $this->data['name'];

tests/Fragments/Generated/Query/Test/Data/Viewer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ final class Viewer
1515
/**
1616
* @var list<string>
1717
*/
18-
public const array POSSIBLE_TYPES = ['User', 'Application'];
18+
public const array POSSIBLE_TYPES = ['Application', 'User'];
1919

2020
public string $__typename {
2121
get => $this->__typename ??= $this->data['__typename'];

tests/InlineFragments/Generated/Query/Test/Data/Viewer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ final class Viewer
1414
/**
1515
* @var list<string>
1616
*/
17-
public const array POSSIBLE_TYPES = ['User', 'Application'];
17+
public const array POSSIBLE_TYPES = ['Application', 'User'];
1818

1919
public string $__typename {
2020
get => $this->__typename ??= $this->data['__typename'];

tests/Optimization/Generated/Query/Test/Data/Viewer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ final class Viewer
1414
/**
1515
* @var list<string>
1616
*/
17-
public const array POSSIBLE_TYPES = ['User', 'Application'];
17+
public const array POSSIBLE_TYPES = ['Application', 'User'];
1818

1919
public string $__typename {
2020
get => $this->__typename ??= $this->data['__typename'];

0 commit comments

Comments
 (0)