Skip to content

Commit d55a1d4

Browse files
committed
Don't generate empty argument objects
1 parent 304b145 commit d55a1d4

File tree

6 files changed

+31
-25
lines changed

6 files changed

+31
-25
lines changed

src/SchemaGenerator/CodeGenerator/QueryObjectClassBuilder.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ public function addScalarField(string $fieldName, bool $isDeprecated, ?string $d
5252
* @param string $fieldName
5353
* @param string $typeName
5454
* @param string $typeKind
55-
* @param string $argsObjectName
55+
* @param string|null $argsObjectName
5656
* @param bool $isDeprecated
5757
* @param string|null $deprecationReason
5858
*/
59-
public function addObjectField(string $fieldName, string $typeName, string $typeKind, string $argsObjectName, bool $isDeprecated, ?string $deprecationReason)
59+
public function addObjectField(string $fieldName, string $typeName, string $typeKind, ?string $argsObjectName, bool $isDeprecated, ?string $deprecationReason)
6060
{
6161
$upperCamelCaseProp = StringLiteralFormatter::formatUpperCamelCase($fieldName);
6262
$this->addObjectSelector($fieldName, $upperCamelCaseProp, $typeName, $typeKind, $argsObjectName, $isDeprecated, $deprecationReason);
@@ -84,14 +84,24 @@ protected function addSimpleSelector(string $propertyName, string $upperCamelNam
8484
* @param string $upperCamelName
8585
* @param string $fieldTypeName
8686
* @param string $fieldTypeKind
87-
* @param string $argsObjectName
87+
* @param string|null $argsObjectName
8888
* @param bool $isDeprecated
8989
* @param string|null $deprecationReason
9090
*/
91-
protected function addObjectSelector(string $fieldName, string $upperCamelName, string $fieldTypeName, string $fieldTypeKind, string $argsObjectName, bool $isDeprecated, ?string $deprecationReason)
91+
protected function addObjectSelector(string $fieldName, string $upperCamelName, string $fieldTypeName, string $fieldTypeKind, ?string $argsObjectName, bool $isDeprecated, ?string $deprecationReason)
9292
{
9393
$objectClass = $fieldTypeName . ($fieldTypeKind === FieldTypeKindEnum::UNION_OBJECT ? 'UnionObject' : 'QueryObject');
94-
$method = "public function select$upperCamelName($argsObjectName \$argsObject = null)
94+
95+
if ($argsObjectName === null) {
96+
$method = "public function select$upperCamelName()
97+
{
98+
\$object = new $objectClass(\"$fieldName\");
99+
\$this->selectField(\$object);
100+
101+
return \$object;
102+
}";
103+
} else {
104+
$method = "public function select$upperCamelName($argsObjectName \$argsObject = null)
95105
{
96106
\$object = new $objectClass(\"$fieldName\");
97107
if (\$argsObject !== null) {
@@ -101,6 +111,8 @@ protected function addObjectSelector(string $fieldName, string $upperCamelName,
101111
102112
return \$object;
103113
}";
114+
}
115+
104116
$this->classFile->addMethod($method, $isDeprecated, $deprecationReason);
105117
}
106118

src/SchemaGenerator/SchemaClassGenerator.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,17 @@ private function appendQueryObjectFields(QueryObjectClassBuilder $queryObjectBui
112112
// Generate nested type object if it wasn't generated
113113
$objectGenerated = $this->generateObject($typeName, $typeKind);
114114
if ($objectGenerated) {
115+
$arguments = $fieldArray['args'] ?? [];
116+
if (empty($arguments)) {
117+
$argsObjectName = null;
118+
$argsObjectGenerated = true;
119+
} else {
120+
// Generate nested type arguments object if it wasn't generated
121+
$argsObjectName = $currentTypeName . StringLiteralFormatter::formatUpperCamelCase($name) . 'ArgumentsObject';
122+
$argsObjectGenerated = $this->generateArgumentsObject($argsObjectName, $fieldArray['args'] ?? []);
123+
}
115124

116-
// Generate nested type arguments object if it wasn't generated
117-
$argsObjectName = $currentTypeName . StringLiteralFormatter::formatUpperCamelCase($name) . 'ArgumentsObject';
118-
$argsObjectGenerated = $this->generateArgumentsObject($argsObjectName, $fieldArray['args'] ?? []);
119125
if ($argsObjectGenerated) {
120-
121126
// Add sub type as a field to the query object if all generation happened successfully
122127
$queryObjectBuilder->addObjectField($name, $typeName, $typeKind, $argsObjectName, $fieldArray['isDeprecated'], $fieldArray['deprecationReason']);
123128
}

tests/QueryObjectClassBuilderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ public function testAddMultipleObjectSelectors()
121121
$objectName = 'MultipleObjectSelectors';
122122
$classBuilder = new QueryObjectClassBuilder(static::getGeneratedFilesDir(), $objectName, static::TESTING_NAMESPACE);
123123
$objectName .= 'QueryObject';
124-
$classBuilder->addObjectField('right', 'MultipleObjectSelectorsRight', FieldTypeKindEnum::OBJECT, 'MultipleObjectSelectorsRightArgumentsObject', false, null);
125-
$classBuilder->addObjectField('left_objects', 'Left', FieldTypeKindEnum::OBJECT, 'MultipleObjectSelectorsLeftObjectsArgumentsObject', true, null);
124+
$classBuilder->addObjectField('right', 'MultipleObjectSelectorsRight', FieldTypeKindEnum::OBJECT, null, false, null);
125+
$classBuilder->addObjectField('left_objects', 'Left', FieldTypeKindEnum::OBJECT, null, true, null);
126126
$classBuilder->build();
127127

128128
$this->assertFileEquals(

tests/SchemaClassGeneratorTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -783,10 +783,8 @@ public function testGenerateQueryObjectWithObjectFields()
783783

784784
// Test if the right classes are generated.
785785
$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");
787786

788787
$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");
790788
}
791789

792790
/**

tests/files_expected/query_objects/MultipleObjectSelectorsQueryObject.php

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

11-
public function selectRight(MultipleObjectSelectorsRightArgumentsObject $argsObject = null)
11+
public function selectRight()
1212
{
1313
$object = new MultipleObjectSelectorsRightQueryObject("right");
14-
if ($argsObject !== null) {
15-
$object->appendArguments($argsObject->toArray());
16-
}
1714
$this->selectField($object);
1815

1916
return $object;
@@ -22,12 +19,9 @@ public function selectRight(MultipleObjectSelectorsRightArgumentsObject $argsObj
2219
/**
2320
* @deprecated
2421
*/
25-
public function selectLeftObjects(MultipleObjectSelectorsLeftObjectsArgumentsObject $argsObject = null)
22+
public function selectLeftObjects()
2623
{
2724
$object = new LeftQueryObject("left_objects");
28-
if ($argsObject !== null) {
29-
$object->appendArguments($argsObject->toArray());
30-
}
3125
$this->selectField($object);
3226

3327
return $object;

tests/files_expected/union_objects/UnionObject1QueryObject.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@ class UnionObject1QueryObject extends QueryObject
88
{
99
const OBJECT_NAME = "UnionObject1";
1010

11-
public function selectUnion(UnionObject1UnionArgumentsObject $argsObject = null)
11+
public function selectUnion()
1212
{
1313
$object = new UnionTestObjectUnionObject("union");
14-
if ($argsObject !== null) {
15-
$object->appendArguments($argsObject->toArray());
16-
}
1714
$this->selectField($object);
1815

1916
return $object;

0 commit comments

Comments
 (0)