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

api-platform / core / 7047458607

30 Nov 2023 01:53PM UTC coverage: 37.263% (+0.002%) from 37.261%
7047458607

push

github

soyuka
Merge 3.2

20 of 35 new or added lines in 12 files covered. (57.14%)

10 existing lines in 3 files now uncovered.

10295 of 27628 relevant lines covered (37.26%)

21.04 hits per line

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

0.0
/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\Doctrine\Odm\State\Options as ODMOptions;
17
use ApiPlatform\Doctrine\Orm\State\Options;
18
use ApiPlatform\GraphQl\Resolver\Factory\ResolverFactory;
19
use ApiPlatform\GraphQl\Resolver\Factory\ResolverFactoryInterface;
20
use ApiPlatform\GraphQl\Type\Definition\TypeInterface;
21
use ApiPlatform\Metadata\GraphQl\Mutation;
22
use ApiPlatform\Metadata\GraphQl\Operation;
23
use ApiPlatform\Metadata\GraphQl\Query;
24
use ApiPlatform\Metadata\GraphQl\Subscription;
25
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
26
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
27
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
28
use ApiPlatform\Metadata\ResourceClassResolverInterface;
29
use ApiPlatform\Metadata\Util\Inflector;
30
use ApiPlatform\State\Pagination\Pagination;
31
use GraphQL\Type\Definition\InputObjectType;
32
use GraphQL\Type\Definition\ListOfType;
33
use GraphQL\Type\Definition\NonNull;
34
use GraphQL\Type\Definition\NullableType;
35
use GraphQL\Type\Definition\Type as GraphQLType;
36
use GraphQL\Type\Definition\WrappingType;
37
use Psr\Container\ContainerInterface;
38
use Symfony\Component\Config\Definition\Exception\InvalidTypeException;
39
use Symfony\Component\PropertyInfo\Type;
40
use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface;
41
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
42

43
/**
44
 * Builds the GraphQL fields.
45
 *
46
 * @author Alan Poulain <contact@alanpoulain.eu>
47
 */
48
final class FieldsBuilder implements FieldsBuilderInterface, FieldsBuilderEnumInterface
49
{
50
    private readonly TypeBuilderEnumInterface|TypeBuilderInterface $typeBuilder;
51

52
    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)
53
    {
54
        if ($typeBuilder instanceof TypeBuilderInterface) {
×
55
            @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);
×
56
        }
57
        $this->typeBuilder = $typeBuilder;
×
58
    }
59

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

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

83
        $fieldName = lcfirst('item_query' === $operation->getName() ? $operation->getShortName() : $operation->getName().$operation->getShortName());
×
84

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

90
            return [$fieldName => array_merge($fieldConfiguration, $configuration)];
×
91
        }
92

93
        return [];
×
94
    }
95

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

105
        $fieldName = lcfirst('collection_query' === $operation->getName() ? $operation->getShortName() : $operation->getName().$operation->getShortName());
×
106

107
        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)) {
×
108
            $args = $this->resolveResourceArgs($configuration['args'] ?? [], $operation);
×
109
            $extraArgs = $this->resolveResourceArgs($operation->getExtraArgs() ?? [], $operation);
×
110
            $configuration['args'] = $args ?: $configuration['args'] ?? $fieldConfiguration['args'] + $extraArgs;
×
111

112
            return [Inflector::pluralize($fieldName) => array_merge($fieldConfiguration, $configuration)];
×
113
        }
114

115
        return [];
×
116
    }
117

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

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

131
        $mutationFields[$operation->getName().$operation->getShortName()] = $fieldConfiguration ?? [];
×
132

133
        return $mutationFields;
×
134
    }
135

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

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

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

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

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

161
        return $subscriptionFields;
×
162
    }
163

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

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

180
            return [];
×
181
        }
182

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

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

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

199
            return $fields;
×
200
        }
201

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

209
        ++$depth; // increment the depth for the call to getResourceFieldConfiguration.
×
210

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

220
                if (
221
                    !$propertyTypes
×
222
                    || (!$input && false === $propertyMetadata->isReadable())
×
NEW
223
                    || ($input && false === $propertyMetadata->isWritable())
×
224
                ) {
225
                    continue;
×
226
                }
227

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

239
        if ($operation instanceof Mutation && $input) {
×
240
            $fields['clientMutationId'] = $clientMutationId;
×
241
        }
242

243
        return $fields;
×
244
    }
245

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

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

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

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

274
        return $enumCases;
×
275
    }
276

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

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

290
        return $args;
×
291
    }
292

293
    /**
294
     * Get the field configuration of a resource.
295
     *
296
     * @see http://webonyx.github.io/graphql-php/type-system/object-types/
297
     */
298
    private function getResourceFieldConfiguration(?string $property, ?string $fieldDescription, ?string $deprecationReason, Type $type, string $rootResource, bool $input, Operation $rootOperation, int $depth = 0, bool $forceNullable = false): ?array
299
    {
300
        try {
301
            $isCollectionType = $this->typeBuilder->isCollection($type);
×
302

303
            if (
304
                $isCollectionType
×
305
                && $collectionValueType = $type->getCollectionValueTypes()[0] ?? null
×
306
            ) {
307
                $resourceClass = $collectionValueType->getClassName();
×
308
            } else {
309
                $resourceClass = $type->getClassName();
×
310
            }
311

312
            $resourceOperation = $rootOperation;
×
313
            if ($resourceClass && $depth >= 1 && $this->resourceClassResolver->isResourceClass($resourceClass)) {
×
314
                $resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($resourceClass);
×
315
                $resourceOperation = $resourceMetadataCollection->getOperation($isCollectionType ? 'collection_query' : 'item_query');
×
316
            }
317

318
            if (!$resourceOperation instanceof Operation) {
×
319
                throw new \LogicException('The resource operation should be a GraphQL operation.');
×
320
            }
321

322
            $graphqlType = $this->convertType($type, $input, $resourceOperation, $rootOperation, $resourceClass ?? '', $rootResource, $property, $depth, $forceNullable);
×
323

324
            $graphqlWrappedType = $graphqlType;
×
325
            if ($graphqlType instanceof WrappingType) {
×
326
                if (method_exists($graphqlType, 'getInnermostType')) {
×
327
                    $graphqlWrappedType = $graphqlType->getInnermostType();
×
328
                } else {
329
                    $graphqlWrappedType = $graphqlType->getWrappedType(true);
×
330
                }
331
            }
332
            $isStandardGraphqlType = \in_array($graphqlWrappedType, GraphQLType::getStandardTypes(), true);
×
333
            if ($isStandardGraphqlType) {
×
334
                $resourceClass = '';
×
335
            }
336

337
            // Check mercure attribute if it's a subscription at the root level.
338
            if ($rootOperation instanceof Subscription && null === $property && !$rootOperation->getMercure()) {
×
339
                return null;
×
340
            }
341

342
            $args = [];
×
343

344
            if (!$input && !$rootOperation instanceof Mutation && !$rootOperation instanceof Subscription && !$isStandardGraphqlType && $isCollectionType) {
×
345
                if (!$this->isEnumClass($resourceClass) && $this->pagination->isGraphQlEnabled($resourceOperation)) {
×
346
                    $args = $this->getGraphQlPaginationArgs($resourceOperation);
×
347
                }
348

349
                $args = $this->getFilterArgs($args, $resourceClass, $rootResource, $resourceOperation, $rootOperation, $property, $depth);
×
350
            }
351

352
            if ($this->itemResolverFactory instanceof ResolverFactory) {
×
353
                if ($isStandardGraphqlType || $input) {
×
354
                    $resolve = null;
×
355
                } else {
356
                    $resolve = ($this->itemResolverFactory)($resourceClass, $rootResource, $resourceOperation);
×
357
                }
358
            } else {
359
                if ($isStandardGraphqlType || $input) {
×
360
                    $resolve = null;
×
361
                } elseif (($rootOperation instanceof Mutation || $rootOperation instanceof Subscription) && $depth <= 0) {
×
362
                    $resolve = $rootOperation instanceof Mutation ? ($this->itemMutationResolverFactory)($resourceClass, $rootResource, $resourceOperation) : ($this->itemSubscriptionResolverFactory)($resourceClass, $rootResource, $resourceOperation);
×
363
                } elseif ($this->typeBuilder->isCollection($type)) {
×
364
                    $resolve = ($this->collectionResolverFactory)($resourceClass, $rootResource, $resourceOperation);
×
365
                } else {
366
                    $resolve = ($this->itemResolverFactory)($resourceClass, $rootResource, $resourceOperation);
×
367
                }
368
            }
369

370
            return [
×
371
                'type' => $graphqlType,
×
372
                'description' => $fieldDescription,
×
373
                'args' => $args,
×
374
                'resolve' => $resolve,
×
375
                'deprecationReason' => $deprecationReason,
×
376
            ];
×
377
        } catch (InvalidTypeException) {
×
378
            // just ignore invalid types
379
        }
380

381
        return null;
×
382
    }
383

384
    private function getGraphQlPaginationArgs(Operation $queryOperation): array
385
    {
386
        $paginationType = $this->pagination->getGraphQlPaginationType($queryOperation);
×
387

388
        if ('cursor' === $paginationType) {
×
389
            return [
×
390
                'first' => [
×
391
                    'type' => GraphQLType::int(),
×
392
                    'description' => 'Returns the first n elements from the list.',
×
393
                ],
×
394
                'last' => [
×
395
                    'type' => GraphQLType::int(),
×
396
                    'description' => 'Returns the last n elements from the list.',
×
397
                ],
×
398
                'before' => [
×
399
                    'type' => GraphQLType::string(),
×
400
                    'description' => 'Returns the elements in the list that come before the specified cursor.',
×
401
                ],
×
402
                'after' => [
×
403
                    'type' => GraphQLType::string(),
×
404
                    'description' => 'Returns the elements in the list that come after the specified cursor.',
×
405
                ],
×
406
            ];
×
407
        }
408

409
        $paginationOptions = $this->pagination->getOptions();
×
410

411
        $args = [
×
412
            $paginationOptions['page_parameter_name'] => [
×
413
                'type' => GraphQLType::int(),
×
414
                'description' => 'Returns the current page.',
×
415
            ],
×
416
        ];
×
417

418
        if ($paginationOptions['client_items_per_page']) {
×
419
            $args[$paginationOptions['items_per_page_parameter_name']] = [
×
420
                'type' => GraphQLType::int(),
×
421
                'description' => 'Returns the number of items per page.',
×
422
            ];
×
423
        }
424

425
        return $args;
×
426
    }
427

428
    private function getFilterArgs(array $args, ?string $resourceClass, string $rootResource, Operation $resourceOperation, Operation $rootOperation, ?string $property, int $depth): array
429
    {
430
        if (null === $resourceClass) {
×
431
            return $args;
×
432
        }
433

434
        foreach ($resourceOperation->getFilters() ?? [] as $filterId) {
×
435
            if (!$this->filterLocator->has($filterId)) {
×
436
                continue;
×
437
            }
438

439
            $entityClass = $resourceClass;
×
440
            if ($options = $resourceOperation->getStateOptions()) {
×
441
                if ($options instanceof Options && $options->getEntityClass()) {
×
442
                    $entityClass = $options->getEntityClass();
×
443
                }
444

445
                if ($options instanceof ODMOptions && $options->getDocumentClass()) {
×
446
                    $entityClass = $options->getDocumentClass();
×
447
                }
448
            }
449

450
            foreach ($this->filterLocator->get($filterId)->getDescription($entityClass) as $key => $value) {
×
451
                $nullable = isset($value['required']) ? !$value['required'] : true;
×
452
                $filterType = \in_array($value['type'], Type::$builtinTypes, true) ? new Type($value['type'], $nullable) : new Type('object', $nullable, $value['type']);
×
453
                $graphqlFilterType = $this->convertType($filterType, false, $resourceOperation, $rootOperation, $resourceClass, $rootResource, $property, $depth);
×
454

455
                if (str_ends_with($key, '[]')) {
×
456
                    $graphqlFilterType = GraphQLType::listOf($graphqlFilterType);
×
457
                    $key = substr($key, 0, -2).'_list';
×
458
                }
459

460
                /** @var string $key */
461
                $key = str_replace('.', $this->nestingSeparator, $key);
×
462

463
                parse_str($key, $parsed);
×
464
                if (\array_key_exists($key, $parsed) && \is_array($parsed[$key])) {
×
465
                    $parsed = [$key => ''];
×
466
                }
467
                array_walk_recursive($parsed, static function (&$value) use ($graphqlFilterType): void {
×
468
                    $value = $graphqlFilterType;
×
469
                });
×
470
                $args = $this->mergeFilterArgs($args, $parsed, $resourceOperation, $key);
×
471
            }
472
        }
473

474
        return $this->convertFilterArgsToTypes($args);
×
475
    }
476

477
    private function mergeFilterArgs(array $args, array $parsed, Operation $operation = null, string $original = ''): array
478
    {
479
        foreach ($parsed as $key => $value) {
×
480
            // Never override keys that cannot be merged
481
            if (isset($args[$key]) && !\is_array($args[$key])) {
×
482
                continue;
×
483
            }
484

485
            if (\is_array($value)) {
×
486
                $value = $this->mergeFilterArgs($args[$key] ?? [], $value);
×
487
                if (!isset($value['#name'])) {
×
488
                    $name = (false === $pos = strrpos($original, '[')) ? $original : substr($original, 0, (int) $pos);
×
489
                    $value['#name'] = ($operation ? $operation->getShortName() : '').'Filter_'.strtr($name, ['[' => '_', ']' => '', '.' => '__']);
×
490
                }
491
            }
492

493
            $args[$key] = $value;
×
494
        }
495

496
        return $args;
×
497
    }
498

499
    private function convertFilterArgsToTypes(array $args): array
500
    {
501
        foreach ($args as $key => $value) {
×
502
            if (strpos($key, '.')) {
×
503
                // Declare relations/nested fields in a GraphQL compatible syntax.
504
                $args[str_replace('.', $this->nestingSeparator, $key)] = $value;
×
505
                unset($args[$key]);
×
506
            }
507
        }
508

509
        foreach ($args as $key => $value) {
×
510
            if (!\is_array($value) || !isset($value['#name'])) {
×
511
                continue;
×
512
            }
513

514
            $name = $value['#name'];
×
515

516
            if ($this->typesContainer->has($name)) {
×
517
                $args[$key] = $this->typesContainer->get($name);
×
518
                continue;
×
519
            }
520

521
            unset($value['#name']);
×
522

523
            $filterArgType = GraphQLType::listOf(new InputObjectType([
×
524
                'name' => $name,
×
525
                'fields' => $this->convertFilterArgsToTypes($value),
×
526
            ]));
×
527

528
            $this->typesContainer->set($name, $filterArgType);
×
529

530
            $args[$key] = $filterArgType;
×
531
        }
532

533
        return $args;
×
534
    }
535

536
    /**
537
     * Converts a built-in type to its GraphQL equivalent.
538
     *
539
     * @throws InvalidTypeException
540
     */
541
    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
542
    {
543
        $graphqlType = $this->typeConverter->convertType($type, $input, $rootOperation, $resourceClass, $rootResource, $property, $depth);
×
544

545
        if (null === $graphqlType) {
×
546
            throw new InvalidTypeException(sprintf('The type "%s" is not supported.', $type->getBuiltinType()));
×
547
        }
548

549
        if (\is_string($graphqlType)) {
×
550
            if (!$this->typesContainer->has($graphqlType)) {
×
551
                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));
×
552
            }
553

554
            $graphqlType = $this->typesContainer->get($graphqlType);
×
555
        }
556

557
        if ($this->typeBuilder->isCollection($type)) {
×
558
            if (!$input && !$this->isEnumClass($resourceClass) && $this->pagination->isGraphQlEnabled($resourceOperation)) {
×
559
                // Deprecated path, to remove in API Platform 4.
560
                if ($this->typeBuilder instanceof TypeBuilderInterface) {
×
561
                    return $this->typeBuilder->getResourcePaginatedCollectionType($graphqlType, $resourceClass, $resourceOperation);
×
562
                }
563

564
                return $this->typeBuilder->getPaginatedCollectionType($graphqlType, $resourceOperation);
×
565
            }
566

567
            return GraphQLType::listOf($graphqlType);
×
568
        }
569

570
        return $forceNullable || !$graphqlType instanceof NullableType || $type->isNullable() || ($rootOperation instanceof Mutation && 'update' === $rootOperation->getName())
×
571
            ? $graphqlType
×
572
            : GraphQLType::nonNull($graphqlType);
×
573
    }
574

575
    private function normalizePropertyName(string $property, string $resourceClass): string
576
    {
577
        if (null === $this->nameConverter) {
×
578
            return $property;
×
579
        }
580
        if ($this->nameConverter instanceof AdvancedNameConverterInterface) {
×
581
            return $this->nameConverter->normalize($property, $resourceClass);
×
582
        }
583

584
        return $this->nameConverter->normalize($property);
×
585
    }
586
}
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