-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
312 additions
and
39 deletions.
There are no files selected for viewing
126 changes: 126 additions & 0 deletions
126
src/SchemaGenerator/CodeGenerator/MutationObjectClassBuilder.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
|
||
namespace GraphQL\SchemaGenerator\CodeGenerator; | ||
|
||
use GraphQL\Enumeration\FieldTypeKindEnum; | ||
use GraphQL\SchemaGenerator\CodeGenerator\CodeFile\ClassFile; | ||
use GraphQL\SchemaObject\QueryObject; | ||
use GraphQL\Util\StringLiteralFormatter; | ||
|
||
/** | ||
* Class QueryObjectClassBuilder | ||
* | ||
* @package GraphQL\SchemaManager\CodeGenerator | ||
*/ | ||
class MutationObjectClassBuilder extends ObjectClassBuilder | ||
{ | ||
/** | ||
* QueryObjectClassBuilder constructor. | ||
* | ||
* @param string $writeDir | ||
* @param string $objectName | ||
* @param string $namespace | ||
*/ | ||
public function __construct(string $writeDir, string $objectName, string $namespace = self::DEFAULT_NAMESPACE) | ||
{ | ||
$className = $objectName . 'MutationObject'; | ||
|
||
$this->classFile = new ClassFile($writeDir, $className); | ||
$this->classFile->setNamespace($namespace); | ||
if ($namespace !== self::DEFAULT_NAMESPACE) { | ||
$this->classFile->addImport('GraphQL\\SchemaObject\\MutationObject'); | ||
} | ||
$this->classFile->extendsClass('MutationObject'); | ||
|
||
// Special case for handling root query object | ||
if ($objectName === QueryObject::ROOT_QUERY_OBJECT_NAME) { | ||
$objectName = ''; | ||
} | ||
$this->classFile->addConstant('OBJECT_NAME', $objectName); | ||
} | ||
|
||
/** | ||
* @param string $fieldName | ||
*/ | ||
public function addScalarField(string $fieldName, bool $isDeprecated, ?string $deprecationReason) | ||
{ | ||
$upperCamelCaseProp = StringLiteralFormatter::formatUpperCamelCase($fieldName); | ||
$this->addSimpleSelector($fieldName, $upperCamelCaseProp, $isDeprecated, $deprecationReason); | ||
} | ||
|
||
/** | ||
* @param string $fieldName | ||
* @param string $typeName | ||
* @param string $typeKind | ||
* @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) | ||
{ | ||
$upperCamelCaseProp = StringLiteralFormatter::formatUpperCamelCase($fieldName); | ||
$this->addObjectSelector($fieldName, $upperCamelCaseProp, $typeName, $typeKind, $argsObjectName, $isDeprecated, $deprecationReason); | ||
} | ||
|
||
/** | ||
* @param string $propertyName | ||
* @param string $upperCamelName | ||
* @param bool $isDeprecated | ||
* @param string|null $deprecationReason | ||
*/ | ||
protected function addSimpleSelector(string $propertyName, string $upperCamelName, bool $isDeprecated, ?string $deprecationReason) | ||
{ | ||
$method = "public function select$upperCamelName() | ||
{ | ||
\$this->selectField(\"$propertyName\"); | ||
return \$this; | ||
}"; | ||
$this->classFile->addMethod($method, $isDeprecated, $deprecationReason); | ||
} | ||
|
||
/** | ||
* @param string $fieldName | ||
* @param string $upperCamelName | ||
* @param string $fieldTypeName | ||
* @param string $fieldTypeKind | ||
* @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) | ||
{ | ||
$objectClass = $fieldTypeName . ($fieldTypeKind === FieldTypeKindEnum::UNION_OBJECT ? 'UnionObject' : 'QueryObject'); | ||
|
||
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) { | ||
\$object->appendArguments(\$argsObject->toArray()); | ||
} | ||
\$this->selectField(\$object); | ||
return \$object; | ||
}"; | ||
} | ||
|
||
$this->classFile->addMethod($method, $isDeprecated, $deprecationReason); | ||
} | ||
|
||
/** | ||
* This method builds the class and writes it to the file system | ||
*/ | ||
public function build(): void | ||
{ | ||
$this->classFile->writeFile(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.