Skip to content

Commit f72823c

Browse files
jorritmghoneimy
authored andcommitted
generatedObjects list should cache more specific class name
1 parent 87cc447 commit f72823c

File tree

7 files changed

+64
-52
lines changed

7 files changed

+64
-52
lines changed

src/SchemaGenerator/CodeGenerator/ArgumentsObjectClassBuilder.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ class ArgumentsObjectClassBuilder extends ObjectClassBuilder
2121
*/
2222
public function __construct(string $writeDir, string $objectName, string $namespace = self::DEFAULT_NAMESPACE)
2323
{
24-
$className = $objectName . 'ArgumentsObject';
25-
26-
$this->classFile = new ClassFile($writeDir, $className);
24+
$this->classFile = new ClassFile($writeDir, $objectName);
2725
$this->classFile->setNamespace($namespace);
2826
if ($namespace !== self::DEFAULT_NAMESPACE) {
2927
$this->classFile->addImport('GraphQL\\SchemaObject\\ArgumentsObject');

src/SchemaGenerator/CodeGenerator/QueryObjectClassBuilder.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ protected function addSimpleSelector(string $propertyName, string $upperCamelNam
8484
protected function addObjectSelector(string $fieldName, string $upperCamelName, string $fieldTypeName, string $argsObjectName, bool $isDeprecated, ?string $deprecationReason)
8585
{
8686
$objectClassName = $fieldTypeName . 'QueryObject';
87-
$argsMapClassName = $argsObjectName . 'ArgumentsObject';
88-
$method = "public function select$upperCamelName($argsMapClassName \$argsObject = null)
87+
$method = "public function select$upperCamelName($argsObjectName \$argsObject = null)
8988
{
9089
\$object = new $objectClassName(\"$fieldName\");
9190
if (\$argsObject !== null) {

src/SchemaGenerator/SchemaClassGenerator.php

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,14 @@ public function generateRootQueryObject(): bool
7171
$queryTypeName = $objectArray['name'];
7272
//$rootObjectDescr = $objectArray['description'];
7373

74-
$queryObjectBuilder = new QueryObjectClassBuilder($this->writeDir, $rootObjectName, $this->generationNamespace);
74+
if (array_key_exists($queryTypeName, $this->generatedObjects)) {
75+
return true;
76+
}
77+
7578
$this->generatedObjects[$queryTypeName] = true;
76-
$this->appendQueryObjectFields($queryObjectBuilder, $rootObjectName, $objectArray['fields']);
7779

80+
$queryObjectBuilder = new QueryObjectClassBuilder($this->writeDir, $rootObjectName, $this->generationNamespace);
81+
$this->appendQueryObjectFields($queryObjectBuilder, $rootObjectName, $objectArray['fields']);
7882
$queryObjectBuilder->build();
7983

8084
return true;
@@ -100,21 +104,17 @@ private function appendQueryObjectFields(QueryObjectClassBuilder $queryObjectBui
100104
if ($typeKind === FieldTypeKindEnum::SCALAR) {
101105
$queryObjectBuilder->addScalarField($name, $fieldArray['isDeprecated'], $fieldArray['deprecationReason']);
102106
} elseif ($typeKind === FieldTypeKindEnum::ENUM_OBJECT) {
103-
if (!array_key_exists($typeName, $this->generatedObjects)) {
104-
$this->generateEnumObject($typeName);
105-
}
107+
$this->generateEnumObject($typeName);
106108
$queryObjectBuilder->addScalarField($name, $fieldArray['isDeprecated'], $fieldArray['deprecationReason']);
107109
} else {
108110

109111
// Generate nested type object if it wasn't generated
110-
$objectGenerated = array_key_exists($typeName, $this->generatedObjects) ? :
111-
$this->generateObject($typeName, $typeKind);
112+
$objectGenerated = $this->generateObject($typeName, $typeKind);
112113
if ($objectGenerated) {
113114

114115
// Generate nested type arguments object if it wasn't generated
115-
$argsObjectName = $currentTypeName . StringLiteralFormatter::formatUpperCamelCase($name);
116-
$argsObjectGenerated = array_key_exists($argsObjectName, $this->generatedObjects) ? :
117-
$this->generateArgumentsObject($argsObjectName, $fieldArray['args'] ?? []);
116+
$argsObjectName = $currentTypeName . StringLiteralFormatter::formatUpperCamelCase($name) . 'ArgumentsObject';
117+
$argsObjectGenerated = $this->generateArgumentsObject($argsObjectName, $fieldArray['args'] ?? []);
118118
if ($argsObjectGenerated) {
119119

120120
// Add sub type as a field to the query object if all generation happened successfully
@@ -153,11 +153,15 @@ protected function generateObject(string $objectName, string $objectKind): bool
153153
*/
154154
protected function generateQueryObject(string $objectName): bool
155155
{
156+
if (array_key_exists($objectName, $this->generatedObjects)) {
157+
return true;
158+
}
159+
160+
$this->generatedObjects[$objectName] = true;
156161
$objectArray = $this->schemaInspector->getObjectSchema($objectName);
157162
$objectName = $objectArray['name'];
158163
$objectBuilder = new QueryObjectClassBuilder($this->writeDir, $objectName, $this->generationNamespace);
159164

160-
$this->generatedObjects[$objectName] = true;
161165
$this->appendQueryObjectFields($objectBuilder, $objectName, $objectArray['fields']);
162166
$objectBuilder->build();
163167

@@ -171,11 +175,15 @@ protected function generateQueryObject(string $objectName): bool
171175
*/
172176
protected function generateInputObject(string $objectName): bool
173177
{
178+
if (array_key_exists($objectName, $this->generatedObjects)) {
179+
return true;
180+
}
181+
182+
$this->generatedObjects[$objectName] = true;
174183
$objectArray = $this->schemaInspector->getInputObjectSchema($objectName);
175184
$objectName = $objectArray['name'];
176185
$objectBuilder = new InputObjectClassBuilder($this->writeDir, $objectName, $this->generationNamespace);
177186

178-
$this->generatedObjects[$objectName] = true;
179187
foreach ($objectArray['inputFields'] as $inputFieldArray) {
180188
$name = $inputFieldArray['name'];
181189
//$description = $inputFieldArray['description'];
@@ -184,7 +192,7 @@ protected function generateInputObject(string $objectName): bool
184192

185193
$objectGenerated = true;
186194
if ($typeKind !== FieldTypeKindEnum::SCALAR) {
187-
$objectGenerated = array_key_exists($typeName, $this->generatedObjects) ?: $this->generateObject($typeName, $typeKind);
195+
$objectGenerated = $this->generateObject($typeName, $typeKind);
188196
}
189197

190198
if ($objectGenerated) {
@@ -212,11 +220,16 @@ protected function generateInputObject(string $objectName): bool
212220
*/
213221
protected function generateEnumObject(string $objectName): bool
214222
{
223+
if (array_key_exists($objectName, $this->generatedObjects)) {
224+
return true;
225+
}
226+
227+
$this->generatedObjects[$objectName] = true;
228+
215229
$objectArray = $this->schemaInspector->getEnumObjectSchema($objectName);
216230
$objectName = $objectArray['name'];
217231
$objectBuilder = new EnumObjectBuilder($this->writeDir, $objectName, $this->generationNamespace);
218-
219-
$this->generatedObjects[$objectName] = true;
232+
220233
foreach ($objectArray['enumValues'] as $enumValue) {
221234
$name = $enumValue['name'];
222235
//$description = $enumValue['description'];
@@ -235,9 +248,14 @@ protected function generateEnumObject(string $objectName): bool
235248
*/
236249
protected function generateArgumentsObject(string $argsObjectName, array $arguments): bool
237250
{
238-
$objectBuilder = new ArgumentsObjectClassBuilder($this->writeDir, $argsObjectName, $this->generationNamespace);
251+
if (array_key_exists($argsObjectName, $this->generatedObjects)) {
252+
return true;
253+
}
239254

240255
$this->generatedObjects[$argsObjectName] = true;
256+
257+
$objectBuilder = new ArgumentsObjectClassBuilder($this->writeDir, $argsObjectName, $this->generationNamespace);
258+
241259
foreach ($arguments as $argumentArray) {
242260
$name = $argumentArray['name'];
243261
//$description = $inputFieldArray['description'];
@@ -246,7 +264,7 @@ protected function generateArgumentsObject(string $argsObjectName, array $argume
246264

247265
$objectGenerated = true;
248266
if ($typeKind !== FieldTypeKindEnum::SCALAR) {
249-
$objectGenerated = array_key_exists($typeName, $this->generatedObjects) ?: $this->generateObject($typeName, $typeKind);
267+
$objectGenerated = $this->generateObject($typeName, $typeKind);
250268
}
251269

252270
if ($objectGenerated) {

tests/ArgumentsObjectClassBuilderTest.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ protected static function getExpectedFilesDir()
3232
*/
3333
public function testAddScalarArgument()
3434
{
35-
$objectName = 'WithScalarArg';
35+
$objectName = 'WithScalarArgArgumentsObject';
3636
$classBuilder = new ArgumentsObjectClassBuilder(static::getGeneratedFilesDir(), $objectName, static::TESTING_NAMESPACE);
37-
$objectName .= 'ArgumentsObject';
3837
$classBuilder->addScalarArgument('scalarProperty');
3938
$classBuilder->build();
4039

@@ -54,9 +53,8 @@ public function testAddScalarArgument()
5453
*/
5554
public function testAddMultipleScalarArguments()
5655
{
57-
$objectName = 'WithMultipleScalarArgs';
56+
$objectName = 'WithMultipleScalarArgsArgumentsObject';
5857
$classBuilder = new ArgumentsObjectClassBuilder(static::getGeneratedFilesDir(), $objectName, static::TESTING_NAMESPACE);
59-
$objectName .= 'ArgumentsObject';
6058
$classBuilder->addScalarArgument('scalarProperty');
6159
$classBuilder->addScalarArgument('another_scalar_property');
6260
$classBuilder->build();
@@ -77,9 +75,8 @@ public function testAddMultipleScalarArguments()
7775
*/
7876
public function testAddListArgument()
7977
{
80-
$objectName = 'WithListArg';
78+
$objectName = 'WithListArgArgumentsObject';
8179
$classBuilder = new ArgumentsObjectClassBuilder(static::getGeneratedFilesDir(), $objectName, static::TESTING_NAMESPACE);
82-
$objectName .= 'ArgumentsObject';
8380
$classBuilder->addListArgument('listProperty', 'string');
8481
$classBuilder->build();
8582

@@ -99,9 +96,8 @@ public function testAddListArgument()
9996
*/
10097
public function testAddMultipleListArguments()
10198
{
102-
$objectName = 'WithMultipleListArgs';
99+
$objectName = 'WithMultipleListArgsArgumentsObject';
103100
$classBuilder = new ArgumentsObjectClassBuilder(static::getGeneratedFilesDir(), $objectName, static::TESTING_NAMESPACE);
104-
$objectName .= 'ArgumentsObject';
105101
$classBuilder->addListArgument('listProperty', 'string');
106102
$classBuilder->addListArgument('another_list_property', 'string');
107103
$classBuilder->build();
@@ -122,9 +118,8 @@ public function testAddMultipleListArguments()
122118
*/
123119
public function testAddInputObjectArgument()
124120
{
125-
$objectName = 'WithInputObjectArg';
121+
$objectName = 'WithInputObjectArgArgumentsObject';
126122
$classBuilder = new ArgumentsObjectClassBuilder(static::getGeneratedFilesDir(), $objectName, static::TESTING_NAMESPACE);
127-
$objectName .= 'ArgumentsObject';
128123
$classBuilder->addInputObjectArgument('objectProperty', 'Some');
129124
$classBuilder->build();
130125

@@ -144,9 +139,8 @@ public function testAddInputObjectArgument()
144139
*/
145140
public function testAddMultipleInputObjectArguments()
146141
{
147-
$objectName = 'WithMultipleInputObjectArgs';
142+
$objectName = 'WithMultipleInputObjectArgsArgumentsObject';
148143
$classBuilder = new ArgumentsObjectClassBuilder(static::getGeneratedFilesDir(), $objectName, static::TESTING_NAMESPACE);
149-
$objectName .= 'ArgumentsObject';
150144
$classBuilder->addInputObjectArgument('objectProperty', 'Some');
151145
$classBuilder->addInputObjectArgument('another_object_property', 'Another');
152146
$classBuilder->build();

tests/QueryObjectClassBuilderTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function testAddObjectSelector()
100100
$objectName = 'ObjectSelector';
101101
$classBuilder = new QueryObjectClassBuilder(static::getGeneratedFilesDir(), $objectName, static::TESTING_NAMESPACE);
102102
$objectName .= 'QueryObject';
103-
$classBuilder->addObjectField('others', 'Other', 'RootOthers', false, null);
103+
$classBuilder->addObjectField('others', 'Other', 'RootOthersArgumentsObject', false, null);
104104
$classBuilder->build();
105105

106106
$this->assertFileEquals(
@@ -120,8 +120,8 @@ public function testAddMultipleObjectSelectors()
120120
$objectName = 'MultipleObjectSelectors';
121121
$classBuilder = new QueryObjectClassBuilder(static::getGeneratedFilesDir(), $objectName, static::TESTING_NAMESPACE);
122122
$objectName .= 'QueryObject';
123-
$classBuilder->addObjectField('right_objects', 'Right', 'MultipleObjectSelectorsRightObjects', false, null);
124-
$classBuilder->addObjectField('left_objects', 'Left', 'MultipleObjectSelectorsLeftObjects', true, null);
123+
$classBuilder->addObjectField('right', 'MultipleObjectSelectorsRight', 'MultipleObjectSelectorsRightArgumentsObject', false, null);
124+
$classBuilder->addObjectField('left_objects', 'Left', 'MultipleObjectSelectorsLeftObjectsArgumentsObject', true, null);
125125
$classBuilder->build();
126126

127127
$this->assertFileEquals(

tests/SchemaClassGeneratorTest.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ public function testGenerateInputObjectWithNestedObjectValues()
413413
*/
414414
public function testGenerateArgumentsObjectWithScalarArgs()
415415
{
416-
$objectName = 'WithMultipleScalarArgs';
416+
$objectName = 'WithMultipleScalarArgsArgumentsObject';
417417
$argsArray = [
418418
[
419419
'name' => 'scalarProperty',
@@ -437,9 +437,8 @@ public function testGenerateArgumentsObjectWithScalarArgs()
437437
]
438438
]
439439
];
440-
$this->classGenerator->generateArgumentsObject('WithMultipleScalarArgs', $argsArray);
440+
$this->classGenerator->generateArgumentsObject($objectName, $argsArray);
441441

442-
$objectName .= 'ArgumentsObject';
443442
$this->assertFileEquals(
444443
static::getExpectedFilesDir() . "/arguments_objects/$objectName.php",
445444
static::getGeneratedFilesDir() . "/$objectName.php"
@@ -452,7 +451,7 @@ public function testGenerateArgumentsObjectWithScalarArgs()
452451
*/
453452
public function testGenerateArgumentsObjectWithEnumArg()
454453
{
455-
$objectName = 'WithMultipleEnumArg';
454+
$objectName = 'WithMultipleEnumArgArgumentsObject';
456455
// Add mock responses
457456
$this->mockHandler->append(new Response(200, [], json_encode([
458457
'data' => [
@@ -481,9 +480,8 @@ public function testGenerateArgumentsObjectWithEnumArg()
481480
]
482481
]
483482
];
484-
$this->classGenerator->generateArgumentsObject('WithMultipleEnumArg', $argsArray);
483+
$this->classGenerator->generateArgumentsObject($objectName, $argsArray);
485484

486-
$objectName .= 'ArgumentsObject';
487485
$this->assertFileEquals(
488486
static::getExpectedFilesDir() . "/arguments_objects/$objectName.php",
489487
static::getGeneratedFilesDir() . "/$objectName.php"
@@ -496,7 +494,7 @@ public function testGenerateArgumentsObjectWithEnumArg()
496494
*/
497495
public function testGenerateArgumentsObjectWithListArgs()
498496
{
499-
$objectName = 'WithMultipleListArgs';
497+
$objectName = 'WithMultipleListArgsArgumentsObject';
500498
// Add mock responses
501499
$this->mockHandler->append(new Response(200, [], json_encode([
502500
'data' => [
@@ -552,7 +550,6 @@ public function testGenerateArgumentsObjectWithListArgs()
552550
];
553551
$this->classGenerator->generateArgumentsObject($objectName, $argsArray);
554552

555-
$objectName .= 'ArgumentsObject';
556553
$this->assertFileEquals(
557554
static::getExpectedFilesDir() . "/arguments_objects/$objectName.php",
558555
static::getGeneratedFilesDir() . "/$objectName.php"
@@ -585,7 +582,7 @@ public function testGenerateArgumentsObjectWithInputObjectArgs()
585582
]
586583
])));
587584

588-
$objectName = 'WithMultipleInputObjectArgs';
585+
$objectName = 'WithMultipleInputObjectArgsArgumentsObject';
589586
$argsArray = [
590587
[
591588
'name' => 'objectProperty',
@@ -611,7 +608,6 @@ public function testGenerateArgumentsObjectWithInputObjectArgs()
611608
];
612609
$this->classGenerator->generateArgumentsObject($objectName, $argsArray);
613610

614-
$objectName .= 'ArgumentsObject';
615611
$this->assertFileEquals(
616612
static::getExpectedFilesDir() . "/arguments_objects/$objectName.php",
617613
static::getGeneratedFilesDir() . "/$objectName.php"
@@ -721,7 +717,7 @@ public function testGenerateQueryObjectWithObjectFields()
721717
'kind' => FieldTypeKindEnum::OBJECT,
722718
'fields' => [
723719
[
724-
'name' => 'right_objects',
720+
'name' => 'right',
725721
'description' => null,
726722
'isDeprecated' => false,
727723
'deprecationReason' => null,
@@ -730,7 +726,7 @@ public function testGenerateQueryObjectWithObjectFields()
730726
'kind' => FieldTypeKindEnum::LIST,
731727
'description' => null,
732728
'ofType' => [
733-
'name' => 'Right',
729+
'name' => 'MultipleObjectSelectorsRight',
734730
'kind' => FieldTypeKindEnum::OBJECT,
735731
'description' => null,
736732
'ofType' => null,
@@ -762,7 +758,7 @@ public function testGenerateQueryObjectWithObjectFields()
762758
$this->mockHandler->append(new Response(200, [], json_encode([
763759
'data' => [
764760
'__type' => [
765-
'name' => 'Right',
761+
'name' => 'MultipleObjectSelectorsRight',
766762
'kind' => FieldTypeKindEnum::OBJECT,
767763
'fields' => []
768764
]
@@ -784,6 +780,13 @@ public function testGenerateQueryObjectWithObjectFields()
784780
static::getExpectedFilesDir() . "/query_objects/$objectName.php",
785781
static::getGeneratedFilesDir() . "/$objectName.php"
786782
);
783+
784+
// Test if the right classes are generated.
785+
$this->assertFileExists(static::getGeneratedFilesDir() . "/LeftQueryObject.php", "The query object name for the left field should consist of the type name Left plus QueryObject");
786+
$this->assertFileExists(static::getGeneratedFilesDir() . "/MultipleObjectSelectorsLeftObjectsArgumentsObject.php", "The argument object name for the left field should consist of the parent type name MultipleObjectSelectors plus the field name LeftObjects plus ArgumentsObject");
787+
788+
$this->assertFileExists(static::getGeneratedFilesDir() . "/MultipleObjectSelectorsRightQueryObject.php", "The query object name for the right field should consist of the type name MultipleObjectSelectorsRight plus QueryObject");
789+
$this->assertFileExists(static::getGeneratedFilesDir() . "/MultipleObjectSelectorsRightArgumentsObject.php", "The argument object name for the right field should consist of the parent type name MultipleObjectSelectors plus the field name Right plus ArgumentsObject");
787790
}
788791

789792
/**

tests/files_expected/query_objects/MultipleObjectSelectorsQueryObject.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ class MultipleObjectSelectorsQueryObject extends QueryObject
88
{
99
const OBJECT_NAME = "MultipleObjectSelectors";
1010

11-
public function selectRightObjects(MultipleObjectSelectorsRightObjectsArgumentsObject $argsObject = null)
11+
public function selectRight(MultipleObjectSelectorsRightArgumentsObject $argsObject = null)
1212
{
13-
$object = new RightQueryObject("right_objects");
13+
$object = new MultipleObjectSelectorsRightQueryObject("right");
1414
if ($argsObject !== null) {
1515
$object->appendArguments($argsObject->toArray());
1616
}

0 commit comments

Comments
 (0)