|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace GraphQL\SchemaGenerator\CodeGenerator; |
| 4 | + |
| 5 | +use GraphQL\Enumeration\FieldTypeKindEnum; |
| 6 | +use GraphQL\SchemaGenerator\CodeGenerator\CodeFile\ClassFile; |
| 7 | +use GraphQL\SchemaObject\QueryObject; |
| 8 | +use GraphQL\Util\StringLiteralFormatter; |
| 9 | + |
| 10 | +/** |
| 11 | + * Class QueryObjectClassBuilder |
| 12 | + * |
| 13 | + * @package GraphQL\SchemaManager\CodeGenerator |
| 14 | + */ |
| 15 | +class MutationObjectClassBuilder extends ObjectClassBuilder |
| 16 | +{ |
| 17 | + /** |
| 18 | + * QueryObjectClassBuilder constructor. |
| 19 | + * |
| 20 | + * @param string $writeDir |
| 21 | + * @param string $objectName |
| 22 | + * @param string $namespace |
| 23 | + */ |
| 24 | + public function __construct(string $writeDir, string $objectName, string $namespace = self::DEFAULT_NAMESPACE) |
| 25 | + { |
| 26 | + $className = $objectName . 'MutationObject'; |
| 27 | + |
| 28 | + $this->classFile = new ClassFile($writeDir, $className); |
| 29 | + $this->classFile->setNamespace($namespace); |
| 30 | + if ($namespace !== self::DEFAULT_NAMESPACE) { |
| 31 | + $this->classFile->addImport('GraphQL\\SchemaObject\\MutationObject'); |
| 32 | + } |
| 33 | + $this->classFile->extendsClass('MutationObject'); |
| 34 | + |
| 35 | + // Special case for handling root query object |
| 36 | + if ($objectName === QueryObject::ROOT_QUERY_OBJECT_NAME) { |
| 37 | + $objectName = ''; |
| 38 | + } |
| 39 | + $this->classFile->addConstant('OBJECT_NAME', $objectName); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @param string $fieldName |
| 44 | + */ |
| 45 | + public function addScalarField(string $fieldName, bool $isDeprecated, ?string $deprecationReason) |
| 46 | + { |
| 47 | + $upperCamelCaseProp = StringLiteralFormatter::formatUpperCamelCase($fieldName); |
| 48 | + $this->addSimpleSelector($fieldName, $upperCamelCaseProp, $isDeprecated, $deprecationReason); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @param string $fieldName |
| 53 | + * @param string $typeName |
| 54 | + * @param string $typeKind |
| 55 | + * @param string|null $argsObjectName |
| 56 | + * @param bool $isDeprecated |
| 57 | + * @param string|null $deprecationReason |
| 58 | + */ |
| 59 | + public function addObjectField(string $fieldName, string $typeName, string $typeKind, ?string $argsObjectName, bool $isDeprecated, ?string $deprecationReason) |
| 60 | + { |
| 61 | + $upperCamelCaseProp = StringLiteralFormatter::formatUpperCamelCase($fieldName); |
| 62 | + $this->addObjectSelector($fieldName, $upperCamelCaseProp, $typeName, $typeKind, $argsObjectName, $isDeprecated, $deprecationReason); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @param string $propertyName |
| 67 | + * @param string $upperCamelName |
| 68 | + * @param bool $isDeprecated |
| 69 | + * @param string|null $deprecationReason |
| 70 | + */ |
| 71 | + protected function addSimpleSelector(string $propertyName, string $upperCamelName, bool $isDeprecated, ?string $deprecationReason) |
| 72 | + { |
| 73 | + $method = "public function select$upperCamelName() |
| 74 | +{ |
| 75 | + \$this->selectField(\"$propertyName\"); |
| 76 | +
|
| 77 | + return \$this; |
| 78 | +}"; |
| 79 | + $this->classFile->addMethod($method, $isDeprecated, $deprecationReason); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @param string $fieldName |
| 84 | + * @param string $upperCamelName |
| 85 | + * @param string $fieldTypeName |
| 86 | + * @param string $fieldTypeKind |
| 87 | + * @param string|null $argsObjectName |
| 88 | + * @param bool $isDeprecated |
| 89 | + * @param string|null $deprecationReason |
| 90 | + */ |
| 91 | + protected function addObjectSelector(string $fieldName, string $upperCamelName, string $fieldTypeName, string $fieldTypeKind, ?string $argsObjectName, bool $isDeprecated, ?string $deprecationReason) |
| 92 | + { |
| 93 | + $objectClass = $fieldTypeName . ($fieldTypeKind === FieldTypeKindEnum::UNION_OBJECT ? 'UnionObject' : 'QueryObject'); |
| 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) |
| 105 | +{ |
| 106 | + \$object = new $objectClass(\"$fieldName\"); |
| 107 | + if (\$argsObject !== null) { |
| 108 | + \$object->appendArguments(\$argsObject->toArray()); |
| 109 | + } |
| 110 | + \$this->selectField(\$object); |
| 111 | +
|
| 112 | + return \$object; |
| 113 | +}"; |
| 114 | + } |
| 115 | + |
| 116 | + $this->classFile->addMethod($method, $isDeprecated, $deprecationReason); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * This method builds the class and writes it to the file system |
| 121 | + */ |
| 122 | + public function build(): void |
| 123 | + { |
| 124 | + $this->classFile->writeFile(); |
| 125 | + } |
| 126 | +} |
0 commit comments