Skip to content

Commit 5503478

Browse files
authored
fix(graphql): register query parameter arguments with filters (#6727)
1 parent 99262dc commit 5503478

File tree

1 file changed

+55
-40
lines changed

1 file changed

+55
-40
lines changed

src/GraphQl/Type/FieldsBuilder.php

Lines changed: 55 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use ApiPlatform\GraphQl\Resolver\Factory\ResolverFactory;
2020
use ApiPlatform\GraphQl\Resolver\Factory\ResolverFactoryInterface;
2121
use ApiPlatform\GraphQl\Type\Definition\TypeInterface;
22+
use ApiPlatform\Metadata\FilterInterface;
2223
use ApiPlatform\Metadata\GraphQl\Mutation;
2324
use ApiPlatform\Metadata\GraphQl\Operation;
2425
use ApiPlatform\Metadata\GraphQl\Query;
@@ -292,41 +293,6 @@ public function resolveResourceArgs(array $args, Operation $operation): array
292293
$args[$id]['type'] = $this->typeConverter->resolveType($arg['type']);
293294
}
294295

295-
/*
296-
* This is @experimental, read the comment on the parameterToObjectType function as additional information.
297-
*/
298-
foreach ($operation->getParameters() ?? [] as $parameter) {
299-
$key = $parameter->getKey();
300-
301-
if (str_contains($key, ':property')) {
302-
if (!($filterId = $parameter->getFilter()) || !$this->filterLocator->has($filterId)) {
303-
continue;
304-
}
305-
306-
$parsedKey = explode('[:property]', $key);
307-
$flattenFields = [];
308-
foreach ($this->filterLocator->get($filterId)->getDescription($operation->getClass()) as $key => $value) {
309-
$values = [];
310-
parse_str($key, $values);
311-
if (isset($values[$parsedKey[0]])) {
312-
$values = $values[$parsedKey[0]];
313-
}
314-
315-
$name = key($values);
316-
$flattenFields[] = ['name' => $name, 'required' => $value['required'] ?? null, 'description' => $value['description'] ?? null, 'leafs' => $values[$name], 'type' => $value['type'] ?? 'string'];
317-
}
318-
319-
$args[$parsedKey[0]] = $this->parameterToObjectType($flattenFields, $parsedKey[0]);
320-
continue;
321-
}
322-
323-
$args[$key] = ['type' => GraphQLType::string()];
324-
325-
if ($parameter->getRequired()) {
326-
$args[$key]['type'] = GraphQLType::nonNull($args[$key]['type']);
327-
}
328-
}
329-
330296
return $args;
331297
}
332298

@@ -448,12 +414,15 @@ private function getResourceFieldConfiguration(?string $property, ?string $field
448414

449415
$args = [];
450416

451-
if (!$input && !$rootOperation instanceof Mutation && !$rootOperation instanceof Subscription && !$isStandardGraphqlType && $isCollectionType) {
452-
if (!$this->isEnumClass($resourceClass) && $this->pagination->isGraphQlEnabled($resourceOperation)) {
453-
$args = $this->getGraphQlPaginationArgs($resourceOperation);
454-
}
417+
if (!$input && !$rootOperation instanceof Mutation && !$rootOperation instanceof Subscription && !$isStandardGraphqlType) {
418+
if ($isCollectionType) {
419+
if (!$this->isEnumClass($resourceClass) && $this->pagination->isGraphQlEnabled($resourceOperation)) {
420+
$args = $this->getGraphQlPaginationArgs($resourceOperation);
421+
}
455422

456-
$args = $this->getFilterArgs($args, $resourceClass, $rootResource, $resourceOperation, $rootOperation, $property, $depth);
423+
$args = $this->getFilterArgs($args, $resourceClass, $rootResource, $resourceOperation, $rootOperation, $property, $depth);
424+
$args = $this->getParameterArgs($rootOperation, $args);
425+
}
457426
}
458427

459428
if ($this->itemResolverFactory instanceof ResolverFactory) {
@@ -488,6 +457,52 @@ private function getResourceFieldConfiguration(?string $property, ?string $field
488457
return null;
489458
}
490459

460+
/*
461+
* This function is @experimental, read the comment on the parameterToObjectType function for additional information.
462+
* @experimental
463+
*/
464+
private function getParameterArgs(Operation $operation, array $args = []): array
465+
{
466+
foreach ($operation->getParameters() ?? [] as $parameter) {
467+
$key = $parameter->getKey();
468+
469+
if (!str_contains($key, ':property')) {
470+
$args[$key] = ['type' => GraphQLType::string()];
471+
472+
if ($parameter->getRequired()) {
473+
$args[$key]['type'] = GraphQLType::nonNull($args[$key]['type']);
474+
}
475+
476+
continue;
477+
}
478+
479+
if (!($filterId = $parameter->getFilter()) || !$this->filterLocator->has($filterId)) {
480+
continue;
481+
}
482+
483+
$filter = $this->filterLocator->get($filterId);
484+
$parsedKey = explode('[:property]', $key);
485+
$flattenFields = [];
486+
487+
if ($filter instanceof FilterInterface) {
488+
foreach ($filter->getDescription($operation->getClass()) as $name => $value) {
489+
$values = [];
490+
parse_str($name, $values);
491+
if (isset($values[$parsedKey[0]])) {
492+
$values = $values[$parsedKey[0]];
493+
}
494+
495+
$name = key($values);
496+
$flattenFields[] = ['name' => $name, 'required' => $value['required'] ?? null, 'description' => $value['description'] ?? null, 'leafs' => $values[$name], 'type' => $value['type'] ?? 'string'];
497+
}
498+
499+
$args[$parsedKey[0]] = $this->parameterToObjectType($flattenFields, $parsedKey[0]);
500+
}
501+
}
502+
503+
return $args;
504+
}
505+
491506
private function getGraphQlPaginationArgs(Operation $queryOperation): array
492507
{
493508
$paginationType = $this->pagination->getGraphQlPaginationType($queryOperation);

0 commit comments

Comments
 (0)