From d55a1d453d73a6a0aea71b09bfe7922b3c66fc74 Mon Sep 17 00:00:00 2001 From: Jorrit Schippers Date: Mon, 6 Sep 2021 17:04:43 +0200 Subject: [PATCH 1/6] Don't generate empty argument objects --- .../CodeGenerator/QueryObjectClassBuilder.php | 22 ++++++++++++++----- src/SchemaGenerator/SchemaClassGenerator.php | 13 +++++++---- tests/QueryObjectClassBuilderTest.php | 4 ++-- tests/SchemaClassGeneratorTest.php | 2 -- .../MultipleObjectSelectorsQueryObject.php | 10 ++------- .../union_objects/UnionObject1QueryObject.php | 5 +---- 6 files changed, 31 insertions(+), 25 deletions(-) diff --git a/src/SchemaGenerator/CodeGenerator/QueryObjectClassBuilder.php b/src/SchemaGenerator/CodeGenerator/QueryObjectClassBuilder.php index a8f4d03..087d01f 100644 --- a/src/SchemaGenerator/CodeGenerator/QueryObjectClassBuilder.php +++ b/src/SchemaGenerator/CodeGenerator/QueryObjectClassBuilder.php @@ -52,11 +52,11 @@ public function addScalarField(string $fieldName, bool $isDeprecated, ?string $d * @param string $fieldName * @param string $typeName * @param string $typeKind - * @param string $argsObjectName + * @param string|null $argsObjectName * @param bool $isDeprecated * @param string|null $deprecationReason */ - public function addObjectField(string $fieldName, string $typeName, string $typeKind, string $argsObjectName, bool $isDeprecated, ?string $deprecationReason) + public function addObjectField(string $fieldName, string $typeName, string $typeKind, ?string $argsObjectName, bool $isDeprecated, ?string $deprecationReason) { $upperCamelCaseProp = StringLiteralFormatter::formatUpperCamelCase($fieldName); $this->addObjectSelector($fieldName, $upperCamelCaseProp, $typeName, $typeKind, $argsObjectName, $isDeprecated, $deprecationReason); @@ -84,14 +84,24 @@ protected function addSimpleSelector(string $propertyName, string $upperCamelNam * @param string $upperCamelName * @param string $fieldTypeName * @param string $fieldTypeKind - * @param string $argsObjectName + * @param string|null $argsObjectName * @param bool $isDeprecated * @param string|null $deprecationReason */ - protected function addObjectSelector(string $fieldName, string $upperCamelName, string $fieldTypeName, string $fieldTypeKind, string $argsObjectName, bool $isDeprecated, ?string $deprecationReason) + protected function addObjectSelector(string $fieldName, string $upperCamelName, string $fieldTypeName, string $fieldTypeKind, ?string $argsObjectName, bool $isDeprecated, ?string $deprecationReason) { $objectClass = $fieldTypeName . ($fieldTypeKind === FieldTypeKindEnum::UNION_OBJECT ? 'UnionObject' : 'QueryObject'); - $method = "public function select$upperCamelName($argsObjectName \$argsObject = null) + + if ($argsObjectName === null) { + $method = "public function select$upperCamelName() +{ + \$object = new $objectClass(\"$fieldName\"); + \$this->selectField(\$object); + + return \$object; +}"; + } else { + $method = "public function select$upperCamelName($argsObjectName \$argsObject = null) { \$object = new $objectClass(\"$fieldName\"); if (\$argsObject !== null) { @@ -101,6 +111,8 @@ protected function addObjectSelector(string $fieldName, string $upperCamelName, return \$object; }"; + } + $this->classFile->addMethod($method, $isDeprecated, $deprecationReason); } diff --git a/src/SchemaGenerator/SchemaClassGenerator.php b/src/SchemaGenerator/SchemaClassGenerator.php index e4920ad..5e613c1 100644 --- a/src/SchemaGenerator/SchemaClassGenerator.php +++ b/src/SchemaGenerator/SchemaClassGenerator.php @@ -112,12 +112,17 @@ private function appendQueryObjectFields(QueryObjectClassBuilder $queryObjectBui // Generate nested type object if it wasn't generated $objectGenerated = $this->generateObject($typeName, $typeKind); if ($objectGenerated) { + $arguments = $fieldArray['args'] ?? []; + if (empty($arguments)) { + $argsObjectName = null; + $argsObjectGenerated = true; + } else { + // Generate nested type arguments object if it wasn't generated + $argsObjectName = $currentTypeName . StringLiteralFormatter::formatUpperCamelCase($name) . 'ArgumentsObject'; + $argsObjectGenerated = $this->generateArgumentsObject($argsObjectName, $fieldArray['args'] ?? []); + } - // Generate nested type arguments object if it wasn't generated - $argsObjectName = $currentTypeName . StringLiteralFormatter::formatUpperCamelCase($name) . 'ArgumentsObject'; - $argsObjectGenerated = $this->generateArgumentsObject($argsObjectName, $fieldArray['args'] ?? []); if ($argsObjectGenerated) { - // Add sub type as a field to the query object if all generation happened successfully $queryObjectBuilder->addObjectField($name, $typeName, $typeKind, $argsObjectName, $fieldArray['isDeprecated'], $fieldArray['deprecationReason']); } diff --git a/tests/QueryObjectClassBuilderTest.php b/tests/QueryObjectClassBuilderTest.php index b3eda79..8e601a3 100644 --- a/tests/QueryObjectClassBuilderTest.php +++ b/tests/QueryObjectClassBuilderTest.php @@ -121,8 +121,8 @@ public function testAddMultipleObjectSelectors() $objectName = 'MultipleObjectSelectors'; $classBuilder = new QueryObjectClassBuilder(static::getGeneratedFilesDir(), $objectName, static::TESTING_NAMESPACE); $objectName .= 'QueryObject'; - $classBuilder->addObjectField('right', 'MultipleObjectSelectorsRight', FieldTypeKindEnum::OBJECT, 'MultipleObjectSelectorsRightArgumentsObject', false, null); - $classBuilder->addObjectField('left_objects', 'Left', FieldTypeKindEnum::OBJECT, 'MultipleObjectSelectorsLeftObjectsArgumentsObject', true, null); + $classBuilder->addObjectField('right', 'MultipleObjectSelectorsRight', FieldTypeKindEnum::OBJECT, null, false, null); + $classBuilder->addObjectField('left_objects', 'Left', FieldTypeKindEnum::OBJECT, null, true, null); $classBuilder->build(); $this->assertFileEquals( diff --git a/tests/SchemaClassGeneratorTest.php b/tests/SchemaClassGeneratorTest.php index bdd95bf..ab6ed5f 100644 --- a/tests/SchemaClassGeneratorTest.php +++ b/tests/SchemaClassGeneratorTest.php @@ -783,10 +783,8 @@ public function testGenerateQueryObjectWithObjectFields() // Test if the right classes are generated. $this->assertFileExists(static::getGeneratedFilesDir() . "/LeftQueryObject.php", "The query object name for the left field should consist of the type name Left plus QueryObject"); - $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"); $this->assertFileExists(static::getGeneratedFilesDir() . "/MultipleObjectSelectorsRightQueryObject.php", "The query object name for the right field should consist of the type name MultipleObjectSelectorsRight plus QueryObject"); - $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"); } /** diff --git a/tests/files_expected/query_objects/MultipleObjectSelectorsQueryObject.php b/tests/files_expected/query_objects/MultipleObjectSelectorsQueryObject.php index eecce4b..603e63e 100644 --- a/tests/files_expected/query_objects/MultipleObjectSelectorsQueryObject.php +++ b/tests/files_expected/query_objects/MultipleObjectSelectorsQueryObject.php @@ -8,12 +8,9 @@ class MultipleObjectSelectorsQueryObject extends QueryObject { const OBJECT_NAME = "MultipleObjectSelectors"; - public function selectRight(MultipleObjectSelectorsRightArgumentsObject $argsObject = null) + public function selectRight() { $object = new MultipleObjectSelectorsRightQueryObject("right"); - if ($argsObject !== null) { - $object->appendArguments($argsObject->toArray()); - } $this->selectField($object); return $object; @@ -22,12 +19,9 @@ public function selectRight(MultipleObjectSelectorsRightArgumentsObject $argsObj /** * @deprecated */ - public function selectLeftObjects(MultipleObjectSelectorsLeftObjectsArgumentsObject $argsObject = null) + public function selectLeftObjects() { $object = new LeftQueryObject("left_objects"); - if ($argsObject !== null) { - $object->appendArguments($argsObject->toArray()); - } $this->selectField($object); return $object; diff --git a/tests/files_expected/union_objects/UnionObject1QueryObject.php b/tests/files_expected/union_objects/UnionObject1QueryObject.php index 22a8708..77cd930 100644 --- a/tests/files_expected/union_objects/UnionObject1QueryObject.php +++ b/tests/files_expected/union_objects/UnionObject1QueryObject.php @@ -8,12 +8,9 @@ class UnionObject1QueryObject extends QueryObject { const OBJECT_NAME = "UnionObject1"; - public function selectUnion(UnionObject1UnionArgumentsObject $argsObject = null) + public function selectUnion() { $object = new UnionTestObjectUnionObject("union"); - if ($argsObject !== null) { - $object->appendArguments($argsObject->toArray()); - } $this->selectField($object); return $object; From 17d9a75fd7ee9acd91b7aac39a130eceb3c86e92 Mon Sep 17 00:00:00 2001 From: Jorrit Schippers Date: Tue, 20 Feb 2024 14:14:55 +0100 Subject: [PATCH 2/6] Add support for interfaces --- src/Enumeration/FieldTypeKindEnum.php | 15 +-- .../CodeGenerator/InterfaceObjectBuilder.php | 60 ++++++++++++ src/SchemaGenerator/SchemaClassGenerator.php | 40 +++++++- src/SchemaGenerator/SchemaInspector.php | 4 + src/SchemaObject/InterfaceObject.php | 27 +++++ tests/InterfaceObjectTest.php | 84 ++++++++++++++++ tests/SchemaClassGeneratorTest.php | 98 +++++++++++++++++++ tests/UnionObjectTest.php | 10 +- .../InterfaceObject1QueryObject.php | 17 ++++ .../InterfaceObject2QueryObject.php | 10 ++ .../TestInterfaceQueryObject.php | 25 +++++ 11 files changed, 376 insertions(+), 14 deletions(-) create mode 100644 src/SchemaGenerator/CodeGenerator/InterfaceObjectBuilder.php create mode 100644 src/SchemaObject/InterfaceObject.php create mode 100644 tests/InterfaceObjectTest.php create mode 100644 tests/files_expected/interface_objects/InterfaceObject1QueryObject.php create mode 100644 tests/files_expected/interface_objects/InterfaceObject2QueryObject.php create mode 100644 tests/files_expected/interface_objects/TestInterfaceQueryObject.php diff --git a/src/Enumeration/FieldTypeKindEnum.php b/src/Enumeration/FieldTypeKindEnum.php index 28dfa7b..d70614a 100644 --- a/src/Enumeration/FieldTypeKindEnum.php +++ b/src/Enumeration/FieldTypeKindEnum.php @@ -9,11 +9,12 @@ */ class FieldTypeKindEnum { - const SCALAR = 'SCALAR'; - const LIST = 'LIST'; - const NON_NULL = 'NON_NULL'; - const OBJECT = 'OBJECT'; - const INPUT_OBJECT = 'INPUT_OBJECT'; - const ENUM_OBJECT = 'ENUM'; - const UNION_OBJECT = 'UNION'; + const SCALAR = 'SCALAR'; + const LIST = 'LIST'; + const NON_NULL = 'NON_NULL'; + const OBJECT = 'OBJECT'; + const INPUT_OBJECT = 'INPUT_OBJECT'; + const ENUM_OBJECT = 'ENUM'; + const UNION_OBJECT = 'UNION'; + const INTERFACE_OBJECT = 'INTERFACE'; } diff --git a/src/SchemaGenerator/CodeGenerator/InterfaceObjectBuilder.php b/src/SchemaGenerator/CodeGenerator/InterfaceObjectBuilder.php new file mode 100644 index 0000000..7373d03 --- /dev/null +++ b/src/SchemaGenerator/CodeGenerator/InterfaceObjectBuilder.php @@ -0,0 +1,60 @@ +classFile = new ClassFile($writeDir, $className); + $this->classFile->setNamespace($namespace); + if ($namespace !== self::DEFAULT_NAMESPACE) { + $this->classFile->addImport('GraphQL\\SchemaObject\\InterfaceObject'); + } + $this->classFile->extendsClass('InterfaceObject'); + } + + /** + * @param string $typeName + */ + public function addImplementation(string $typeName) + { + $upperCamelCaseTypeName = StringLiteralFormatter::formatUpperCamelCase($typeName); + $objectClassName = $typeName . 'QueryObject'; + $method = "public function on$upperCamelCaseTypeName(): $objectClassName +{ + return \$this->addImplementation($objectClassName::class); +}"; + $this->classFile->addMethod($method); + } + + /** + * @return void + */ + public function build(): void + { + $this->classFile->writeFile(); + } +} diff --git a/src/SchemaGenerator/SchemaClassGenerator.php b/src/SchemaGenerator/SchemaClassGenerator.php index 5e613c1..c37348d 100644 --- a/src/SchemaGenerator/SchemaClassGenerator.php +++ b/src/SchemaGenerator/SchemaClassGenerator.php @@ -7,6 +7,7 @@ use GraphQL\SchemaGenerator\CodeGenerator\ArgumentsObjectClassBuilder; use GraphQL\SchemaGenerator\CodeGenerator\EnumObjectBuilder; use GraphQL\SchemaGenerator\CodeGenerator\InputObjectClassBuilder; +use GraphQL\SchemaGenerator\CodeGenerator\InterfaceObjectBuilder; use GraphQL\SchemaGenerator\CodeGenerator\ObjectBuilderInterface; use GraphQL\SchemaGenerator\CodeGenerator\QueryObjectClassBuilder; use GraphQL\SchemaGenerator\CodeGenerator\UnionObjectBuilder; @@ -44,7 +45,7 @@ class SchemaClassGenerator *AND complete covering the schema scanner class * @var array */ - private $generatedObjects; + private $generatedObjects; /** * SchemaClassGenerator constructor. @@ -141,6 +142,7 @@ protected function generateObject(string $objectName, string $objectKind): bool { switch ($objectKind) { case FieldTypeKindEnum::OBJECT: + case FieldTypeKindEnum::INTERFACE_OBJECT: return $this->generateQueryObject($objectName); case FieldTypeKindEnum::INPUT_OBJECT: return $this->generateInputObject($objectName); @@ -168,7 +170,15 @@ protected function generateQueryObject(string $objectName): bool $this->generatedObjects[$objectName] = true; $objectArray = $this->schemaInspector->getObjectSchema($objectName); $objectName = $objectArray['name']; - $objectBuilder = new QueryObjectClassBuilder($this->writeDir, $objectName, $this->generationNamespace); + + if ($objectArray['kind'] === FieldTypeKindEnum::INTERFACE_OBJECT) { + $objectBuilder = new InterfaceObjectBuilder($this->writeDir, $objectName, $this->generationNamespace); + foreach ($objectArray['possibleTypes'] as $possibleType) { + $objectBuilder->addImplementation($possibleType['name']); + } + } else { + $objectBuilder = new QueryObjectClassBuilder($this->writeDir, $objectName, $this->generationNamespace); + } $this->appendQueryObjectFields($objectBuilder, $objectName, $objectArray['fields']); $objectBuilder->build(); @@ -274,6 +284,32 @@ protected function generateUnionObject(string $objectName): bool return true; } + /** + * @param string $objectName + * + * @return bool + */ + protected function generateInterfaceObject(string $objectName): bool + { + if (array_key_exists($objectName, $this->generatedObjects)) { + return true; + } + + $this->generatedObjects[$objectName] = true; + + $objectArray = $this->schemaInspector->getObjectSchema($objectName); + $objectName = $objectArray['name']; + $objectBuilder = new UnionObjectBuilder($this->writeDir, $objectName, $this->generationNamespace); + + foreach ($objectArray['possibleTypes'] as $possibleType) { + $this->generateObject($possibleType['name'], $possibleType['kind']); + $objectBuilder->addPossibleType($possibleType['name']); + } + $objectBuilder->build(); + + return true; + } + /** * @param string $argsObjectName * @param array $arguments diff --git a/src/SchemaGenerator/SchemaInspector.php b/src/SchemaGenerator/SchemaInspector.php index 424ccc7..9c80e32 100644 --- a/src/SchemaGenerator/SchemaInspector.php +++ b/src/SchemaGenerator/SchemaInspector.php @@ -96,6 +96,10 @@ public function getObjectSchema(string $objectName): array __type(name: \"$objectName\") { name kind + possibleTypes { + kind + name + } fields(includeDeprecated: true){ name description diff --git a/src/SchemaObject/InterfaceObject.php b/src/SchemaObject/InterfaceObject.php new file mode 100644 index 0000000..05167b9 --- /dev/null +++ b/src/SchemaObject/InterfaceObject.php @@ -0,0 +1,27 @@ +implementations[$implementationTypeClassName])) { + $implementationType = new $implementationTypeClassName(); + $fragment = new InlineFragment($implementationType::OBJECT_NAME, $implementationType); + $this->selectField($fragment); + $this->implementations[$implementationTypeClassName] = $implementationType; + } + + return $this->implementations[$implementationTypeClassName]; + } +} diff --git a/tests/InterfaceObjectTest.php b/tests/InterfaceObjectTest.php new file mode 100644 index 0000000..9e94211 --- /dev/null +++ b/tests/InterfaceObjectTest.php @@ -0,0 +1,84 @@ +onType1()->selectScalar(); + $object->onType2()->selectAnotherScalar(); + $object->onType2()->selectScalar(); + $this->assertEquals( + 'query { +interface { +... on Type1 { +scalar +} +... on Type2 { +anotherScalar +scalar +} +} +}', + (string) $object->getQuery()); + } +} + +class SimpleInterfaceObject extends InterfaceObject +{ + const OBJECT_NAME = 'Simple'; + + + + public function onType1(): InterfaceType1QueryObject + { + return $this->addImplementation(InterfaceType1QueryObject::class); + } + + public function onType2(): InterfaceType2QueryObject + { + return $this->addImplementation(InterfaceType2QueryObject::class); + } +} + +abstract class InterfaceSimpleSubTypeQueryObject extends QueryObject +{ + public function selectScalar() + { + $this->selectField('scalar'); + + return $this; + } + + public function selectAnotherScalar() + { + $this->selectField('anotherScalar'); + + return $this; + } +} + +class InterfaceType1QueryObject extends InterfaceSimpleSubTypeQueryObject +{ + const OBJECT_NAME = 'Type1'; +} + +class InterfaceType2QueryObject extends InterfaceSimpleSubTypeQueryObject +{ + const OBJECT_NAME = 'Type2'; +} + diff --git a/tests/SchemaClassGeneratorTest.php b/tests/SchemaClassGeneratorTest.php index ab6ed5f..b2d6568 100644 --- a/tests/SchemaClassGeneratorTest.php +++ b/tests/SchemaClassGeneratorTest.php @@ -887,6 +887,104 @@ public function testGenerateUnionObject() ); } + /** + * @covers \GraphQL\SchemaGenerator\SchemaClassGenerator::generateInterfaceObject + */ + public function testGenerateInterfaceObject() + { + $objectName = 'TestInterface'; + + // Add mock responses + $this->mockHandler->append(new Response(200, [], json_encode([ + 'data' => [ + '__type' => [ + 'name' => 'InterfaceObject1', + 'kind' => FieldTypeKindEnum::OBJECT, + 'interfaces' => [ + ['name' => $objectName], + ], + 'fields' => [ + [ + 'name' => 'value', + 'description' => null, + 'isDeprecated' => false, + 'deprecationReason' => null, + 'type' => [ + 'name' => 'String', + 'kind' => FieldTypeKindEnum::SCALAR, + 'description' => null, + 'ofType' => null, + ], + 'args' => null, + ], + ], + ] + ] + ]))); + $this->mockHandler->append(new Response(200, [], json_encode([ + 'data' => [ + '__type' => [ + 'name' => $objectName, + 'kind' => FieldTypeKindEnum::INTERFACE_OBJECT, + 'fields' => [ + [ + 'name' => 'interface_field', + 'description' => null, + 'isDeprecated' => false, + 'deprecationReason' => null, + 'type' => [ + 'name' => 'String', + 'kind' => FieldTypeKindEnum::SCALAR, + 'description' => null, + 'ofType' => null, + ], + 'args' => null, + ] + ], + 'possibleTypes' => [ + [ + 'kind' => FieldTypeKindEnum::OBJECT, + 'name' => 'InterfaceObject1', + ], [ + 'kind' => FieldTypeKindEnum::OBJECT, + 'name' => 'InterfaceObject2', + ], + ] + ] + ] + ]))); + $this->mockHandler->append(new Response(200, [], json_encode([ + 'data' => [ + '__type' => [ + 'name' => 'InterfaceObject2', + 'kind' => FieldTypeKindEnum::OBJECT, + 'interfaces' => [ + ['name' => $objectName], + ], + 'fields' => [], + ] + ] + ]))); + + $this->classGenerator->generateObject('InterfaceObject1', FieldTypeKindEnum::INTERFACE_OBJECT); + $this->classGenerator->generateObject($objectName, FieldTypeKindEnum::INTERFACE_OBJECT); + $this->classGenerator->generateObject('InterfaceObject2', FieldTypeKindEnum::INTERFACE_OBJECT); + + $objectName .= 'QueryObject'; + $this->assertFileEquals( + static::getExpectedFilesDir() . "/interface_objects/InterfaceObject1QueryObject.php", + static::getGeneratedFilesDir() . "/InterfaceObject1QueryObject.php" + ); + $this->assertFileEquals( + static::getExpectedFilesDir() . "/interface_objects/InterfaceObject2QueryObject.php", + static::getGeneratedFilesDir() . "/InterfaceObject2QueryObject.php" + ); + $this->assertFileEquals( + static::getExpectedFilesDir() . "/interface_objects/$objectName.php", + static::getGeneratedFilesDir() . "/$objectName.php" + ); + } + ///** // * @covers \GraphQL\SchemaGenerator\SchemaClassGenerator::generateObject // */ diff --git a/tests/UnionObjectTest.php b/tests/UnionObjectTest.php index ae1a5de..111de4a 100644 --- a/tests/UnionObjectTest.php +++ b/tests/UnionObjectTest.php @@ -45,7 +45,7 @@ class SimpleUnionObject extends UnionObject public function onType1() { - $object = new Type1QueryObject(); + $object = new UnionType1QueryObject(); $this->addPossibleType($object); @@ -54,7 +54,7 @@ public function onType1() public function onType2() { - $object = new Type2QueryObject(); + $object = new UnionType2QueryObject(); $this->addPossibleType($object); @@ -62,7 +62,7 @@ public function onType2() } } -abstract class SimpleSubTypeQueryObject extends QueryObject +abstract class UnionSimpleSubTypeQueryObject extends QueryObject { public function selectScalar() { @@ -79,12 +79,12 @@ public function selectAnotherScalar() } } -class Type1QueryObject extends SimpleSubTypeQueryObject +class UnionType1QueryObject extends UnionSimpleSubTypeQueryObject { const OBJECT_NAME = 'Type1'; } -class Type2QueryObject extends SimpleSubTypeQueryObject +class UnionType2QueryObject extends UnionSimpleSubTypeQueryObject { const OBJECT_NAME = 'Type2'; } diff --git a/tests/files_expected/interface_objects/InterfaceObject1QueryObject.php b/tests/files_expected/interface_objects/InterfaceObject1QueryObject.php new file mode 100644 index 0000000..e81ab3b --- /dev/null +++ b/tests/files_expected/interface_objects/InterfaceObject1QueryObject.php @@ -0,0 +1,17 @@ +selectField("value"); + + return $this; + } +} diff --git a/tests/files_expected/interface_objects/InterfaceObject2QueryObject.php b/tests/files_expected/interface_objects/InterfaceObject2QueryObject.php new file mode 100644 index 0000000..e0ba23a --- /dev/null +++ b/tests/files_expected/interface_objects/InterfaceObject2QueryObject.php @@ -0,0 +1,10 @@ +addImplementation(InterfaceObject1QueryObject::class); + } + + public function onInterfaceObject2(): InterfaceObject2QueryObject + { + return $this->addImplementation(InterfaceObject2QueryObject::class); + } + + public function selectInterfaceField() + { + $this->selectField("interface_field"); + + return $this; + } +} From c3e78ca9cc42ccaace810a8fc1cc8249fec98453 Mon Sep 17 00:00:00 2001 From: Jorrit Schippers Date: Fri, 23 Aug 2024 15:02:08 +0200 Subject: [PATCH 3/6] Add support for mutations --- .../CodeGenerator/QueryObjectClassBuilder.php | 36 +++++-- src/SchemaGenerator/SchemaClassGenerator.php | 95 ++++++++++++------- src/SchemaGenerator/SchemaInspector.php | 7 +- src/SchemaObject/QueryObject.php | 7 ++ tests/SchemaClassGeneratorTest.php | 37 +++++++- .../mutation_objects/RootMutationObject.php | 17 ++++ 6 files changed, 154 insertions(+), 45 deletions(-) create mode 100644 tests/files_expected/mutation_objects/RootMutationObject.php diff --git a/src/SchemaGenerator/CodeGenerator/QueryObjectClassBuilder.php b/src/SchemaGenerator/CodeGenerator/QueryObjectClassBuilder.php index 087d01f..7a53b9e 100644 --- a/src/SchemaGenerator/CodeGenerator/QueryObjectClassBuilder.php +++ b/src/SchemaGenerator/CodeGenerator/QueryObjectClassBuilder.php @@ -3,6 +3,7 @@ namespace GraphQL\SchemaGenerator\CodeGenerator; use GraphQL\Enumeration\FieldTypeKindEnum; +use GraphQL\Mutation; use GraphQL\SchemaGenerator\CodeGenerator\CodeFile\ClassFile; use GraphQL\SchemaObject\QueryObject; use GraphQL\Util\StringLiteralFormatter; @@ -14,6 +15,11 @@ */ class QueryObjectClassBuilder extends ObjectClassBuilder { + /** + * @var bool + */ + private $isRootMutation = false; + /** * QueryObjectClassBuilder constructor. * @@ -23,7 +29,13 @@ class QueryObjectClassBuilder extends ObjectClassBuilder */ public function __construct(string $writeDir, string $objectName, string $namespace = self::DEFAULT_NAMESPACE) { - $className = $objectName . 'QueryObject'; + if ($objectName === QueryObject::ROOT_MUTATION_OBJECT_NAME) { + $objectName = ''; + $className = 'RootMutationObject'; + $this->isRootMutation = true; + } else { + $className = $objectName . 'QueryObject'; + } $this->classFile = new ClassFile($writeDir, $className); $this->classFile->setNamespace($namespace); @@ -37,6 +49,16 @@ public function __construct(string $writeDir, string $objectName, string $namesp $objectName = ''; } $this->classFile->addConstant('OBJECT_NAME', $objectName); + + if ($this->isRootMutation) { + $this->classFile->addImport(Mutation::class); + $constructor = 'public function __construct() +{ + parent::__construct(); + $this->query = new Mutation(); +}'; + $this->classFile->addMethod($constructor); + } } /** @@ -68,11 +90,12 @@ public function addObjectField(string $fieldName, string $typeName, string $type * @param bool $isDeprecated * @param string|null $deprecationReason */ - protected function addSimpleSelector(string $propertyName, string $upperCamelName, bool $isDeprecated, ?string $deprecationReason) + protected function addSimpleSelector(string $fieldName, string $upperCamelName, bool $isDeprecated, ?string $deprecationReason) { - $method = "public function select$upperCamelName() + $methodName = $this->isRootMutation ? $fieldName : 'select' . $upperCamelName; + $method = "public function $methodName() { - \$this->selectField(\"$propertyName\"); + \$this->selectField(\"$fieldName\"); return \$this; }"; @@ -90,10 +113,11 @@ protected function addSimpleSelector(string $propertyName, string $upperCamelNam */ protected function addObjectSelector(string $fieldName, string $upperCamelName, string $fieldTypeName, string $fieldTypeKind, ?string $argsObjectName, bool $isDeprecated, ?string $deprecationReason) { + $methodName = $this->isRootMutation ? $fieldName : 'select' . $upperCamelName; $objectClass = $fieldTypeName . ($fieldTypeKind === FieldTypeKindEnum::UNION_OBJECT ? 'UnionObject' : 'QueryObject'); if ($argsObjectName === null) { - $method = "public function select$upperCamelName() + $method = "public function $methodName() { \$object = new $objectClass(\"$fieldName\"); \$this->selectField(\$object); @@ -101,7 +125,7 @@ protected function addObjectSelector(string $fieldName, string $upperCamelName, return \$object; }"; } else { - $method = "public function select$upperCamelName($argsObjectName \$argsObject = null) + $method = "public function $methodName($argsObjectName \$argsObject = null) { \$object = new $objectClass(\"$fieldName\"); if (\$argsObject !== null) { diff --git a/src/SchemaGenerator/SchemaClassGenerator.php b/src/SchemaGenerator/SchemaClassGenerator.php index c37348d..be4a0bd 100644 --- a/src/SchemaGenerator/SchemaClassGenerator.php +++ b/src/SchemaGenerator/SchemaClassGenerator.php @@ -9,6 +9,7 @@ use GraphQL\SchemaGenerator\CodeGenerator\InputObjectClassBuilder; use GraphQL\SchemaGenerator\CodeGenerator\InterfaceObjectBuilder; use GraphQL\SchemaGenerator\CodeGenerator\ObjectBuilderInterface; +use GraphQL\SchemaGenerator\CodeGenerator\ObjectClassBuilder; use GraphQL\SchemaGenerator\CodeGenerator\QueryObjectClassBuilder; use GraphQL\SchemaGenerator\CodeGenerator\UnionObjectBuilder; use GraphQL\SchemaObject\QueryObject; @@ -32,12 +33,12 @@ class SchemaClassGenerator /** * @var string */ - private $writeDir; + private $writeDir; /** * @var string */ - private $generationNamespace; + private $generationNamespace; /** * This array is used as a set to store the already generated objects @@ -54,24 +55,53 @@ class SchemaClassGenerator * @param string $writeDir * @param string $namespace */ - public function __construct(Client $client, string $writeDir = '', string $namespace = ObjectBuilderInterface::DEFAULT_NAMESPACE) + public function __construct(Client $client, string $writeDir = '', string $namespace = ObjectBuilderInterface::DEFAULT_NAMESPACE) { - $this->schemaInspector = new SchemaInspector($client); - $this->generatedObjects = []; - $this->writeDir = $writeDir; + $this->schemaInspector = new SchemaInspector($client); + $this->generatedObjects = []; + $this->writeDir = $writeDir; $this->generationNamespace = $namespace; $this->setWriteDir(); } + public function generateRootObjects(): bool + { + $this->generateRootQueryObject(); + $this->generateRootMutationObject(); + + return true; + } + /** * @return bool */ - public function generateRootQueryObject(): bool - { - $objectArray = $this->schemaInspector->getQueryTypeSchema(); + public function generateRootQueryObject(): bool + { + $objectArray = $this->schemaInspector->getRootSchema('query'); $rootObjectName = QueryObject::ROOT_QUERY_OBJECT_NAME; - $queryTypeName = $objectArray['name']; - //$rootObjectDescr = $objectArray['description']; + $queryTypeName = $objectArray['name']; + + if (array_key_exists($queryTypeName, $this->generatedObjects)) { + return true; + } + + $this->generatedObjects[$queryTypeName] = true; + + $queryObjectBuilder = new QueryObjectClassBuilder($this->writeDir, $rootObjectName, $this->generationNamespace); + $this->appendObjectFields($queryObjectBuilder, $rootObjectName, $objectArray['fields']); + $queryObjectBuilder->build(); + + return true; + } + + /** + * @return bool + */ + public function generateRootMutationObject(): bool + { + $objectArray = $this->schemaInspector->getRootSchema('mutation'); + $rootObjectName = QueryObject::ROOT_MUTATION_OBJECT_NAME; + $queryTypeName = $objectArray['name']; if (array_key_exists($queryTypeName, $this->generatedObjects)) { return true; @@ -80,7 +110,7 @@ public function generateRootQueryObject(): bool $this->generatedObjects[$queryTypeName] = true; $queryObjectBuilder = new QueryObjectClassBuilder($this->writeDir, $rootObjectName, $this->generationNamespace); - $this->appendQueryObjectFields($queryObjectBuilder, $rootObjectName, $objectArray['fields']); + $this->appendObjectFields($queryObjectBuilder, $rootObjectName, $objectArray['fields']); $queryObjectBuilder->build(); return true; @@ -89,18 +119,17 @@ public function generateRootQueryObject(): bool /** * This method receives the array of object fields as an input and adds the fields to the query object building * - * @param QueryObjectClassBuilder $queryObjectBuilder - * @param string $currentTypeName - * @param array $fieldsArray + * @param ObjectClassBuilder $queryObjectBuilder + * @param string $currentTypeName + * @param array $fieldsArray */ - private function appendQueryObjectFields(QueryObjectClassBuilder $queryObjectBuilder, string $currentTypeName, array $fieldsArray) + private function appendObjectFields(ObjectClassBuilder $queryObjectBuilder, string $currentTypeName, array $fieldsArray) { foreach ($fieldsArray as $fieldArray) { $name = $fieldArray['name']; - // Skip fields with name "query" - if ($name === 'query') continue; + // Skip fields with name "query" or "field" + if ($name === 'query' || $name === 'field') continue; - //$description = $fieldArray['description']; [$typeName, $typeKind] = $this->getTypeInfo($fieldArray); if ($typeKind === FieldTypeKindEnum::SCALAR) { @@ -168,8 +197,8 @@ protected function generateQueryObject(string $objectName): bool } $this->generatedObjects[$objectName] = true; - $objectArray = $this->schemaInspector->getObjectSchema($objectName); - $objectName = $objectArray['name']; + $objectArray = $this->schemaInspector->getObjectSchema($objectName); + $objectName = $objectArray['name']; if ($objectArray['kind'] === FieldTypeKindEnum::INTERFACE_OBJECT) { $objectBuilder = new InterfaceObjectBuilder($this->writeDir, $objectName, $this->generationNamespace); @@ -180,7 +209,7 @@ protected function generateQueryObject(string $objectName): bool $objectBuilder = new QueryObjectClassBuilder($this->writeDir, $objectName, $this->generationNamespace); } - $this->appendQueryObjectFields($objectBuilder, $objectName, $objectArray['fields']); + $this->appendObjectFields($objectBuilder, $objectName, $objectArray['fields']); $objectBuilder->build(); return true; @@ -198,8 +227,8 @@ protected function generateInputObject(string $objectName): bool } $this->generatedObjects[$objectName] = true; - $objectArray = $this->schemaInspector->getInputObjectSchema($objectName); - $objectName = $objectArray['name']; + $objectArray = $this->schemaInspector->getInputObjectSchema($objectName); + $objectName = $objectArray['name']; $objectBuilder = new InputObjectClassBuilder($this->writeDir, $objectName, $this->generationNamespace); foreach ($objectArray['inputFields'] as $inputFieldArray) { @@ -244,12 +273,12 @@ protected function generateEnumObject(string $objectName): bool $this->generatedObjects[$objectName] = true; - $objectArray = $this->schemaInspector->getEnumObjectSchema($objectName); - $objectName = $objectArray['name']; + $objectArray = $this->schemaInspector->getEnumObjectSchema($objectName); + $objectName = $objectArray['name']; $objectBuilder = new EnumObjectBuilder($this->writeDir, $objectName, $this->generationNamespace); foreach ($objectArray['enumValues'] as $enumValue) { - $name = $enumValue['name']; + $name = $enumValue['name']; //$description = $enumValue['description']; $objectBuilder->addEnumValue($name); } @@ -271,8 +300,8 @@ protected function generateUnionObject(string $objectName): bool $this->generatedObjects[$objectName] = true; - $objectArray = $this->schemaInspector->getUnionObjectSchema($objectName); - $objectName = $objectArray['name']; + $objectArray = $this->schemaInspector->getUnionObjectSchema($objectName); + $objectName = $objectArray['name']; $objectBuilder = new UnionObjectBuilder($this->writeDir, $objectName, $this->generationNamespace); foreach ($objectArray['possibleTypes'] as $possibleType) { @@ -297,8 +326,8 @@ protected function generateInterfaceObject(string $objectName): bool $this->generatedObjects[$objectName] = true; - $objectArray = $this->schemaInspector->getObjectSchema($objectName); - $objectName = $objectArray['name']; + $objectArray = $this->schemaInspector->getObjectSchema($objectName); + $objectName = $objectArray['name']; $objectBuilder = new UnionObjectBuilder($this->writeDir, $objectName, $this->generationNamespace); foreach ($objectArray['possibleTypes'] as $possibleType) { @@ -312,7 +341,7 @@ protected function generateInterfaceObject(string $objectName): bool /** * @param string $argsObjectName - * @param array $arguments + * @param array $arguments * * @return bool */ @@ -382,7 +411,7 @@ protected function getTypeInfo(array $dataArray): array /** * Sets the write directory if it's not set for the class */ - private function setWriteDir(): void + private function setWriteDir(): void { if ($this->writeDir !== '') return; diff --git a/src/SchemaGenerator/SchemaInspector.php b/src/SchemaGenerator/SchemaInspector.php index 9c80e32..d9fb815 100644 --- a/src/SchemaGenerator/SchemaInspector.php +++ b/src/SchemaGenerator/SchemaInspector.php @@ -54,13 +54,14 @@ public function __construct(Client $client) } /** + * @param string $type query or mutation * @return array */ - public function getQueryTypeSchema(): array + public function getRootSchema(string $type): array { $schemaQuery = "{ __schema{ - queryType{ + ${type}Type{ name kind description @@ -82,7 +83,7 @@ public function getQueryTypeSchema(): array }"; $response = $this->client->runRawQuery($schemaQuery, true); - return $response->getData()['__schema']['queryType']; + return $response->getData()['__schema'][$type.'Type']; } /** diff --git a/src/SchemaObject/QueryObject.php b/src/SchemaObject/QueryObject.php index 42b54b4..d1adcd3 100644 --- a/src/SchemaObject/QueryObject.php +++ b/src/SchemaObject/QueryObject.php @@ -21,6 +21,13 @@ abstract class QueryObject extends AbstractQueryBuilder */ public const ROOT_QUERY_OBJECT_NAME = 'Root'; + /** + * This constant stores the name to be given to the root query object + * + * @var string + */ + public const ROOT_MUTATION_OBJECT_NAME = 'RootMutation'; + /** * This constant stores the name of the object name in the API definition * diff --git a/tests/SchemaClassGeneratorTest.php b/tests/SchemaClassGeneratorTest.php index b2d6568..7198fd5 100644 --- a/tests/SchemaClassGeneratorTest.php +++ b/tests/SchemaClassGeneratorTest.php @@ -616,7 +616,7 @@ public function testGenerateArgumentsObjectWithInputObjectArgs() /** * @covers \GraphQL\SchemaGenerator\SchemaClassGenerator::generateQueryObject - * @covers \GraphQL\SchemaGenerator\SchemaClassGenerator::appendQueryObjectFields + * @covers \GraphQL\SchemaGenerator\SchemaClassGenerator::appendObjectFields * @covers \GraphQL\SchemaGenerator\SchemaClassGenerator::generateObject */ public function testGenerateQueryObjectWithScalarFields() @@ -703,7 +703,7 @@ public function testGenerateQueryObjectWithScalarFields() /** * @covers \GraphQL\SchemaGenerator\SchemaClassGenerator::generateQueryObject - * @covers \GraphQL\SchemaGenerator\SchemaClassGenerator::appendQueryObjectFields + * @covers \GraphQL\SchemaGenerator\SchemaClassGenerator::appendObjectFields * @covers \GraphQL\SchemaGenerator\SchemaClassGenerator::generateObject */ public function testGenerateQueryObjectWithObjectFields() @@ -790,7 +790,7 @@ public function testGenerateQueryObjectWithObjectFields() /** * @covers \GraphQL\SchemaGenerator\SchemaClassGenerator::generateRootQueryObject */ - public function testGenerateRootObject() + public function testGenerateRootQueryObject() { $this->mockHandler->append(new Response(200, [], json_encode([ 'data' => [ @@ -813,6 +813,32 @@ public function testGenerateRootObject() ); } + /** + * @covers \GraphQL\SchemaGenerator\SchemaClassGenerator::generateRootQueryObject + */ + public function testGenerateRootMutationObject() + { + $this->mockHandler->append(new Response(200, [], json_encode([ + 'data' => [ + '__schema' => [ + 'mutationType' => [ + 'name' => 'Mutation', + 'kind' => FieldTypeKindEnum::OBJECT, + 'description' => null, + 'fields' => [] + ] + ] + ] + ]))); + $this->classGenerator->generateRootMutationObject(); + + $objectName = 'RootMutationObject'; + $this->assertFileEquals( + static::getExpectedFilesDir() . "/mutation_objects/$objectName.php", + static::getGeneratedFilesDir() . "/$objectName.php" + ); + } + /** * @covers \GraphQL\SchemaGenerator\SchemaClassGenerator::generateUnionObject */ @@ -1010,6 +1036,11 @@ public function generateRootQueryObject(): bool return parent::generateRootQueryObject(); } + public function generateRootMutationObject(): bool + { + return parent::generateRootMutationObject(); + } + public function generateQueryObject(string $objectName): bool { return parent::generateQueryObject($objectName); diff --git a/tests/files_expected/mutation_objects/RootMutationObject.php b/tests/files_expected/mutation_objects/RootMutationObject.php new file mode 100644 index 0000000..dc0fcb7 --- /dev/null +++ b/tests/files_expected/mutation_objects/RootMutationObject.php @@ -0,0 +1,17 @@ +query = new Mutation(); + } +} From 74a6a560f0b406ffff3811ffe0b9a33f3af40787 Mon Sep 17 00:00:00 2001 From: Jorrit Schippers Date: Mon, 17 Feb 2025 13:14:09 +0100 Subject: [PATCH 4/6] Fix deprecated implicit nullable parameter --- README.md | 16 ++++++++-------- .../CodeGenerator/QueryObjectClassBuilder.php | 2 +- .../query_objects/ObjectSelectorQueryObject.php | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index d0d9dc0..aa8f7f2 100644 --- a/README.md +++ b/README.md @@ -252,7 +252,7 @@ class RootQueryObject extends QueryObject { const OBJECT_NAME = "query"; - public function selectPokemons(RootPokemonsArgumentsObject $argsObject = null) + public function selectPokemons(?RootPokemonsArgumentsObject $argsObject = null) { $object = new PokemonQueryObject("pokemons"); if ($argsObject !== null) { @@ -263,7 +263,7 @@ class RootQueryObject extends QueryObject return $object; } - public function selectPokemon(RootPokemonArgumentsObject $argsObject = null) + public function selectPokemon(?RootPokemonArgumentsObject $argsObject = null) { $object = new PokemonQueryObject("pokemon"); if ($argsObject !== null) { @@ -359,7 +359,7 @@ class PokemonQueryObject extends QueryObject return $this; } - public function selectWeight(PokemonWeightArgumentsObject $argsObject = null) + public function selectWeight(?PokemonWeightArgumentsObject $argsObject = null) { $object = new PokemonDimensionQueryObject("weight"); if ($argsObject !== null) { @@ -370,7 +370,7 @@ class PokemonQueryObject extends QueryObject return $object; } - public function selectHeight(PokemonHeightArgumentsObject $argsObject = null) + public function selectHeight(?PokemonHeightArgumentsObject $argsObject = null) { $object = new PokemonDimensionQueryObject("height"); if ($argsObject !== null) { @@ -402,7 +402,7 @@ class PokemonQueryObject extends QueryObject return $this; } - public function selectAttacks(PokemonAttacksArgumentsObject $argsObject = null) + public function selectAttacks(?PokemonAttacksArgumentsObject $argsObject = null) { $object = new PokemonAttackQueryObject("attacks"); if ($argsObject !== null) { @@ -434,7 +434,7 @@ class PokemonQueryObject extends QueryObject return $this; } - public function selectEvolutions(PokemonEvolutionsArgumentsObject $argsObject = null) + public function selectEvolutions(?PokemonEvolutionsArgumentsObject $argsObject = null) { $object = new PokemonQueryObject("evolutions"); if ($argsObject !== null) { @@ -445,7 +445,7 @@ class PokemonQueryObject extends QueryObject return $object; } - public function selectEvolutionRequirements(PokemonEvolutionRequirementsArgumentsObject $argsObject = null) + public function selectEvolutionRequirements(?PokemonEvolutionRequirementsArgumentsObject $argsObject = null) { $object = new PokemonEvolutionRequirementQueryObject("evolutionRequirements"); if ($argsObject !== null) { @@ -470,4 +470,4 @@ class PokemonQueryObject extends QueryObject return $this; } } -``` \ No newline at end of file +``` diff --git a/src/SchemaGenerator/CodeGenerator/QueryObjectClassBuilder.php b/src/SchemaGenerator/CodeGenerator/QueryObjectClassBuilder.php index 7a53b9e..e12dd12 100644 --- a/src/SchemaGenerator/CodeGenerator/QueryObjectClassBuilder.php +++ b/src/SchemaGenerator/CodeGenerator/QueryObjectClassBuilder.php @@ -125,7 +125,7 @@ protected function addObjectSelector(string $fieldName, string $upperCamelName, return \$object; }"; } else { - $method = "public function $methodName($argsObjectName \$argsObject = null) + $method = "public function $methodName(?$argsObjectName \$argsObject = null) { \$object = new $objectClass(\"$fieldName\"); if (\$argsObject !== null) { diff --git a/tests/files_expected/query_objects/ObjectSelectorQueryObject.php b/tests/files_expected/query_objects/ObjectSelectorQueryObject.php index 0c42077..5dcc54a 100644 --- a/tests/files_expected/query_objects/ObjectSelectorQueryObject.php +++ b/tests/files_expected/query_objects/ObjectSelectorQueryObject.php @@ -8,7 +8,7 @@ class ObjectSelectorQueryObject extends QueryObject { const OBJECT_NAME = "ObjectSelector"; - public function selectOthers(RootOthersArgumentsObject $argsObject = null) + public function selectOthers(?RootOthersArgumentsObject $argsObject = null) { $object = new OtherQueryObject("others"); if ($argsObject !== null) { From 84a2941c83584f7058f94c13b444222d857642f4 Mon Sep 17 00:00:00 2001 From: Jorrit Schippers Date: Mon, 17 Feb 2025 13:52:59 +0100 Subject: [PATCH 5/6] Add return types in generated code --- README.md | 42 +++++++++---------- .../CodeGenerator/ObjectClassBuilder.php | 10 ++--- .../CodeGenerator/QueryObjectClassBuilder.php | 8 ++-- .../CodeGenerator/UnionObjectBuilder.php | 2 +- src/SchemaGenerator/SchemaInspector.php | 2 +- .../WithInputObjectArgArgumentsObject.php | 2 +- .../WithListArgArgumentsObject.php | 2 +- .../WithMultipleEnumArgArgumentsObject.php | 2 +- ...MultipleInputObjectArgsArgumentsObject.php | 4 +- .../WithMultipleListArgsArgumentsObject.php | 4 +- .../WithMultipleScalarArgsArgumentsObject.php | 4 +- .../WithScalarArgArgumentsObject.php | 2 +- .../WithEnumValueInputObject.php | 2 +- .../WithInputObjectValueInputObject.php | 2 +- .../WithListValueInputObject.php | 2 +- ...thMultipleInputObjectValuesInputObject.php | 4 +- .../WithMultipleListValuesInputObject.php | 4 +- .../WithMultipleScalarValuesInputObject.php | 4 +- .../WithScalarValueInputObject.php | 2 +- .../input_objects/_TestFilterInputObject.php | 8 ++-- .../InterfaceObject1QueryObject.php | 2 +- .../TestInterfaceQueryObject.php | 2 +- .../MultipleObjectSelectorsQueryObject.php | 4 +- .../MultipleSimpleSelectorsQueryObject.php | 6 +-- .../ObjectSelectorQueryObject.php | 2 +- .../SimpleSelectorQueryObject.php | 2 +- .../union_objects/UnionObject1QueryObject.php | 2 +- .../UnionTestObjectUnionObject.php | 4 +- 28 files changed, 68 insertions(+), 68 deletions(-) diff --git a/README.md b/README.md index aa8f7f2..5a81251 100644 --- a/README.md +++ b/README.md @@ -252,7 +252,7 @@ class RootQueryObject extends QueryObject { const OBJECT_NAME = "query"; - public function selectPokemons(?RootPokemonsArgumentsObject $argsObject = null) + public function selectPokemons(?RootPokemonsArgumentsObject $argsObject = null): PokemonQueryObject { $object = new PokemonQueryObject("pokemons"); if ($argsObject !== null) { @@ -263,7 +263,7 @@ class RootQueryObject extends QueryObject return $object; } - public function selectPokemon(?RootPokemonArgumentsObject $argsObject = null) + public function selectPokemon(?RootPokemonArgumentsObject $argsObject = null): PokemonQueryObject { $object = new PokemonQueryObject("pokemon"); if ($argsObject !== null) { @@ -287,7 +287,7 @@ class RootPokemonsArgumentsObject extends ArgumentsObject { protected $first; - public function setFirst($first) + public function setFirst($first): self { $this->first = $first; @@ -308,14 +308,14 @@ class RootPokemonArgumentsObject extends ArgumentsObject protected $id; protected $name; - public function setId($id) + public function setId($id): self { $this->id = $id; return $this; } - public function setName($name) + public function setName($name): self { $this->name = $name; @@ -338,28 +338,28 @@ class PokemonQueryObject extends QueryObject { const OBJECT_NAME = "Pokemon"; - public function selectId() + public function selectId(): self { $this->selectField("id"); return $this; } - public function selectNumber() + public function selectNumber(): self { $this->selectField("number"); return $this; } - public function selectName() + public function selectName(): self { $this->selectField("name"); return $this; } - public function selectWeight(?PokemonWeightArgumentsObject $argsObject = null) + public function selectWeight(?PokemonWeightArgumentsObject $argsObject = null): PokemonDimensionQueryObject { $object = new PokemonDimensionQueryObject("weight"); if ($argsObject !== null) { @@ -370,7 +370,7 @@ class PokemonQueryObject extends QueryObject return $object; } - public function selectHeight(?PokemonHeightArgumentsObject $argsObject = null) + public function selectHeight(?PokemonHeightArgumentsObject $argsObject = null): PokemonDimensionQueryObject { $object = new PokemonDimensionQueryObject("height"); if ($argsObject !== null) { @@ -381,28 +381,28 @@ class PokemonQueryObject extends QueryObject return $object; } - public function selectClassification() + public function selectClassification(): self { $this->selectField("classification"); return $this; } - public function selectTypes() + public function selectTypes(): self { $this->selectField("types"); return $this; } - public function selectResistant() + public function selectResistant(): self { $this->selectField("resistant"); return $this; } - public function selectAttacks(?PokemonAttacksArgumentsObject $argsObject = null) + public function selectAttacks(?PokemonAttacksArgumentsObject $argsObject = null): PokemonAttackQueryObject { $object = new PokemonAttackQueryObject("attacks"); if ($argsObject !== null) { @@ -413,28 +413,28 @@ class PokemonQueryObject extends QueryObject return $object; } - public function selectWeaknesses() + public function selectWeaknesses(): self { $this->selectField("weaknesses"); return $this; } - public function selectFleeRate() + public function selectFleeRate(): self { $this->selectField("fleeRate"); return $this; } - public function selectMaxCP() + public function selectMaxCP(): self { $this->selectField("maxCP"); return $this; } - public function selectEvolutions(?PokemonEvolutionsArgumentsObject $argsObject = null) + public function selectEvolutions(?PokemonEvolutionsArgumentsObject $argsObject = null): PokemonQueryObject { $object = new PokemonQueryObject("evolutions"); if ($argsObject !== null) { @@ -445,7 +445,7 @@ class PokemonQueryObject extends QueryObject return $object; } - public function selectEvolutionRequirements(?PokemonEvolutionRequirementsArgumentsObject $argsObject = null) + public function selectEvolutionRequirements(?PokemonEvolutionRequirementsArgumentsObject $argsObject = null): PokemonEvolutionRequirementQueryObject { $object = new PokemonEvolutionRequirementQueryObject("evolutionRequirements"); if ($argsObject !== null) { @@ -456,14 +456,14 @@ class PokemonQueryObject extends QueryObject return $object; } - public function selectMaxHP() + public function selectMaxHP(): self { $this->selectField("maxHP"); return $this; } - public function selectImage() + public function selectImage(): self { $this->selectField("image"); diff --git a/src/SchemaGenerator/CodeGenerator/ObjectClassBuilder.php b/src/SchemaGenerator/CodeGenerator/ObjectClassBuilder.php index f275cd3..c2c35e3 100644 --- a/src/SchemaGenerator/CodeGenerator/ObjectClassBuilder.php +++ b/src/SchemaGenerator/CodeGenerator/ObjectClassBuilder.php @@ -31,7 +31,7 @@ protected function addProperty($propertyName) protected function addScalarSetter($propertyName, $upperCamelName) { $lowerCamelName = lcfirst($upperCamelName); - $method = "public function set$upperCamelName($$lowerCamelName) + $method = "public function set$upperCamelName($$lowerCamelName): self { \$this->$propertyName = $$lowerCamelName; @@ -48,7 +48,7 @@ protected function addScalarSetter($propertyName, $upperCamelName) protected function addListSetter(string $propertyName, string $upperCamelName, string $propertyType) { $lowerCamelName = lcfirst($upperCamelName); - $method = "public function set$upperCamelName(array $$lowerCamelName) + $method = "public function set$upperCamelName(array $$lowerCamelName): self { \$this->$propertyName = $$lowerCamelName; @@ -65,7 +65,7 @@ protected function addListSetter(string $propertyName, string $upperCamelName, s protected function addEnumSetter(string $propertyName, string $upperCamelName, string $objectClass) { $lowerCamelName = lcfirst(str_replace('_', '', $objectClass)); - $method = "public function set$upperCamelName($$lowerCamelName) + $method = "public function set$upperCamelName($$lowerCamelName): self { \$this->$propertyName = new RawObject($$lowerCamelName); @@ -83,7 +83,7 @@ protected function addEnumSetter(string $propertyName, string $upperCamelName, s protected function addObjectSetter(string $propertyName, string $upperCamelName, string $objectClass) { $lowerCamelName = lcfirst(str_replace('_', '', $objectClass)); - $method = "public function set$upperCamelName($objectClass $$lowerCamelName) + $method = "public function set$upperCamelName($objectClass $$lowerCamelName): self { \$this->$propertyName = $$lowerCamelName; @@ -91,4 +91,4 @@ protected function addObjectSetter(string $propertyName, string $upperCamelName, }"; $this->classFile->addMethod($method); } -} \ No newline at end of file +} diff --git a/src/SchemaGenerator/CodeGenerator/QueryObjectClassBuilder.php b/src/SchemaGenerator/CodeGenerator/QueryObjectClassBuilder.php index e12dd12..5908560 100644 --- a/src/SchemaGenerator/CodeGenerator/QueryObjectClassBuilder.php +++ b/src/SchemaGenerator/CodeGenerator/QueryObjectClassBuilder.php @@ -85,7 +85,7 @@ public function addObjectField(string $fieldName, string $typeName, string $type } /** - * @param string $propertyName + * @param string $fieldName * @param string $upperCamelName * @param bool $isDeprecated * @param string|null $deprecationReason @@ -93,7 +93,7 @@ public function addObjectField(string $fieldName, string $typeName, string $type protected function addSimpleSelector(string $fieldName, string $upperCamelName, bool $isDeprecated, ?string $deprecationReason) { $methodName = $this->isRootMutation ? $fieldName : 'select' . $upperCamelName; - $method = "public function $methodName() + $method = "public function $methodName(): self { \$this->selectField(\"$fieldName\"); @@ -117,7 +117,7 @@ protected function addObjectSelector(string $fieldName, string $upperCamelName, $objectClass = $fieldTypeName . ($fieldTypeKind === FieldTypeKindEnum::UNION_OBJECT ? 'UnionObject' : 'QueryObject'); if ($argsObjectName === null) { - $method = "public function $methodName() + $method = "public function $methodName(): $objectClass { \$object = new $objectClass(\"$fieldName\"); \$this->selectField(\$object); @@ -125,7 +125,7 @@ protected function addObjectSelector(string $fieldName, string $upperCamelName, return \$object; }"; } else { - $method = "public function $methodName(?$argsObjectName \$argsObject = null) + $method = "public function $methodName(?$argsObjectName \$argsObject = null): $objectClass { \$object = new $objectClass(\"$fieldName\"); if (\$argsObject !== null) { diff --git a/src/SchemaGenerator/CodeGenerator/UnionObjectBuilder.php b/src/SchemaGenerator/CodeGenerator/UnionObjectBuilder.php index 750a9d7..a34b522 100644 --- a/src/SchemaGenerator/CodeGenerator/UnionObjectBuilder.php +++ b/src/SchemaGenerator/CodeGenerator/UnionObjectBuilder.php @@ -43,7 +43,7 @@ public function addPossibleType(string $typeName) { $upperCamelCaseTypeName = StringLiteralFormatter::formatUpperCamelCase($typeName); $objectClassName = $typeName . 'QueryObject'; - $method = "public function on$upperCamelCaseTypeName() + $method = "public function on$upperCamelCaseTypeName(): $objectClassName { \$object = new $objectClassName(); \$this->addPossibleType(\$object); diff --git a/src/SchemaGenerator/SchemaInspector.php b/src/SchemaGenerator/SchemaInspector.php index d9fb815..4f673d0 100644 --- a/src/SchemaGenerator/SchemaInspector.php +++ b/src/SchemaGenerator/SchemaInspector.php @@ -61,7 +61,7 @@ public function getRootSchema(string $type): array { $schemaQuery = "{ __schema{ - ${type}Type{ + {$type}Type{ name kind description diff --git a/tests/files_expected/arguments_objects/WithInputObjectArgArgumentsObject.php b/tests/files_expected/arguments_objects/WithInputObjectArgArgumentsObject.php index 21fcc28..a02e51a 100644 --- a/tests/files_expected/arguments_objects/WithInputObjectArgArgumentsObject.php +++ b/tests/files_expected/arguments_objects/WithInputObjectArgArgumentsObject.php @@ -8,7 +8,7 @@ class WithInputObjectArgArgumentsObject extends ArgumentsObject { protected $objectProperty; - public function setObjectProperty(SomeInputObject $someInputObject) + public function setObjectProperty(SomeInputObject $someInputObject): self { $this->objectProperty = $someInputObject; diff --git a/tests/files_expected/arguments_objects/WithListArgArgumentsObject.php b/tests/files_expected/arguments_objects/WithListArgArgumentsObject.php index d77ffa8..15e04c4 100644 --- a/tests/files_expected/arguments_objects/WithListArgArgumentsObject.php +++ b/tests/files_expected/arguments_objects/WithListArgArgumentsObject.php @@ -8,7 +8,7 @@ class WithListArgArgumentsObject extends ArgumentsObject { protected $listProperty; - public function setListProperty(array $listProperty) + public function setListProperty(array $listProperty): self { $this->listProperty = $listProperty; diff --git a/tests/files_expected/arguments_objects/WithMultipleEnumArgArgumentsObject.php b/tests/files_expected/arguments_objects/WithMultipleEnumArgArgumentsObject.php index 30ce55f..1e15277 100644 --- a/tests/files_expected/arguments_objects/WithMultipleEnumArgArgumentsObject.php +++ b/tests/files_expected/arguments_objects/WithMultipleEnumArgArgumentsObject.php @@ -9,7 +9,7 @@ class WithMultipleEnumArgArgumentsObject extends ArgumentsObject { protected $enumProperty; - public function setEnumProperty($some) + public function setEnumProperty($some): self { $this->enumProperty = new RawObject($some); diff --git a/tests/files_expected/arguments_objects/WithMultipleInputObjectArgsArgumentsObject.php b/tests/files_expected/arguments_objects/WithMultipleInputObjectArgsArgumentsObject.php index bcbccc2..9c20a92 100644 --- a/tests/files_expected/arguments_objects/WithMultipleInputObjectArgsArgumentsObject.php +++ b/tests/files_expected/arguments_objects/WithMultipleInputObjectArgsArgumentsObject.php @@ -9,14 +9,14 @@ class WithMultipleInputObjectArgsArgumentsObject extends ArgumentsObject protected $objectProperty; protected $another_object_property; - public function setObjectProperty(SomeInputObject $someInputObject) + public function setObjectProperty(SomeInputObject $someInputObject): self { $this->objectProperty = $someInputObject; return $this; } - public function setAnotherObjectProperty(AnotherInputObject $anotherInputObject) + public function setAnotherObjectProperty(AnotherInputObject $anotherInputObject): self { $this->another_object_property = $anotherInputObject; diff --git a/tests/files_expected/arguments_objects/WithMultipleListArgsArgumentsObject.php b/tests/files_expected/arguments_objects/WithMultipleListArgsArgumentsObject.php index 50f2b2d..4c6fb51 100644 --- a/tests/files_expected/arguments_objects/WithMultipleListArgsArgumentsObject.php +++ b/tests/files_expected/arguments_objects/WithMultipleListArgsArgumentsObject.php @@ -9,14 +9,14 @@ class WithMultipleListArgsArgumentsObject extends ArgumentsObject protected $listProperty; protected $another_list_property; - public function setListProperty(array $listProperty) + public function setListProperty(array $listProperty): self { $this->listProperty = $listProperty; return $this; } - public function setAnotherListProperty(array $anotherListProperty) + public function setAnotherListProperty(array $anotherListProperty): self { $this->another_list_property = $anotherListProperty; diff --git a/tests/files_expected/arguments_objects/WithMultipleScalarArgsArgumentsObject.php b/tests/files_expected/arguments_objects/WithMultipleScalarArgsArgumentsObject.php index 33001b8..a84b59c 100644 --- a/tests/files_expected/arguments_objects/WithMultipleScalarArgsArgumentsObject.php +++ b/tests/files_expected/arguments_objects/WithMultipleScalarArgsArgumentsObject.php @@ -9,14 +9,14 @@ class WithMultipleScalarArgsArgumentsObject extends ArgumentsObject protected $scalarProperty; protected $another_scalar_property; - public function setScalarProperty($scalarProperty) + public function setScalarProperty($scalarProperty): self { $this->scalarProperty = $scalarProperty; return $this; } - public function setAnotherScalarProperty($anotherScalarProperty) + public function setAnotherScalarProperty($anotherScalarProperty): self { $this->another_scalar_property = $anotherScalarProperty; diff --git a/tests/files_expected/arguments_objects/WithScalarArgArgumentsObject.php b/tests/files_expected/arguments_objects/WithScalarArgArgumentsObject.php index 34825ef..433cdfb 100644 --- a/tests/files_expected/arguments_objects/WithScalarArgArgumentsObject.php +++ b/tests/files_expected/arguments_objects/WithScalarArgArgumentsObject.php @@ -8,7 +8,7 @@ class WithScalarArgArgumentsObject extends ArgumentsObject { protected $scalarProperty; - public function setScalarProperty($scalarProperty) + public function setScalarProperty($scalarProperty): self { $this->scalarProperty = $scalarProperty; diff --git a/tests/files_expected/input_objects/WithEnumValueInputObject.php b/tests/files_expected/input_objects/WithEnumValueInputObject.php index 7579e62..666ce10 100644 --- a/tests/files_expected/input_objects/WithEnumValueInputObject.php +++ b/tests/files_expected/input_objects/WithEnumValueInputObject.php @@ -8,7 +8,7 @@ class WithEnumValueInputObject extends InputObject { protected $enumVal; - public function setEnumVal($enumVal) + public function setEnumVal($enumVal): self { $this->enumVal = $enumVal; diff --git a/tests/files_expected/input_objects/WithInputObjectValueInputObject.php b/tests/files_expected/input_objects/WithInputObjectValueInputObject.php index bec87cd..a33df98 100644 --- a/tests/files_expected/input_objects/WithInputObjectValueInputObject.php +++ b/tests/files_expected/input_objects/WithInputObjectValueInputObject.php @@ -8,7 +8,7 @@ class WithInputObjectValueInputObject extends InputObject { protected $inputObject; - public function setInputObject(WithListValueInputObject $withListValueInputObject) + public function setInputObject(WithListValueInputObject $withListValueInputObject): self { $this->inputObject = $withListValueInputObject; diff --git a/tests/files_expected/input_objects/WithListValueInputObject.php b/tests/files_expected/input_objects/WithListValueInputObject.php index adfa7c1..fccc58d 100644 --- a/tests/files_expected/input_objects/WithListValueInputObject.php +++ b/tests/files_expected/input_objects/WithListValueInputObject.php @@ -8,7 +8,7 @@ class WithListValueInputObject extends InputObject { protected $listOne; - public function setListOne(array $listOne) + public function setListOne(array $listOne): self { $this->listOne = $listOne; diff --git a/tests/files_expected/input_objects/WithMultipleInputObjectValuesInputObject.php b/tests/files_expected/input_objects/WithMultipleInputObjectValuesInputObject.php index 1b9f4dc..f460b09 100644 --- a/tests/files_expected/input_objects/WithMultipleInputObjectValuesInputObject.php +++ b/tests/files_expected/input_objects/WithMultipleInputObjectValuesInputObject.php @@ -9,14 +9,14 @@ class WithMultipleInputObjectValuesInputObject extends InputObject protected $inputObject; protected $inputObjectTwo; - public function setInputObject(WithListValueInputObject $withListValueInputObject) + public function setInputObject(WithListValueInputObject $withListValueInputObject): self { $this->inputObject = $withListValueInputObject; return $this; } - public function setInputObjectTwo(_TestFilterInputObject $testFilterInputObject) + public function setInputObjectTwo(_TestFilterInputObject $testFilterInputObject): self { $this->inputObjectTwo = $testFilterInputObject; diff --git a/tests/files_expected/input_objects/WithMultipleListValuesInputObject.php b/tests/files_expected/input_objects/WithMultipleListValuesInputObject.php index be4176f..687ccc0 100644 --- a/tests/files_expected/input_objects/WithMultipleListValuesInputObject.php +++ b/tests/files_expected/input_objects/WithMultipleListValuesInputObject.php @@ -9,14 +9,14 @@ class WithMultipleListValuesInputObject extends InputObject protected $listOne; protected $list_two; - public function setListOne(array $listOne) + public function setListOne(array $listOne): self { $this->listOne = $listOne; return $this; } - public function setListTwo(array $listTwo) + public function setListTwo(array $listTwo): self { $this->list_two = $listTwo; diff --git a/tests/files_expected/input_objects/WithMultipleScalarValuesInputObject.php b/tests/files_expected/input_objects/WithMultipleScalarValuesInputObject.php index b3b116e..b064dee 100644 --- a/tests/files_expected/input_objects/WithMultipleScalarValuesInputObject.php +++ b/tests/files_expected/input_objects/WithMultipleScalarValuesInputObject.php @@ -9,14 +9,14 @@ class WithMultipleScalarValuesInputObject extends InputObject protected $valOne; protected $val_two; - public function setValOne($valOne) + public function setValOne($valOne): self { $this->valOne = $valOne; return $this; } - public function setValTwo($valTwo) + public function setValTwo($valTwo): self { $this->val_two = $valTwo; diff --git a/tests/files_expected/input_objects/WithScalarValueInputObject.php b/tests/files_expected/input_objects/WithScalarValueInputObject.php index 09a3afa..6e15b8b 100644 --- a/tests/files_expected/input_objects/WithScalarValueInputObject.php +++ b/tests/files_expected/input_objects/WithScalarValueInputObject.php @@ -8,7 +8,7 @@ class WithScalarValueInputObject extends InputObject { protected $valOne; - public function setValOne($valOne) + public function setValOne($valOne): self { $this->valOne = $valOne; diff --git a/tests/files_expected/input_objects/_TestFilterInputObject.php b/tests/files_expected/input_objects/_TestFilterInputObject.php index 48c1086..651fb8b 100644 --- a/tests/files_expected/input_objects/_TestFilterInputObject.php +++ b/tests/files_expected/input_objects/_TestFilterInputObject.php @@ -11,28 +11,28 @@ class _TestFilterInputObject extends InputObject protected $ids; protected $testFilter; - public function setFirstName($firstName) + public function setFirstName($firstName): self { $this->first_name = $firstName; return $this; } - public function setLastName($lastName) + public function setLastName($lastName): self { $this->lastName = $lastName; return $this; } - public function setIds(array $ids) + public function setIds(array $ids): self { $this->ids = $ids; return $this; } - public function setTestFilter(_TestFilterInputObject $testFilterInputObject) + public function setTestFilter(_TestFilterInputObject $testFilterInputObject): self { $this->testFilter = $testFilterInputObject; diff --git a/tests/files_expected/interface_objects/InterfaceObject1QueryObject.php b/tests/files_expected/interface_objects/InterfaceObject1QueryObject.php index e81ab3b..5305f71 100644 --- a/tests/files_expected/interface_objects/InterfaceObject1QueryObject.php +++ b/tests/files_expected/interface_objects/InterfaceObject1QueryObject.php @@ -8,7 +8,7 @@ class InterfaceObject1QueryObject extends QueryObject { const OBJECT_NAME = "InterfaceObject1"; - public function selectValue() + public function selectValue(): self { $this->selectField("value"); diff --git a/tests/files_expected/interface_objects/TestInterfaceQueryObject.php b/tests/files_expected/interface_objects/TestInterfaceQueryObject.php index c3a2c89..5f8bed0 100644 --- a/tests/files_expected/interface_objects/TestInterfaceQueryObject.php +++ b/tests/files_expected/interface_objects/TestInterfaceQueryObject.php @@ -16,7 +16,7 @@ public function onInterfaceObject2(): InterfaceObject2QueryObject return $this->addImplementation(InterfaceObject2QueryObject::class); } - public function selectInterfaceField() + public function selectInterfaceField(): self { $this->selectField("interface_field"); diff --git a/tests/files_expected/query_objects/MultipleObjectSelectorsQueryObject.php b/tests/files_expected/query_objects/MultipleObjectSelectorsQueryObject.php index 603e63e..f183777 100644 --- a/tests/files_expected/query_objects/MultipleObjectSelectorsQueryObject.php +++ b/tests/files_expected/query_objects/MultipleObjectSelectorsQueryObject.php @@ -8,7 +8,7 @@ class MultipleObjectSelectorsQueryObject extends QueryObject { const OBJECT_NAME = "MultipleObjectSelectors"; - public function selectRight() + public function selectRight(): MultipleObjectSelectorsRightQueryObject { $object = new MultipleObjectSelectorsRightQueryObject("right"); $this->selectField($object); @@ -19,7 +19,7 @@ public function selectRight() /** * @deprecated */ - public function selectLeftObjects() + public function selectLeftObjects(): LeftQueryObject { $object = new LeftQueryObject("left_objects"); $this->selectField($object); diff --git a/tests/files_expected/query_objects/MultipleSimpleSelectorsQueryObject.php b/tests/files_expected/query_objects/MultipleSimpleSelectorsQueryObject.php index 01b778d..71c3112 100644 --- a/tests/files_expected/query_objects/MultipleSimpleSelectorsQueryObject.php +++ b/tests/files_expected/query_objects/MultipleSimpleSelectorsQueryObject.php @@ -8,7 +8,7 @@ class MultipleSimpleSelectorsQueryObject extends QueryObject { const OBJECT_NAME = "MultipleSimpleSelectors"; - public function selectFirstName() + public function selectFirstName(): self { $this->selectField("first_name"); @@ -18,14 +18,14 @@ public function selectFirstName() /** * @deprecated is deprecated */ - public function selectLastName() + public function selectLastName(): self { $this->selectField("last_name"); return $this; } - public function selectGender() + public function selectGender(): self { $this->selectField("gender"); diff --git a/tests/files_expected/query_objects/ObjectSelectorQueryObject.php b/tests/files_expected/query_objects/ObjectSelectorQueryObject.php index 5dcc54a..1ad518d 100644 --- a/tests/files_expected/query_objects/ObjectSelectorQueryObject.php +++ b/tests/files_expected/query_objects/ObjectSelectorQueryObject.php @@ -8,7 +8,7 @@ class ObjectSelectorQueryObject extends QueryObject { const OBJECT_NAME = "ObjectSelector"; - public function selectOthers(?RootOthersArgumentsObject $argsObject = null) + public function selectOthers(?RootOthersArgumentsObject $argsObject = null): OtherQueryObject { $object = new OtherQueryObject("others"); if ($argsObject !== null) { diff --git a/tests/files_expected/query_objects/SimpleSelectorQueryObject.php b/tests/files_expected/query_objects/SimpleSelectorQueryObject.php index c568068..068916e 100644 --- a/tests/files_expected/query_objects/SimpleSelectorQueryObject.php +++ b/tests/files_expected/query_objects/SimpleSelectorQueryObject.php @@ -8,7 +8,7 @@ class SimpleSelectorQueryObject extends QueryObject { const OBJECT_NAME = "SimpleSelector"; - public function selectName() + public function selectName(): self { $this->selectField("name"); diff --git a/tests/files_expected/union_objects/UnionObject1QueryObject.php b/tests/files_expected/union_objects/UnionObject1QueryObject.php index 77cd930..17e60a2 100644 --- a/tests/files_expected/union_objects/UnionObject1QueryObject.php +++ b/tests/files_expected/union_objects/UnionObject1QueryObject.php @@ -8,7 +8,7 @@ class UnionObject1QueryObject extends QueryObject { const OBJECT_NAME = "UnionObject1"; - public function selectUnion() + public function selectUnion(): UnionTestObjectUnionObject { $object = new UnionTestObjectUnionObject("union"); $this->selectField($object); diff --git a/tests/files_expected/union_objects/UnionTestObjectUnionObject.php b/tests/files_expected/union_objects/UnionTestObjectUnionObject.php index 5b20cdd..ba536e2 100644 --- a/tests/files_expected/union_objects/UnionTestObjectUnionObject.php +++ b/tests/files_expected/union_objects/UnionTestObjectUnionObject.php @@ -6,7 +6,7 @@ class UnionTestObjectUnionObject extends UnionObject { - public function onUnionObject1() + public function onUnionObject1(): UnionObject1QueryObject { $object = new UnionObject1QueryObject(); $this->addPossibleType($object); @@ -14,7 +14,7 @@ public function onUnionObject1() return $object; } - public function onUnionObject2() + public function onUnionObject2(): UnionObject2QueryObject { $object = new UnionObject2QueryObject(); $this->addPossibleType($object); From 47441690c3e3b67e8afeeddd47e44a43ca48b293 Mon Sep 17 00:00:00 2001 From: Jorrit Schippers Date: Mon, 17 Feb 2025 14:01:10 +0100 Subject: [PATCH 6/6] Add public modifier to consts --- README.md | 4 ++-- .../CodeGenerator/CodeFile/ClassFile.php | 2 +- src/SchemaObject/QueryObject.php | 2 +- tests/InterfaceObjectTest.php | 6 +++--- tests/QueryObjectTest.php | 2 +- tests/UnionObjectTest.php | 6 +++--- tests/files_expected/classes/ClassWithConstant.php | 2 +- tests/files_expected/classes/ClassWithEverything.php | 4 ++-- .../classes/ClassWithMultipleConstants.php | 12 ++++++------ .../enum_objects/WithConstantEnumObject.php | 2 +- .../enum_objects/WithMultipleConstantsEnumObject.php | 6 +++--- .../InterfaceObject1QueryObject.php | 2 +- .../InterfaceObject2QueryObject.php | 2 +- .../mutation_objects/RootMutationObject.php | 2 +- .../query_objects/EmptyQueryObject.php | 2 +- .../MultipleObjectSelectorsQueryObject.php | 2 +- .../MultipleSimpleSelectorsQueryObject.php | 2 +- .../query_objects/ObjectSelectorQueryObject.php | 2 +- .../files_expected/query_objects/RootQueryObject.php | 2 +- .../query_objects/SimpleSelectorQueryObject.php | 2 +- .../union_objects/UnionObject1QueryObject.php | 2 +- .../union_objects/UnionObject2QueryObject.php | 2 +- 22 files changed, 35 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 5a81251..34cc4c9 100644 --- a/README.md +++ b/README.md @@ -250,7 +250,7 @@ namespace GraphQL\SchemaObject; class RootQueryObject extends QueryObject { - const OBJECT_NAME = "query"; + public const OBJECT_NAME = "query"; public function selectPokemons(?RootPokemonsArgumentsObject $argsObject = null): PokemonQueryObject { @@ -336,7 +336,7 @@ namespace GraphQL\SchemaObject; class PokemonQueryObject extends QueryObject { - const OBJECT_NAME = "Pokemon"; + public const OBJECT_NAME = "Pokemon"; public function selectId(): self { diff --git a/src/SchemaGenerator/CodeGenerator/CodeFile/ClassFile.php b/src/SchemaGenerator/CodeGenerator/CodeFile/ClassFile.php index a6525ec..fe92ef2 100644 --- a/src/SchemaGenerator/CodeGenerator/CodeFile/ClassFile.php +++ b/src/SchemaGenerator/CodeGenerator/CodeFile/ClassFile.php @@ -188,7 +188,7 @@ protected function generateConstants(): string if (!empty($this->constants)) { foreach ($this->constants as $name => $value) { $value = $this->serializeParameterValue($value); - $string .= " const $name = $value;" . PHP_EOL; + $string .= " public const $name = $value;" . PHP_EOL; } } diff --git a/src/SchemaObject/QueryObject.php b/src/SchemaObject/QueryObject.php index d1adcd3..9a659b9 100644 --- a/src/SchemaObject/QueryObject.php +++ b/src/SchemaObject/QueryObject.php @@ -33,7 +33,7 @@ abstract class QueryObject extends AbstractQueryBuilder * * @var string */ - protected const OBJECT_NAME = ''; + public const OBJECT_NAME = ''; /** * SchemaObject constructor. diff --git a/tests/InterfaceObjectTest.php b/tests/InterfaceObjectTest.php index 9e94211..fa4b747 100644 --- a/tests/InterfaceObjectTest.php +++ b/tests/InterfaceObjectTest.php @@ -40,7 +40,7 @@ interface { class SimpleInterfaceObject extends InterfaceObject { - const OBJECT_NAME = 'Simple'; + public const OBJECT_NAME = 'Simple'; @@ -74,11 +74,11 @@ public function selectAnotherScalar() class InterfaceType1QueryObject extends InterfaceSimpleSubTypeQueryObject { - const OBJECT_NAME = 'Type1'; + public const OBJECT_NAME = 'Type1'; } class InterfaceType2QueryObject extends InterfaceSimpleSubTypeQueryObject { - const OBJECT_NAME = 'Type2'; + public const OBJECT_NAME = 'Type2'; } diff --git a/tests/QueryObjectTest.php b/tests/QueryObjectTest.php index a85f8df..eb552b5 100644 --- a/tests/QueryObjectTest.php +++ b/tests/QueryObjectTest.php @@ -145,7 +145,7 @@ public function setField($field) { class SimpleQueryObject extends QueryObject { - const OBJECT_NAME = 'Simple'; + public const OBJECT_NAME = 'Simple'; public function selectScalar() { diff --git a/tests/UnionObjectTest.php b/tests/UnionObjectTest.php index 111de4a..c23efd8 100644 --- a/tests/UnionObjectTest.php +++ b/tests/UnionObjectTest.php @@ -39,7 +39,7 @@ public function testUnionObject() class SimpleUnionObject extends UnionObject { - const OBJECT_NAME = 'Simple'; + public const OBJECT_NAME = 'Simple'; @@ -81,11 +81,11 @@ public function selectAnotherScalar() class UnionType1QueryObject extends UnionSimpleSubTypeQueryObject { - const OBJECT_NAME = 'Type1'; + public const OBJECT_NAME = 'Type1'; } class UnionType2QueryObject extends UnionSimpleSubTypeQueryObject { - const OBJECT_NAME = 'Type2'; + public const OBJECT_NAME = 'Type2'; } diff --git a/tests/files_expected/classes/ClassWithConstant.php b/tests/files_expected/classes/ClassWithConstant.php index 35f549e..e1cde08 100644 --- a/tests/files_expected/classes/ClassWithConstant.php +++ b/tests/files_expected/classes/ClassWithConstant.php @@ -2,5 +2,5 @@ class ClassWithConstant { - const CONST_ONE = "ONE"; + public const CONST_ONE = "ONE"; } diff --git a/tests/files_expected/classes/ClassWithEverything.php b/tests/files_expected/classes/ClassWithEverything.php index 3d2cb77..a84e1d6 100644 --- a/tests/files_expected/classes/ClassWithEverything.php +++ b/tests/files_expected/classes/ClassWithEverything.php @@ -13,8 +13,8 @@ class ClassWithEverything extends Base implements Intr1, Intr2 use Trait1; use Trait2; - const CONST_ONE = 1; - const CONST_TWO = ""; + public const CONST_ONE = 1; + public const CONST_TWO = ""; protected $propertyOne; protected $propertyTwo = ""; diff --git a/tests/files_expected/classes/ClassWithMultipleConstants.php b/tests/files_expected/classes/ClassWithMultipleConstants.php index d0a649b..3e54763 100644 --- a/tests/files_expected/classes/ClassWithMultipleConstants.php +++ b/tests/files_expected/classes/ClassWithMultipleConstants.php @@ -2,10 +2,10 @@ class ClassWithMultipleConstants { - const CONST_ONE = "ONE"; - const CONST_TWO = 2; - const CONST_THEE = true; - const CONST_FOUR = false; - const CONST_FIVE = ""; - const CONST_SIX = 6.6; + public const CONST_ONE = "ONE"; + public const CONST_TWO = 2; + public const CONST_THEE = true; + public const CONST_FOUR = false; + public const CONST_FIVE = ""; + public const CONST_SIX = 6.6; } diff --git a/tests/files_expected/enum_objects/WithConstantEnumObject.php b/tests/files_expected/enum_objects/WithConstantEnumObject.php index f132510..12a9cb5 100644 --- a/tests/files_expected/enum_objects/WithConstantEnumObject.php +++ b/tests/files_expected/enum_objects/WithConstantEnumObject.php @@ -6,5 +6,5 @@ class WithConstantEnumObject extends EnumObject { - const FIXED_VALUE = "fixed_value"; + public const FIXED_VALUE = "fixed_value"; } diff --git a/tests/files_expected/enum_objects/WithMultipleConstantsEnumObject.php b/tests/files_expected/enum_objects/WithMultipleConstantsEnumObject.php index ed59585..e7c2dcd 100644 --- a/tests/files_expected/enum_objects/WithMultipleConstantsEnumObject.php +++ b/tests/files_expected/enum_objects/WithMultipleConstantsEnumObject.php @@ -6,7 +6,7 @@ class WithMultipleConstantsEnumObject extends EnumObject { - const SOME_VALUE = "some_value"; - const ANOTHER_VALUE = "another_value"; - const ONEMOREVALUE = "oneMoreValue"; + public const SOME_VALUE = "some_value"; + public const ANOTHER_VALUE = "another_value"; + public const ONEMOREVALUE = "oneMoreValue"; } diff --git a/tests/files_expected/interface_objects/InterfaceObject1QueryObject.php b/tests/files_expected/interface_objects/InterfaceObject1QueryObject.php index 5305f71..00a4b83 100644 --- a/tests/files_expected/interface_objects/InterfaceObject1QueryObject.php +++ b/tests/files_expected/interface_objects/InterfaceObject1QueryObject.php @@ -6,7 +6,7 @@ class InterfaceObject1QueryObject extends QueryObject { - const OBJECT_NAME = "InterfaceObject1"; + public const OBJECT_NAME = "InterfaceObject1"; public function selectValue(): self { diff --git a/tests/files_expected/interface_objects/InterfaceObject2QueryObject.php b/tests/files_expected/interface_objects/InterfaceObject2QueryObject.php index e0ba23a..0c05e58 100644 --- a/tests/files_expected/interface_objects/InterfaceObject2QueryObject.php +++ b/tests/files_expected/interface_objects/InterfaceObject2QueryObject.php @@ -6,5 +6,5 @@ class InterfaceObject2QueryObject extends QueryObject { - const OBJECT_NAME = "InterfaceObject2"; + public const OBJECT_NAME = "InterfaceObject2"; } diff --git a/tests/files_expected/mutation_objects/RootMutationObject.php b/tests/files_expected/mutation_objects/RootMutationObject.php index dc0fcb7..d19254f 100644 --- a/tests/files_expected/mutation_objects/RootMutationObject.php +++ b/tests/files_expected/mutation_objects/RootMutationObject.php @@ -7,7 +7,7 @@ class RootMutationObject extends QueryObject { - const OBJECT_NAME = ""; + public const OBJECT_NAME = ""; public function __construct() { diff --git a/tests/files_expected/query_objects/EmptyQueryObject.php b/tests/files_expected/query_objects/EmptyQueryObject.php index ba1d616..57df904 100644 --- a/tests/files_expected/query_objects/EmptyQueryObject.php +++ b/tests/files_expected/query_objects/EmptyQueryObject.php @@ -6,5 +6,5 @@ class EmptyQueryObject extends QueryObject { - const OBJECT_NAME = "Empty"; + public const OBJECT_NAME = "Empty"; } diff --git a/tests/files_expected/query_objects/MultipleObjectSelectorsQueryObject.php b/tests/files_expected/query_objects/MultipleObjectSelectorsQueryObject.php index f183777..ae17786 100644 --- a/tests/files_expected/query_objects/MultipleObjectSelectorsQueryObject.php +++ b/tests/files_expected/query_objects/MultipleObjectSelectorsQueryObject.php @@ -6,7 +6,7 @@ class MultipleObjectSelectorsQueryObject extends QueryObject { - const OBJECT_NAME = "MultipleObjectSelectors"; + public const OBJECT_NAME = "MultipleObjectSelectors"; public function selectRight(): MultipleObjectSelectorsRightQueryObject { diff --git a/tests/files_expected/query_objects/MultipleSimpleSelectorsQueryObject.php b/tests/files_expected/query_objects/MultipleSimpleSelectorsQueryObject.php index 71c3112..fb89034 100644 --- a/tests/files_expected/query_objects/MultipleSimpleSelectorsQueryObject.php +++ b/tests/files_expected/query_objects/MultipleSimpleSelectorsQueryObject.php @@ -6,7 +6,7 @@ class MultipleSimpleSelectorsQueryObject extends QueryObject { - const OBJECT_NAME = "MultipleSimpleSelectors"; + public const OBJECT_NAME = "MultipleSimpleSelectors"; public function selectFirstName(): self { diff --git a/tests/files_expected/query_objects/ObjectSelectorQueryObject.php b/tests/files_expected/query_objects/ObjectSelectorQueryObject.php index 1ad518d..f3594c5 100644 --- a/tests/files_expected/query_objects/ObjectSelectorQueryObject.php +++ b/tests/files_expected/query_objects/ObjectSelectorQueryObject.php @@ -6,7 +6,7 @@ class ObjectSelectorQueryObject extends QueryObject { - const OBJECT_NAME = "ObjectSelector"; + public const OBJECT_NAME = "ObjectSelector"; public function selectOthers(?RootOthersArgumentsObject $argsObject = null): OtherQueryObject { diff --git a/tests/files_expected/query_objects/RootQueryObject.php b/tests/files_expected/query_objects/RootQueryObject.php index b1abbdd..6b7240a 100644 --- a/tests/files_expected/query_objects/RootQueryObject.php +++ b/tests/files_expected/query_objects/RootQueryObject.php @@ -6,5 +6,5 @@ class RootQueryObject extends QueryObject { - const OBJECT_NAME = ""; + public const OBJECT_NAME = ""; } diff --git a/tests/files_expected/query_objects/SimpleSelectorQueryObject.php b/tests/files_expected/query_objects/SimpleSelectorQueryObject.php index 068916e..e09a026 100644 --- a/tests/files_expected/query_objects/SimpleSelectorQueryObject.php +++ b/tests/files_expected/query_objects/SimpleSelectorQueryObject.php @@ -6,7 +6,7 @@ class SimpleSelectorQueryObject extends QueryObject { - const OBJECT_NAME = "SimpleSelector"; + public const OBJECT_NAME = "SimpleSelector"; public function selectName(): self { diff --git a/tests/files_expected/union_objects/UnionObject1QueryObject.php b/tests/files_expected/union_objects/UnionObject1QueryObject.php index 17e60a2..c1418d8 100644 --- a/tests/files_expected/union_objects/UnionObject1QueryObject.php +++ b/tests/files_expected/union_objects/UnionObject1QueryObject.php @@ -6,7 +6,7 @@ class UnionObject1QueryObject extends QueryObject { - const OBJECT_NAME = "UnionObject1"; + public const OBJECT_NAME = "UnionObject1"; public function selectUnion(): UnionTestObjectUnionObject { diff --git a/tests/files_expected/union_objects/UnionObject2QueryObject.php b/tests/files_expected/union_objects/UnionObject2QueryObject.php index 53aa127..10b8701 100644 --- a/tests/files_expected/union_objects/UnionObject2QueryObject.php +++ b/tests/files_expected/union_objects/UnionObject2QueryObject.php @@ -6,5 +6,5 @@ class UnionObject2QueryObject extends QueryObject { - const OBJECT_NAME = "UnionObject2"; + public const OBJECT_NAME = "UnionObject2"; }