|
42 | 42 | *
|
43 | 43 | * @author Alan Poulain <[email protected]>
|
44 | 44 | */
|
45 |
| -final class FieldsBuilder implements FieldsBuilderInterface |
| 45 | +final class FieldsBuilder implements FieldsBuilderInterface, FieldsBuilderEnumInterface |
46 | 46 | {
|
47 |
| - public function __construct(private readonly PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory, private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, private readonly ResourceClassResolverInterface $resourceClassResolver, private readonly TypesContainerInterface $typesContainer, private readonly TypeBuilderInterface $typeBuilder, private readonly TypeConverterInterface $typeConverter, private readonly ResolverFactoryInterface $itemResolverFactory, private readonly ResolverFactoryInterface $collectionResolverFactory, private readonly ResolverFactoryInterface $itemMutationResolverFactory, private readonly ResolverFactoryInterface $itemSubscriptionResolverFactory, private readonly ContainerInterface $filterLocator, private readonly Pagination $pagination, private readonly ?NameConverterInterface $nameConverter, private readonly string $nestingSeparator) |
| 47 | + private readonly TypeBuilderEnumInterface|TypeBuilderInterface $typeBuilder; |
| 48 | + |
| 49 | + public function __construct(private readonly PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory, private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, private readonly ResourceClassResolverInterface $resourceClassResolver, private readonly TypesContainerInterface $typesContainer, TypeBuilderEnumInterface|TypeBuilderInterface $typeBuilder, private readonly TypeConverterInterface $typeConverter, private readonly ResolverFactoryInterface $itemResolverFactory, private readonly ResolverFactoryInterface $collectionResolverFactory, private readonly ResolverFactoryInterface $itemMutationResolverFactory, private readonly ResolverFactoryInterface $itemSubscriptionResolverFactory, private readonly ContainerInterface $filterLocator, private readonly Pagination $pagination, private readonly ?NameConverterInterface $nameConverter, private readonly string $nestingSeparator) |
48 | 50 | {
|
| 51 | + if ($typeBuilder instanceof TypeBuilderInterface) { |
| 52 | + @trigger_error(sprintf('$typeBuilder argument of FieldsBuilder implementing "%s" is deprecated since API Platform 3.1. It has to implement "%s" instead.', TypeBuilderInterface::class, TypeBuilderEnumInterface::class), \E_USER_DEPRECATED); |
| 53 | + } |
| 54 | + $this->typeBuilder = $typeBuilder; |
49 | 55 | }
|
50 | 56 |
|
51 | 57 | /**
|
@@ -226,6 +232,26 @@ public function getResourceObjectTypeFields(?string $resourceClass, Operation $o
|
226 | 232 | return $fields;
|
227 | 233 | }
|
228 | 234 |
|
| 235 | + /** |
| 236 | + * {@inheritdoc} |
| 237 | + */ |
| 238 | + public function getEnumFields(string $enumClass): array |
| 239 | + { |
| 240 | + $rEnum = new \ReflectionEnum($enumClass); |
| 241 | + |
| 242 | + $enumCases = []; |
| 243 | + foreach ($rEnum->getCases() as $rCase) { |
| 244 | + $enumCase = ['value' => $rCase->getBackingValue()]; |
| 245 | + $propertyMetadata = $this->propertyMetadataFactory->create($enumClass, $rCase->getName()); |
| 246 | + if ($enumCaseDescription = $propertyMetadata->getDescription()) { |
| 247 | + $enumCase['description'] = $enumCaseDescription; |
| 248 | + } |
| 249 | + $enumCases[$rCase->getName()] = $enumCase; |
| 250 | + } |
| 251 | + |
| 252 | + return $enumCases; |
| 253 | + } |
| 254 | + |
229 | 255 | /**
|
230 | 256 | * {@inheritdoc}
|
231 | 257 | */
|
@@ -481,7 +507,16 @@ private function convertType(Type $type, bool $input, Operation $resourceOperati
|
481 | 507 | }
|
482 | 508 |
|
483 | 509 | if ($this->typeBuilder->isCollection($type)) {
|
484 |
| - return $this->pagination->isGraphQlEnabled($resourceOperation) && !$input ? $this->typeBuilder->getResourcePaginatedCollectionType($graphqlType, $resourceClass, $resourceOperation) : GraphQLType::listOf($graphqlType); |
| 510 | + if (!$input && $this->pagination->isGraphQlEnabled($resourceOperation)) { |
| 511 | + // Deprecated path, to remove in API Platform 4. |
| 512 | + if ($this->typeBuilder instanceof TypeBuilderInterface) { |
| 513 | + return $this->typeBuilder->getResourcePaginatedCollectionType($graphqlType, $resourceClass, $resourceOperation); |
| 514 | + } |
| 515 | + |
| 516 | + return $this->typeBuilder->getPaginatedCollectionType($graphqlType, $resourceOperation); |
| 517 | + } |
| 518 | + |
| 519 | + return GraphQLType::listOf($graphqlType); |
485 | 520 | }
|
486 | 521 |
|
487 | 522 | return $forceNullable || !$graphqlType instanceof NullableType || $type->isNullable() || ($rootOperation instanceof Mutation && 'update' === $rootOperation->getName())
|
|
0 commit comments