Skip to content

Commit 0e51804

Browse files
committed
Use stateOptionsTrait to get real entity name
The resource filters were lost when using a DTO as Resource and using stateOptions with 'entityClass' to specify which is the real doctrine class.
1 parent 8c27606 commit 0e51804

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

src/GraphQl/Type/FieldsBuilder.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use ApiPlatform\Metadata\Util\PropertyInfoToTypeInfoHelper;
3232
use ApiPlatform\Metadata\Util\TypeHelper;
3333
use ApiPlatform\State\Pagination\Pagination;
34+
use ApiPlatform\State\Util\StateOptionsTrait;
3435
use GraphQL\Type\Definition\InputObjectType;
3536
use GraphQL\Type\Definition\ListOfType;
3637
use GraphQL\Type\Definition\NonNull;
@@ -55,6 +56,8 @@
5556
*/
5657
final class FieldsBuilder implements FieldsBuilderEnumInterface
5758
{
59+
use StateOptionsTrait;
60+
5861
private readonly ContextAwareTypeBuilderInterface $typeBuilder;
5962

6063
public function __construct(private readonly PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory, private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, private readonly ResourceClassResolverInterface $resourceClassResolver, private readonly TypesContainerInterface $typesContainer, ContextAwareTypeBuilderInterface $typeBuilder, private readonly TypeConverterInterface $typeConverter, private readonly ResolverFactoryInterface $resolverFactory, private readonly ContainerInterface $filterLocator, private readonly Pagination $pagination, private readonly ?NameConverterInterface $nameConverter, private readonly string $nestingSeparator, private readonly ?InflectorInterface $inflector = new Inflector())
@@ -85,7 +88,7 @@ public function getItemQueryFields(string $resourceClass, Operation $operation,
8588
return [];
8689
}
8790

88-
$fieldName = lcfirst('item_query' === $operation->getName() ? $operation->getShortName() : $operation->getName().$operation->getShortName());
91+
$fieldName = lcfirst('item_query' === $operation->getName() ? $operation->getShortName() : $operation->getName() . $operation->getShortName());
8992

9093
if ($fieldConfiguration = $this->getResourceFieldConfiguration(null, $operation->getDescription(), $operation->getDeprecationReason(), Type::nullable(Type::object($resourceClass)), $resourceClass, false, $operation)) {
9194
$args = $this->resolveResourceArgs($configuration['args'] ?? [], $operation);
@@ -107,7 +110,7 @@ public function getCollectionQueryFields(string $resourceClass, Operation $opera
107110
return [];
108111
}
109112

110-
$fieldName = lcfirst('collection_query' === $operation->getName() ? $operation->getShortName() : $operation->getName().$operation->getShortName());
113+
$fieldName = lcfirst('collection_query' === $operation->getName() ? $operation->getShortName() : $operation->getName() . $operation->getShortName());
111114

112115
if ($fieldConfiguration = $this->getResourceFieldConfiguration(null, $operation->getDescription(), $operation->getDeprecationReason(), Type::collection(Type::object(\stdClass::class), Type::object($resourceClass)), $resourceClass, false, $operation)) {
113116
$args = $this->resolveResourceArgs($configuration['args'] ?? [], $operation);
@@ -133,7 +136,7 @@ public function getMutationFields(string $resourceClass, Operation $operation):
133136
$fieldConfiguration['args'] += ['input' => $this->getResourceFieldConfiguration(null, null, $operation->getDeprecationReason(), $resourceType, $resourceClass, true, $operation)];
134137
}
135138

136-
$mutationFields[$operation->getName().$operation->getShortName()] = $fieldConfiguration ?? [];
139+
$mutationFields[$operation->getName() . $operation->getShortName()] = $fieldConfiguration ?? [];
137140

138141
return $mutationFields;
139142
}
@@ -161,7 +164,7 @@ public function getSubscriptionFields(string $resourceClass, Operation $operatio
161164
$subscriptionName = 'update';
162165
}
163166

164-
$subscriptionFields[$subscriptionName.$operation->getShortName().'Subscribe'] = $fieldConfiguration;
167+
$subscriptionFields[$subscriptionName . $operation->getShortName() . 'Subscribe'] = $fieldConfiguration;
165168

166169
return $subscriptionFields;
167170
}
@@ -404,7 +407,7 @@ private function getResourceFieldConfiguration(?string $property, ?string $field
404407
}
405408

406409
try {
407-
$isCollectionType = $type->isSatisfiedBy(fn ($t) => $t instanceof CollectionType) && ($v = TypeHelper::getCollectionValueType($type)) && TypeHelper::getClassName($v);
410+
$isCollectionType = $type->isSatisfiedBy(fn($t) => $t instanceof CollectionType) && ($v = TypeHelper::getCollectionValueType($type)) && TypeHelper::getClassName($v);
408411

409412
$valueType = $type;
410413
if ($isCollectionType) {
@@ -544,7 +547,7 @@ private function getParameterArgs(Operation $operation, array $args = []): array
544547
$flattenFields[] = ['name' => $name, 'required' => $value->getRequired(), 'description' => $value->getDescription(), 'leafs' => $values[$name], 'type' => $value->getSchema()['type'] ?? 'string'];
545548
}
546549

547-
$args[$parsedKey[0]] = $this->parameterToObjectType($flattenFields, $parsedKey[0].$operation->getShortName().$operation->getName());
550+
$args[$parsedKey[0]] = $this->parameterToObjectType($flattenFields, $parsedKey[0] . $operation->getShortName() . $operation->getName());
548551
}
549552
}
550553

@@ -606,7 +609,8 @@ private function getFilterArgs(array $args, ?string $resourceClass, string $root
606609
continue;
607610
}
608611

609-
foreach ($this->filterLocator->get($filterId)->getDescription($resourceClass) as $key => $description) {
612+
$entityClass = $this->getStateOptionsClass($resourceOperation, $resourceOperation->getClass());
613+
foreach ($this->filterLocator->get($filterId)->getDescription($entityClass) as $key => $description) {
610614
$filterType = \in_array($description['type'], TypeIdentifier::values(), true) ? Type::builtin($description['type']) : Type::object($description['type']);
611615
if (!($description['required'] ?? false)) {
612616
$filterType = Type::nullable($filterType);
@@ -615,7 +619,7 @@ private function getFilterArgs(array $args, ?string $resourceClass, string $root
615619

616620
if (str_ends_with($key, '[]')) {
617621
$graphqlFilterType = GraphQLType::listOf($graphqlFilterType);
618-
$key = substr($key, 0, -2).'_list';
622+
$key = substr($key, 0, -2) . '_list';
619623
}
620624

621625
/** @var string $key */
@@ -646,8 +650,8 @@ private function mergeFilterArgs(array $args, array $parsed, ?Operation $operati
646650
if (\is_array($value)) {
647651
$value = $this->mergeFilterArgs($args[$key] ?? [], $value);
648652
if (!isset($value['#name'])) {
649-
$name = (false === $pos = strrpos($original, '[')) ? $original : substr($original, 0, (int) $pos);
650-
$value['#name'] = ($operation ? $operation->getShortName() : '').'Filter_'.strtr($name, ['[' => '_', ']' => '', '.' => '__']);
653+
$name = (false === $pos = strrpos($original, '[')) ? $original : substr($original, 0, (int)$pos);
654+
$value['#name'] = ($operation ? $operation->getShortName() : '') . 'Filter_' . strtr($name, ['[' => '_', ']' => '', '.' => '__']);
651655
}
652656
}
653657

@@ -708,7 +712,7 @@ private function convertType(Type|LegacyType $type, bool $input, Operation $reso
708712
$graphqlType = $this->typeConverter->convertPhpType($type, $input, $rootOperation, $resourceClass, $rootResource, $property, $depth);
709713

710714
if (null === $graphqlType) {
711-
throw new InvalidTypeException(\sprintf('The type "%s" is not supported.', (string) $type));
715+
throw new InvalidTypeException(\sprintf('The type "%s" is not supported.', (string)$type));
712716
}
713717

714718
if (\is_string($graphqlType)) {
@@ -719,7 +723,7 @@ private function convertType(Type|LegacyType $type, bool $input, Operation $reso
719723
$graphqlType = $this->typesContainer->get($graphqlType);
720724
}
721725

722-
if ($type->isSatisfiedBy(fn ($t) => $t instanceof CollectionType) && ($collectionValueType = TypeHelper::getCollectionValueType($type)) && TypeHelper::getClassName($collectionValueType)) {
726+
if ($type->isSatisfiedBy(fn($t) => $t instanceof CollectionType) && ($collectionValueType = TypeHelper::getCollectionValueType($type)) && TypeHelper::getClassName($collectionValueType)) {
723727
if (!$input && !$this->isEnumClass($resourceClass) && $this->pagination->isGraphQlEnabled($resourceOperation)) {
724728
return $this->typeBuilder->getPaginatedCollectionType($graphqlType, $resourceOperation);
725729
}

0 commit comments

Comments
 (0)