• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 18976437839

31 Oct 2025 02:59PM UTC coverage: 22.042% (+0.003%) from 22.039%
18976437839

push

github

soyuka
docs: changelog 4.1.26

11129 of 50490 relevant lines covered (22.04%)

23.4 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

67.61
/src/GraphQl/Type/FieldsBuilder.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\GraphQl\Type;
15

16
use ApiPlatform\GraphQl\Exception\InvalidTypeException;
17
use ApiPlatform\GraphQl\Resolver\Factory\ResolverFactoryInterface;
18
use ApiPlatform\GraphQl\Type\Definition\TypeInterface;
19
use ApiPlatform\Metadata\FilterInterface;
20
use ApiPlatform\Metadata\GraphQl\Mutation;
21
use ApiPlatform\Metadata\GraphQl\Operation;
22
use ApiPlatform\Metadata\GraphQl\Query;
23
use ApiPlatform\Metadata\GraphQl\Subscription;
24
use ApiPlatform\Metadata\InflectorInterface;
25
use ApiPlatform\Metadata\OpenApiParameterFilterInterface;
26
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
27
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
28
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
29
use ApiPlatform\Metadata\ResourceClassResolverInterface;
30
use ApiPlatform\Metadata\Util\Inflector;
31
use ApiPlatform\State\Pagination\Pagination;
32
use ApiPlatform\State\Util\StateOptionsTrait;
33
use GraphQL\Type\Definition\InputObjectType;
34
use GraphQL\Type\Definition\ListOfType;
35
use GraphQL\Type\Definition\NonNull;
36
use GraphQL\Type\Definition\NullableType;
37
use GraphQL\Type\Definition\Type as GraphQLType;
38
use GraphQL\Type\Definition\WrappingType;
39
use Psr\Container\ContainerInterface;
40
use Symfony\Component\PropertyInfo\Type;
41
use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface;
42
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
43
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
44

45
/**
46
 * Builds the GraphQL fields.
47
 *
48
 * @author Alan Poulain <contact@alanpoulain.eu>
49
 */
50
final class FieldsBuilder implements FieldsBuilderEnumInterface
51
{
52
    use StateOptionsTrait;
53

54
    private readonly ContextAwareTypeBuilderInterface $typeBuilder;
55

56
    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())
57
    {
58
        $this->typeBuilder = $typeBuilder;
26✔
59
    }
60

61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function getNodeQueryFields(): array
65
    {
66
        return [
26✔
67
            'type' => $this->typeBuilder->getNodeInterface(),
26✔
68
            'args' => [
26✔
69
                'id' => ['type' => GraphQLType::nonNull(GraphQLType::id())],
26✔
70
            ],
26✔
71
            'resolve' => ($this->resolverFactory)(),
26✔
72
        ];
26✔
73
    }
74

75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function getItemQueryFields(string $resourceClass, Operation $operation, array $configuration): array
79
    {
80
        if ($operation instanceof Query && $operation->getNested()) {
26✔
81
            return [];
26✔
82
        }
83

84
        $fieldName = lcfirst('item_query' === $operation->getName() ? ($operation->getShortName() ?? $operation->getName()) : $operation->getName().$operation->getShortName());
24✔
85

86
        if ($fieldConfiguration = $this->getResourceFieldConfiguration(null, $operation->getDescription(), $operation->getDeprecationReason(), new Type(Type::BUILTIN_TYPE_OBJECT, true, $resourceClass), $resourceClass, false, $operation)) {
24✔
87
            $args = $this->resolveResourceArgs($configuration['args'] ?? [], $operation);
24✔
88
            $extraArgs = $this->resolveResourceArgs($operation->getExtraArgs() ?? [], $operation);
24✔
89
            $configuration['args'] = $args ?: $configuration['args'] ?? ['id' => ['type' => GraphQLType::nonNull(GraphQLType::id())]] + $extraArgs;
24✔
90

91
            return [$fieldName => array_merge($fieldConfiguration, $configuration)];
24✔
92
        }
93

94
        return [];
×
95
    }
96

97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function getCollectionQueryFields(string $resourceClass, Operation $operation, array $configuration): array
101
    {
102
        if ($operation instanceof Query && $operation->getNested()) {
26✔
103
            return [];
26✔
104
        }
105

106
        $fieldName = lcfirst('collection_query' === $operation->getName() ? $operation->getShortName() : $operation->getName().$operation->getShortName());
26✔
107

108
        if ($fieldConfiguration = $this->getResourceFieldConfiguration(null, $operation->getDescription(), $operation->getDeprecationReason(), new Type(Type::BUILTIN_TYPE_OBJECT, false, null, true, null, new Type(Type::BUILTIN_TYPE_OBJECT, false, $resourceClass)), $resourceClass, false, $operation)) {
26✔
109
            $args = $this->resolveResourceArgs($configuration['args'] ?? [], $operation);
26✔
110
            $extraArgs = $this->resolveResourceArgs($operation->getExtraArgs() ?? [], $operation);
26✔
111
            $configuration['args'] = $args ?: $configuration['args'] ?? $fieldConfiguration['args'] + $extraArgs;
26✔
112

113
            return [$this->inflector->pluralize($fieldName) => array_merge($fieldConfiguration, $configuration)];
26✔
114
        }
115

116
        return [];
×
117
    }
118

119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function getMutationFields(string $resourceClass, Operation $operation): array
123
    {
124
        $mutationFields = [];
10✔
125
        $resourceType = new Type(Type::BUILTIN_TYPE_OBJECT, true, $resourceClass);
10✔
126
        $description = $operation->getDescription() ?? ucfirst("{$operation->getName()}s a {$operation->getShortName()}.");
10✔
127

128
        if ($fieldConfiguration = $this->getResourceFieldConfiguration(null, $description, $operation->getDeprecationReason(), $resourceType, $resourceClass, false, $operation)) {
10✔
129
            $fieldConfiguration['args'] += ['input' => $this->getResourceFieldConfiguration(null, null, $operation->getDeprecationReason(), $resourceType, $resourceClass, true, $operation)];
10✔
130
        }
131

132
        $mutationFields[$operation->getName().$operation->getShortName()] = $fieldConfiguration ?? [];
10✔
133

134
        return $mutationFields;
10✔
135
    }
136

137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function getSubscriptionFields(string $resourceClass, Operation $operation): array
141
    {
142
        $subscriptionFields = [];
×
143
        $resourceType = new Type(Type::BUILTIN_TYPE_OBJECT, true, $resourceClass);
×
144
        $description = $operation->getDescription() ?? \sprintf('Subscribes to the action event of a %s.', $operation->getShortName());
×
145

146
        if ($fieldConfiguration = $this->getResourceFieldConfiguration(null, $description, $operation->getDeprecationReason(), $resourceType, $resourceClass, false, $operation)) {
×
147
            $fieldConfiguration['args'] += ['input' => $this->getResourceFieldConfiguration(null, null, $operation->getDeprecationReason(), $resourceType, $resourceClass, true, $operation)];
×
148
        }
149

150
        if (!$fieldConfiguration) {
×
151
            return [];
×
152
        }
153

154
        $subscriptionName = $operation->getName();
×
155
        // TODO: 3.0 change this
156
        if ('update_subscription' === $subscriptionName) {
×
157
            $subscriptionName = 'update';
×
158
        }
159

160
        $subscriptionFields[$subscriptionName.$operation->getShortName().'Subscribe'] = $fieldConfiguration;
×
161

162
        return $subscriptionFields;
×
163
    }
164

165
    /**
166
     * {@inheritdoc}
167
     */
168
    public function getResourceObjectTypeFields(?string $resourceClass, Operation $operation, bool $input, int $depth = 0, ?array $ioMetadata = null): array
169
    {
170
        $fields = [];
26✔
171
        $idField = ['type' => GraphQLType::nonNull(GraphQLType::id())];
26✔
172
        $optionalIdField = ['type' => GraphQLType::id()];
26✔
173
        $clientMutationId = GraphQLType::string();
26✔
174
        $clientSubscriptionId = GraphQLType::string();
26✔
175

176
        if (null !== $ioMetadata && \array_key_exists('class', $ioMetadata) && null === $ioMetadata['class']) {
26✔
177
            if ($input) {
×
178
                return ['clientMutationId' => $clientMutationId];
×
179
            }
180

181
            return [];
×
182
        }
183

184
        if ($operation instanceof Subscription && $input) {
26✔
185
            return [
×
186
                'id' => $idField,
×
187
                'clientSubscriptionId' => $clientSubscriptionId,
×
188
            ];
×
189
        }
190

191
        if ('delete' === $operation->getName()) {
26✔
192
            $fields = [
×
193
                'id' => $idField,
×
194
            ];
×
195

196
            if ($input) {
×
197
                $fields['clientMutationId'] = $clientMutationId;
×
198
            }
199

200
            return $fields;
×
201
        }
202

203
        if (!$input || (!$operation->getResolver() && 'create' !== $operation->getName())) {
26✔
204
            $fields['id'] = $idField;
26✔
205
        }
206
        if ($input && $depth >= 1) {
26✔
207
            $fields['id'] = $optionalIdField;
×
208
        }
209

210
        ++$depth; // increment the depth for the call to getResourceFieldConfiguration.
26✔
211

212
        if (null !== $resourceClass) {
26✔
213
            foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $property) {
26✔
214
                $context = [
26✔
215
                    'normalization_groups' => $operation->getNormalizationContext()['groups'] ?? null,
26✔
216
                    'denormalization_groups' => $operation->getDenormalizationContext()['groups'] ?? null,
26✔
217
                ];
26✔
218
                $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $property, $context);
26✔
219
                $propertyTypes = $propertyMetadata->getBuiltinTypes();
26✔
220

221
                if (
222
                    !$propertyTypes
26✔
223
                    || (!$input && false === $propertyMetadata->isReadable())
26✔
224
                    || ($input && false === $propertyMetadata->isWritable())
26✔
225
                ) {
226
                    continue;
14✔
227
                }
228

229
                // guess union/intersect types: check each type until finding a valid one
230
                foreach ($propertyTypes as $propertyType) {
26✔
231
                    if ($fieldConfiguration = $this->getResourceFieldConfiguration($property, $propertyMetadata->getDescription(), $propertyMetadata->getDeprecationReason(), $propertyType, $resourceClass, $input, $operation, $depth, null !== $propertyMetadata->getSecurity())) {
26✔
232
                        $fields['id' === $property ? '_id' : $this->normalizePropertyName($property, $resourceClass)] = $fieldConfiguration;
26✔
233
                        // stop at the first valid type
234
                        break;
26✔
235
                    }
236
                }
237
            }
238
        }
239

240
        if ($operation instanceof Mutation && $input) {
26✔
241
            $fields['clientMutationId'] = $clientMutationId;
2✔
242
        }
243

244
        return $fields;
26✔
245
    }
246

247
    private function isEnumClass(string $resourceClass): bool
248
    {
249
        return is_a($resourceClass, \BackedEnum::class, true);
26✔
250
    }
251

252
    /**
253
     * {@inheritdoc}
254
     */
255
    public function getEnumFields(string $enumClass): array
256
    {
257
        $rEnum = new \ReflectionEnum($enumClass);
2✔
258

259
        $enumCases = [];
2✔
260
        /* @var \ReflectionEnumUnitCase|\ReflectionEnumBackedCase */
261
        foreach ($rEnum->getCases() as $rCase) {
2✔
262
            if ($rCase instanceof \ReflectionEnumBackedCase) {
2✔
263
                $enumCase = ['value' => $rCase->getBackingValue()];
2✔
264
            } else {
265
                $enumCase = ['value' => $rCase->getValue()];
×
266
            }
267

268
            $propertyMetadata = $this->propertyMetadataFactory->create($enumClass, $rCase->getName());
2✔
269
            if ($enumCaseDescription = $propertyMetadata->getDescription()) {
2✔
270
                $enumCase['description'] = $enumCaseDescription;
2✔
271
            }
272
            $enumCases[$rCase->getName()] = $enumCase;
2✔
273
        }
274

275
        return $enumCases;
2✔
276
    }
277

278
    /**
279
     * {@inheritdoc}
280
     */
281
    public function resolveResourceArgs(array $args, Operation $operation): array
282
    {
283
        foreach ($args as $id => $arg) {
26✔
284
            if (!isset($arg['type'])) {
×
285
                throw new \InvalidArgumentException(\sprintf('The argument "%s" of the custom operation "%s" in %s needs a "type" option.', $id, $operation->getName(), $operation->getShortName()));
×
286
            }
287

288
            $args[$id]['type'] = $this->typeConverter->resolveType($arg['type']);
×
289
        }
290

291
        return $args;
26✔
292
    }
293

294
    /**
295
     * Transform the result of a parse_str to a GraphQL object type.
296
     * We should consider merging getFilterArgs and this, `getFilterArgs` uses `convertType` whereas we assume that parameters have only scalar types.
297
     * Note that this method has a lower complexity then the `getFilterArgs` one.
298
     * TODO: Is there a use case with an argument being a complex type (eg: a Resource, Enum etc.)?
299
     *
300
     * @param array<array{name: string, required: bool|null, description: string|null, leafs: string|array, type: string}> $flattenFields
301
     */
302
    private function parameterToObjectType(array $flattenFields, string $name): InputObjectType
303
    {
304
        $fields = [];
2✔
305
        foreach ($flattenFields as $field) {
2✔
306
            $key = $field['name'];
2✔
307
            $type = $this->getParameterType(\in_array($field['type'], Type::$builtinTypes, true) ? new Type($field['type'], !$field['required']) : new Type('object', !$field['required'], $field['type']));
2✔
308

309
            if (\is_array($l = $field['leafs'])) {
2✔
310
                if (0 === key($l)) {
2✔
311
                    $key = $key;
2✔
312
                    $type = GraphQLType::listOf($type);
2✔
313
                } else {
314
                    $n = [];
2✔
315
                    foreach ($field['leafs'] as $l => $value) {
2✔
316
                        $n[] = ['required' => null, 'name' => $l, 'leafs' => $value, 'type' => 'string', 'description' => null];
2✔
317
                    }
318

319
                    $type = $this->parameterToObjectType($n, $key);
2✔
320
                    if (isset($fields[$key]) && ($t = $fields[$key]['type']) instanceof InputObjectType) {
2✔
321
                        $t = $fields[$key]['type'];
2✔
322
                        $t->config['fields'] = array_merge($t->config['fields'], $type->config['fields']);
2✔
323
                        $type = $t;
2✔
324
                    }
325
                }
326
            }
327

328
            if ($field['required']) {
2✔
329
                $type = GraphQLType::nonNull($type);
×
330
            }
331

332
            if (isset($fields[$key])) {
2✔
333
                if ($type instanceof ListOfType) {
2✔
334
                    $key .= '_list';
2✔
335
                }
336
            }
337

338
            $fields[$key] = ['type' => $type, 'name' => $key];
2✔
339
        }
340

341
        return new InputObjectType(['name' => $name, 'fields' => $fields]);
2✔
342
    }
343

344
    /**
345
     * A simplified version of convert type that does not support resources.
346
     */
347
    private function getParameterType(Type $type): GraphQLType
348
    {
349
        return match ($type->getBuiltinType()) {
2✔
350
            Type::BUILTIN_TYPE_BOOL => GraphQLType::boolean(),
×
351
            Type::BUILTIN_TYPE_INT => GraphQLType::int(),
×
352
            Type::BUILTIN_TYPE_FLOAT => GraphQLType::float(),
×
353
            Type::BUILTIN_TYPE_STRING => GraphQLType::string(),
2✔
354
            Type::BUILTIN_TYPE_ARRAY => GraphQLType::listOf($this->getParameterType($type->getCollectionValueTypes()[0])),
×
355
            Type::BUILTIN_TYPE_ITERABLE => GraphQLType::listOf($this->getParameterType($type->getCollectionValueTypes()[0])),
×
356
            Type::BUILTIN_TYPE_OBJECT => GraphQLType::string(),
2✔
357
            default => GraphQLType::string(),
2✔
358
        };
2✔
359
    }
360

361
    /**
362
     * Get the field configuration of a resource.
363
     *
364
     * @see http://webonyx.github.io/graphql-php/type-system/object-types/
365
     */
366
    private function getResourceFieldConfiguration(?string $property, ?string $fieldDescription, ?string $deprecationReason, Type $type, string $rootResource, bool $input, Operation $rootOperation, int $depth = 0, bool $forceNullable = false): ?array
367
    {
368
        try {
369
            $isCollectionType = $this->typeBuilder->isCollection($type);
26✔
370

371
            if (
372
                $isCollectionType
26✔
373
                && $collectionValueType = $type->getCollectionValueTypes()[0] ?? null
26✔
374
            ) {
375
                $resourceClass = $collectionValueType->getClassName();
26✔
376
            } else {
377
                $resourceClass = $type->getClassName();
26✔
378
            }
379

380
            $resourceOperation = $rootOperation;
26✔
381
            if ($resourceClass && $depth >= 1 && $this->resourceClassResolver->isResourceClass($resourceClass)) {
26✔
382
                $resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($resourceClass);
2✔
383
                $resourceOperation = $resourceMetadataCollection->getOperation($isCollectionType ? 'collection_query' : 'item_query');
2✔
384
            }
385

386
            if (!$resourceOperation instanceof Operation) {
26✔
387
                throw new \LogicException('The resource operation should be a GraphQL operation.');
×
388
            }
389

390
            $graphqlType = $this->convertType($type, $input, $resourceOperation, $rootOperation, $resourceClass ?? '', $rootResource, $property, $depth, $forceNullable);
26✔
391

392
            $graphqlWrappedType = $graphqlType;
26✔
393
            if ($graphqlType instanceof WrappingType) {
26✔
394
                if (method_exists($graphqlType, 'getInnermostType')) {
26✔
395
                    $graphqlWrappedType = $graphqlType->getInnermostType();
26✔
396
                } else {
397
                    $graphqlWrappedType = $graphqlType->getWrappedType(true);
×
398
                }
399
            }
400
            $isStandardGraphqlType = \in_array($graphqlWrappedType, GraphQLType::getStandardTypes(), true);
26✔
401
            if ($isStandardGraphqlType) {
26✔
402
                $resourceClass = '';
26✔
403
            }
404

405
            // Check mercure attribute if it's a subscription at the root level.
406
            if ($rootOperation instanceof Subscription && null === $property && !$rootOperation->getMercure()) {
26✔
407
                return null;
×
408
            }
409

410
            $args = [];
26✔
411

412
            if (!$input && !$rootOperation instanceof Mutation && !$rootOperation instanceof Subscription && !$isStandardGraphqlType) {
26✔
413
                if ($isCollectionType) {
26✔
414
                    if (!$this->isEnumClass($resourceClass) && $this->pagination->isGraphQlEnabled($resourceOperation)) {
26✔
415
                        $args = $this->getGraphQlPaginationArgs($resourceOperation);
12✔
416
                    }
417

418
                    $args = $this->getFilterArgs($args, $resourceClass, $rootResource, $resourceOperation, $rootOperation, $property, $depth);
26✔
419

420
                    // Also register parameter args in the types container
421
                    // Note: This is a workaround, for more information read the comment on the parameterToObjectType function.
422
                    foreach ($this->getParameterArgs($rootOperation) as $key => $arg) {
26✔
423
                        if ($arg instanceof InputObjectType || (\is_array($arg) && isset($arg['name']))) {
2✔
424
                            $this->typesContainer->set(\is_array($arg) ? $arg['name'] : $arg->name(), $arg);
2✔
425
                        }
426
                        $args[$key] = $arg;
2✔
427
                    }
428
                }
429
            }
430

431
            if ($isStandardGraphqlType || $input) {
26✔
432
                $resolve = null;
26✔
433
            } else {
434
                $resolve = ($this->resolverFactory)($resourceClass, $rootResource, $resourceOperation, $this->propertyMetadataFactory);
26✔
435
            }
436

437
            return [
26✔
438
                'type' => $graphqlType,
26✔
439
                'description' => $fieldDescription,
26✔
440
                'args' => $args,
26✔
441
                'resolve' => $resolve,
26✔
442
                'deprecationReason' => $deprecationReason,
26✔
443
            ];
26✔
444
        } catch (InvalidTypeException) {
2✔
445
            // just ignore invalid types
446
        }
447

448
        return null;
2✔
449
    }
450

451
    /*
452
     * This function is @experimental, read the comment on the parameterToObjectType function for additional information.
453
     * @experimental
454
     */
455
    private function getParameterArgs(Operation $operation, array $args = []): array
456
    {
457
        foreach ($operation->getParameters() ?? [] as $parameter) {
26✔
458
            $key = $parameter->getKey();
2✔
459

460
            if (!str_contains($key, ':property')) {
2✔
461
                $args[$key] = ['type' => GraphQLType::string()];
2✔
462

463
                if ($parameter->getRequired()) {
2✔
464
                    $args[$key]['type'] = GraphQLType::nonNull($args[$key]['type']);
×
465
                }
466

467
                continue;
2✔
468
            }
469

470
            if (!($filterId = $parameter->getFilter()) || !$this->filterLocator->has($filterId)) {
2✔
471
                continue;
×
472
            }
473

474
            $filter = $this->filterLocator->get($filterId);
2✔
475
            $parsedKey = explode('[:property]', $key);
2✔
476
            $flattenFields = [];
2✔
477

478
            if ($filter instanceof FilterInterface) {
2✔
479
                foreach ($filter->getDescription($operation->getClass()) as $name => $value) {
2✔
480
                    $values = [];
2✔
481
                    parse_str($name, $values);
2✔
482
                    if (isset($values[$parsedKey[0]])) {
2✔
483
                        $values = $values[$parsedKey[0]];
2✔
484
                    }
485

486
                    $name = key($values);
2✔
487
                    $flattenFields[] = ['name' => $name, 'required' => $value['required'] ?? null, 'description' => $value['description'] ?? null, 'leafs' => $values[$name], 'type' => $value['type'] ?? 'string'];
2✔
488
                }
489

490
                $args[$parsedKey[0]] = $this->parameterToObjectType($flattenFields, $parsedKey[0]);
2✔
491
            }
492

493
            if ($filter instanceof OpenApiParameterFilterInterface) {
2✔
494
                foreach ($filter->getOpenApiParameters($parameter) as $value) {
2✔
495
                    $values = [];
2✔
496
                    parse_str($value->getName(), $values);
2✔
497
                    if (isset($values[$parsedKey[0]])) {
2✔
498
                        $values = $values[$parsedKey[0]];
2✔
499
                    }
500

501
                    $name = key($values);
2✔
502
                    $flattenFields[] = ['name' => $name, 'required' => $value->getRequired(), 'description' => $value->getDescription(), 'leafs' => $values[$name], 'type' => $value->getSchema()['type'] ?? 'string'];
2✔
503
                }
504

505
                $args[$parsedKey[0]] = $this->parameterToObjectType($flattenFields, $parsedKey[0].$operation->getShortName().$operation->getName());
2✔
506
            }
507
        }
508

509
        return $args;
26✔
510
    }
511

512
    private function getGraphQlPaginationArgs(Operation $queryOperation): array
513
    {
514
        $paginationType = $this->pagination->getGraphQlPaginationType($queryOperation);
12✔
515

516
        if ('cursor' === $paginationType) {
12✔
517
            return [
12✔
518
                'first' => [
12✔
519
                    'type' => GraphQLType::int(),
12✔
520
                    'description' => 'Returns the first n elements from the list.',
12✔
521
                ],
12✔
522
                'last' => [
12✔
523
                    'type' => GraphQLType::int(),
12✔
524
                    'description' => 'Returns the last n elements from the list.',
12✔
525
                ],
12✔
526
                'before' => [
12✔
527
                    'type' => GraphQLType::string(),
12✔
528
                    'description' => 'Returns the elements in the list that come before the specified cursor.',
12✔
529
                ],
12✔
530
                'after' => [
12✔
531
                    'type' => GraphQLType::string(),
12✔
532
                    'description' => 'Returns the elements in the list that come after the specified cursor.',
12✔
533
                ],
12✔
534
            ];
12✔
535
        }
536

537
        $paginationOptions = $this->pagination->getOptions();
×
538

539
        $args = [
×
540
            $paginationOptions['page_parameter_name'] => [
×
541
                'type' => GraphQLType::int(),
×
542
                'description' => 'Returns the current page.',
×
543
            ],
×
544
        ];
×
545

546
        if ($paginationOptions['client_items_per_page']) {
×
547
            $args[$paginationOptions['items_per_page_parameter_name']] = [
×
548
                'type' => GraphQLType::int(),
×
549
                'description' => 'Returns the number of items per page.',
×
550
            ];
×
551
        }
552

553
        return $args;
×
554
    }
555

556
    private function getFilterArgs(array $args, ?string $resourceClass, string $rootResource, Operation $resourceOperation, Operation $rootOperation, ?string $property, int $depth): array
557
    {
558
        if (null === $resourceClass) {
26✔
559
            return $args;
×
560
        }
561

562
        foreach ($resourceOperation->getFilters() ?? [] as $filterId) {
26✔
563
            if (!$this->filterLocator->has($filterId)) {
×
564
                continue;
×
565
            }
566

567
            $entityClass = $this->getStateOptionsClass($resourceOperation, $resourceOperation->getClass());
×
568
            foreach ($this->filterLocator->get($filterId)->getDescription($entityClass) as $key => $description) {
×
569
                $nullable = isset($description['required']) ? !$description['required'] : true;
×
570
                $filterType = \in_array($description['type'], Type::$builtinTypes, true) ? new Type($description['type'], $nullable) : new Type('object', $nullable, $description['type']);
×
571
                $graphqlFilterType = $this->convertType($filterType, false, $resourceOperation, $rootOperation, $resourceClass, $rootResource, $property, $depth);
×
572

573
                if (str_ends_with($key, '[]')) {
×
574
                    $graphqlFilterType = GraphQLType::listOf($graphqlFilterType);
×
575
                    $key = substr($key, 0, -2).'_list';
×
576
                }
577

578
                /** @var string $key */
579
                $key = str_replace('.', $this->nestingSeparator, $key);
×
580

581
                parse_str($key, $parsed);
×
582
                if (\array_key_exists($key, $parsed) && \is_array($parsed[$key])) {
×
583
                    $parsed = [$key => ''];
×
584
                }
585
                array_walk_recursive($parsed, static function (&$v) use ($graphqlFilterType): void {
×
586
                    $v = $graphqlFilterType;
×
587
                });
×
588
                $args = $this->mergeFilterArgs($args, $parsed, $resourceOperation, $key);
×
589
            }
590
        }
591

592
        return $this->convertFilterArgsToTypes($args);
26✔
593
    }
594

595
    private function mergeFilterArgs(array $args, array $parsed, ?Operation $operation = null, string $original = ''): array
596
    {
597
        foreach ($parsed as $key => $value) {
×
598
            // Never override keys that cannot be merged
599
            if (isset($args[$key]) && !\is_array($args[$key])) {
×
600
                continue;
×
601
            }
602

603
            if (\is_array($value)) {
×
604
                $value = $this->mergeFilterArgs($args[$key] ?? [], $value);
×
605
                if (!isset($value['#name'])) {
×
606
                    $name = (false === $pos = strrpos($original, '[')) ? $original : substr($original, 0, (int) $pos);
×
607
                    $value['#name'] = ($operation ? $operation->getShortName() : '').'Filter_'.strtr($name, ['[' => '_', ']' => '', '.' => '__']);
×
608
                }
609
            }
610

611
            $args[$key] = $value;
×
612
        }
613

614
        return $args;
×
615
    }
616

617
    private function convertFilterArgsToTypes(array $args): array
618
    {
619
        foreach ($args as $key => $value) {
26✔
620
            if (strpos($key, '.')) {
12✔
621
                // Declare relations/nested fields in a GraphQL compatible syntax.
622
                $args[str_replace('.', $this->nestingSeparator, $key)] = $value;
×
623
                unset($args[$key]);
×
624
            }
625
        }
626

627
        foreach ($args as $key => $value) {
26✔
628
            if (!\is_array($value) || !isset($value['#name'])) {
12✔
629
                continue;
12✔
630
            }
631

632
            $name = $value['#name'];
×
633

634
            if ($this->typesContainer->has($name)) {
×
635
                $args[$key] = $this->typesContainer->get($name);
×
636
                continue;
×
637
            }
638

639
            unset($value['#name']);
×
640

641
            $filterArgType = GraphQLType::listOf(new InputObjectType([
×
642
                'name' => $name,
×
643
                'fields' => $this->convertFilterArgsToTypes($value),
×
644
            ]));
×
645

646
            $this->typesContainer->set($name, $filterArgType);
×
647

648
            $args[$key] = $filterArgType;
×
649
        }
650

651
        return $args;
26✔
652
    }
653

654
    /**
655
     * Converts a built-in type to its GraphQL equivalent.
656
     *
657
     * @throws InvalidTypeException
658
     */
659
    private function convertType(Type $type, bool $input, Operation $resourceOperation, Operation $rootOperation, string $resourceClass, string $rootResource, ?string $property, int $depth, bool $forceNullable = false): GraphQLType|ListOfType|NonNull
660
    {
661
        $graphqlType = $this->typeConverter->convertType($type, $input, $rootOperation, $resourceClass, $rootResource, $property, $depth);
26✔
662

663
        if (null === $graphqlType) {
26✔
664
            throw new InvalidTypeException(\sprintf('The type "%s" is not supported.', $type->getBuiltinType()));
2✔
665
        }
666

667
        if (\is_string($graphqlType)) {
26✔
668
            if (!$this->typesContainer->has($graphqlType)) {
×
669
                throw new InvalidTypeException(\sprintf('The GraphQL type %s is not valid. Valid types are: %s. Have you registered this type by implementing %s?', $graphqlType, implode(', ', array_keys($this->typesContainer->all())), TypeInterface::class));
×
670
            }
671

672
            $graphqlType = $this->typesContainer->get($graphqlType);
×
673
        }
674

675
        if ($this->typeBuilder->isCollection($type)) {
26✔
676
            if (!$input && !$this->isEnumClass($resourceClass) && $this->pagination->isGraphQlEnabled($resourceOperation)) {
26✔
677
                return $this->typeBuilder->getPaginatedCollectionType($graphqlType, $resourceOperation);
12✔
678
            }
679

680
            return GraphQLType::listOf($graphqlType);
18✔
681
        }
682

683
        return $forceNullable || !$graphqlType instanceof NullableType || $type->isNullable() || ($rootOperation instanceof Mutation && 'update' === $rootOperation->getName())
26✔
684
            ? $graphqlType
26✔
685
            : GraphQLType::nonNull($graphqlType);
26✔
686
    }
687

688
    private function normalizePropertyName(string $property, string $resourceClass): string
689
    {
690
        if (null === $this->nameConverter) {
26✔
691
            return $property;
×
692
        }
693
        if ($this->nameConverter instanceof AdvancedNameConverterInterface || $this->nameConverter instanceof MetadataAwareNameConverter) {
26✔
694
            return $this->nameConverter->normalize($property, $resourceClass);
26✔
695
        }
696

697
        return $this->nameConverter->normalize($property);
×
698
    }
699
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc