Last active
December 5, 2024 12:33
-
-
Save savinmikhail/aa92a327b75388da92511617f4092d2b to your computer and use it in GitHub Desktop.
Fix api platform graphql issue, when properties with default values are parsed to schema as required ones
This file contains hidden or 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
| <?php | |
| declare(strict_types=1); | |
| namespace App\Main\ApiPlatform\GraphQl\Type; | |
| use ApiPlatform\GraphQl\Type\FieldsBuilderEnumInterface; | |
| use ApiPlatform\Metadata\GraphQl\Operation; | |
| use BackedEnum; | |
| use ReflectionClass; | |
| use Symfony\Component\DependencyInjection\Attribute\AsDecorator; | |
| #[AsDecorator(decorates: 'api_platform.graphql.fields_builder')] | |
| final readonly class PropertyDefaultValueFieldsBuilder implements FieldsBuilderEnumInterface | |
| { | |
| public function __construct( | |
| private FieldsBuilderEnumInterface $decorated, | |
| ) {} | |
| /** | |
| * @psalm-suppress ArgumentTypeCoercion | |
| */ | |
| public function getResourceObjectTypeFields(?string $resourceClass, Operation $operation, bool $input, int $depth = 0, ?array $ioMetadata = null): array | |
| { | |
| $fields = $this->decorated->getResourceObjectTypeFields($resourceClass, $operation, $input, $depth, $ioMetadata); | |
| if ($resourceClass !== null) { | |
| $reflection = new ReflectionClass($resourceClass); | |
| foreach ($fields as $name => &$field) { | |
| if ( | |
| !$reflection->hasProperty($name) | |
| || isset($field['defaultValue']) | |
| ) { | |
| continue; | |
| } | |
| $property = $reflection->getProperty($name); | |
| $defaultValue = $property->getDefaultValue(); | |
| if ($defaultValue) { | |
| $field['defaultValue'] = $this->convertDefaultValue($defaultValue); | |
| } | |
| } | |
| } | |
| return $fields; | |
| } | |
| public function getNodeQueryFields(): array | |
| { | |
| return $this->decorated->getNodeQueryFields(); | |
| } | |
| public function getItemQueryFields(string $resourceClass, Operation $operation, array $configuration): array | |
| { | |
| return $this->decorated->getItemQueryFields($resourceClass, $operation, $configuration); | |
| } | |
| public function getCollectionQueryFields(string $resourceClass, Operation $operation, array $configuration): array | |
| { | |
| return $this->decorated->getCollectionQueryFields($resourceClass, $operation, $configuration); | |
| } | |
| public function getMutationFields(string $resourceClass, Operation $operation): array | |
| { | |
| return $this->decorated->getMutationFields($resourceClass, $operation); | |
| } | |
| public function getSubscriptionFields(string $resourceClass, Operation $operation): array | |
| { | |
| return $this->decorated->getSubscriptionFields($resourceClass, $operation); | |
| } | |
| public function getEnumFields(string $enumClass): array | |
| { | |
| return $this->decorated->getEnumFields($enumClass); | |
| } | |
| public function resolveResourceArgs(array $args, Operation $operation): array | |
| { | |
| return $this->decorated->resolveResourceArgs($args, $operation); | |
| } | |
| private function convertDefaultValue(mixed $value): mixed | |
| { | |
| if ($value instanceof BackedEnum) { | |
| return $value->value; | |
| } | |
| return $value; | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment