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

api-platform / core / 6981542872

24 Nov 2023 01:48PM UTC coverage: 37.261% (-0.02%) from 37.284%
6981542872

push

github

web-flow
feat(graphql): support enum collection as property (#5955)

Co-authored-by: josef.wagner <josef.wagner@hf-solutions.co>

0 of 40 new or added lines in 6 files covered. (0.0%)

2 existing lines in 2 files now uncovered.

10287 of 27608 relevant lines covered (37.26%)

20.52 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())
×
223
                    || ($input && $operation instanceof Mutation && 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
    {
NEW
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
        foreach ($rEnum->getCases() as $rCase) {
×
260
            $enumCase = ['value' => $rCase->getBackingValue()];
×
261
            $propertyMetadata = $this->propertyMetadataFactory->create($enumClass, $rCase->getName());
×
262
            if ($enumCaseDescription = $propertyMetadata->getDescription()) {
×
263
                $enumCase['description'] = $enumCaseDescription;
×
264
            }
265
            $enumCases[$rCase->getName()] = $enumCase;
×
266
        }
267

268
        return $enumCases;
×
269
    }
270

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

281
            $args[$id]['type'] = $this->typeConverter->resolveType($arg['type']);
×
282
        }
283

284
        return $args;
×
285
    }
286

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

297
            if (
298
                $isCollectionType
×
299
                && $collectionValueType = $type->getCollectionValueTypes()[0] ?? null
×
300
            ) {
301
                $resourceClass = $collectionValueType->getClassName();
×
302
            } else {
303
                $resourceClass = $type->getClassName();
×
304
            }
305

306
            $resourceOperation = $rootOperation;
×
307
            if ($resourceClass && $depth >= 1 && $this->resourceClassResolver->isResourceClass($resourceClass)) {
×
308
                $resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($resourceClass);
×
309
                $resourceOperation = $resourceMetadataCollection->getOperation($isCollectionType ? 'collection_query' : 'item_query');
×
310
            }
311

312
            if (!$resourceOperation instanceof Operation) {
×
313
                throw new \LogicException('The resource operation should be a GraphQL operation.');
×
314
            }
315

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

318
            $graphqlWrappedType = $graphqlType;
×
319
            if ($graphqlType instanceof WrappingType) {
×
320
                if (method_exists($graphqlType, 'getInnermostType')) {
×
321
                    $graphqlWrappedType = $graphqlType->getInnermostType();
×
322
                } else {
323
                    $graphqlWrappedType = $graphqlType->getWrappedType(true);
×
324
                }
325
            }
326
            $isStandardGraphqlType = \in_array($graphqlWrappedType, GraphQLType::getStandardTypes(), true);
×
327
            if ($isStandardGraphqlType) {
×
328
                $resourceClass = '';
×
329
            }
330

331
            // Check mercure attribute if it's a subscription at the root level.
332
            if ($rootOperation instanceof Subscription && null === $property && !$rootOperation->getMercure()) {
×
333
                return null;
×
334
            }
335

336
            $args = [];
×
337

338
            if (!$input && !$rootOperation instanceof Mutation && !$rootOperation instanceof Subscription && !$isStandardGraphqlType && $isCollectionType) {
×
NEW
339
                if (!$this->isEnumClass($resourceClass) && $this->pagination->isGraphQlEnabled($resourceOperation)) {
×
340
                    $args = $this->getGraphQlPaginationArgs($resourceOperation);
×
341
                }
342

343
                $args = $this->getFilterArgs($args, $resourceClass, $rootResource, $resourceOperation, $rootOperation, $property, $depth);
×
344
            }
345

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

364
            return [
×
365
                'type' => $graphqlType,
×
366
                'description' => $fieldDescription,
×
367
                'args' => $args,
×
368
                'resolve' => $resolve,
×
369
                'deprecationReason' => $deprecationReason,
×
370
            ];
×
371
        } catch (InvalidTypeException) {
×
372
            // just ignore invalid types
373
        }
374

375
        return null;
×
376
    }
377

378
    private function getGraphQlPaginationArgs(Operation $queryOperation): array
379
    {
380
        $paginationType = $this->pagination->getGraphQlPaginationType($queryOperation);
×
381

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

403
        $paginationOptions = $this->pagination->getOptions();
×
404

405
        $args = [
×
406
            $paginationOptions['page_parameter_name'] => [
×
407
                'type' => GraphQLType::int(),
×
408
                'description' => 'Returns the current page.',
×
409
            ],
×
410
        ];
×
411

412
        if ($paginationOptions['client_items_per_page']) {
×
413
            $args[$paginationOptions['items_per_page_parameter_name']] = [
×
414
                'type' => GraphQLType::int(),
×
415
                'description' => 'Returns the number of items per page.',
×
416
            ];
×
417
        }
418

419
        return $args;
×
420
    }
421

422
    private function getFilterArgs(array $args, ?string $resourceClass, string $rootResource, Operation $resourceOperation, Operation $rootOperation, ?string $property, int $depth): array
423
    {
424
        if (null === $resourceClass) {
×
425
            return $args;
×
426
        }
427

428
        foreach ($resourceOperation->getFilters() ?? [] as $filterId) {
×
429
            if (!$this->filterLocator->has($filterId)) {
×
430
                continue;
×
431
            }
432

433
            $entityClass = $resourceClass;
×
434
            if ($options = $resourceOperation->getStateOptions()) {
×
435
                if ($options instanceof Options && $options->getEntityClass()) {
×
436
                    $entityClass = $options->getEntityClass();
×
437
                }
438

439
                if ($options instanceof ODMOptions && $options->getDocumentClass()) {
×
440
                    $entityClass = $options->getDocumentClass();
×
441
                }
442
            }
443

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

449
                if (str_ends_with($key, '[]')) {
×
450
                    $graphqlFilterType = GraphQLType::listOf($graphqlFilterType);
×
451
                    $key = substr($key, 0, -2).'_list';
×
452
                }
453

454
                /** @var string $key */
455
                $key = str_replace('.', $this->nestingSeparator, $key);
×
456

457
                parse_str($key, $parsed);
×
458
                if (\array_key_exists($key, $parsed) && \is_array($parsed[$key])) {
×
459
                    $parsed = [$key => ''];
×
460
                }
461
                array_walk_recursive($parsed, static function (&$value) use ($graphqlFilterType): void {
×
462
                    $value = $graphqlFilterType;
×
463
                });
×
464
                $args = $this->mergeFilterArgs($args, $parsed, $resourceOperation, $key);
×
465
            }
466
        }
467

468
        return $this->convertFilterArgsToTypes($args);
×
469
    }
470

471
    private function mergeFilterArgs(array $args, array $parsed, Operation $operation = null, string $original = ''): array
472
    {
473
        foreach ($parsed as $key => $value) {
×
474
            // Never override keys that cannot be merged
475
            if (isset($args[$key]) && !\is_array($args[$key])) {
×
476
                continue;
×
477
            }
478

479
            if (\is_array($value)) {
×
480
                $value = $this->mergeFilterArgs($args[$key] ?? [], $value);
×
481
                if (!isset($value['#name'])) {
×
482
                    $name = (false === $pos = strrpos($original, '[')) ? $original : substr($original, 0, (int) $pos);
×
483
                    $value['#name'] = ($operation ? $operation->getShortName() : '').'Filter_'.strtr($name, ['[' => '_', ']' => '', '.' => '__']);
×
484
                }
485
            }
486

487
            $args[$key] = $value;
×
488
        }
489

490
        return $args;
×
491
    }
492

493
    private function convertFilterArgsToTypes(array $args): array
494
    {
495
        foreach ($args as $key => $value) {
×
496
            if (strpos($key, '.')) {
×
497
                // Declare relations/nested fields in a GraphQL compatible syntax.
498
                $args[str_replace('.', $this->nestingSeparator, $key)] = $value;
×
499
                unset($args[$key]);
×
500
            }
501
        }
502

503
        foreach ($args as $key => $value) {
×
504
            if (!\is_array($value) || !isset($value['#name'])) {
×
505
                continue;
×
506
            }
507

508
            $name = $value['#name'];
×
509

510
            if ($this->typesContainer->has($name)) {
×
511
                $args[$key] = $this->typesContainer->get($name);
×
512
                continue;
×
513
            }
514

515
            unset($value['#name']);
×
516

517
            $filterArgType = GraphQLType::listOf(new InputObjectType([
×
518
                'name' => $name,
×
519
                'fields' => $this->convertFilterArgsToTypes($value),
×
520
            ]));
×
521

522
            $this->typesContainer->set($name, $filterArgType);
×
523

524
            $args[$key] = $filterArgType;
×
525
        }
526

527
        return $args;
×
528
    }
529

530
    /**
531
     * Converts a built-in type to its GraphQL equivalent.
532
     *
533
     * @throws InvalidTypeException
534
     */
535
    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
536
    {
537
        $graphqlType = $this->typeConverter->convertType($type, $input, $rootOperation, $resourceClass, $rootResource, $property, $depth);
×
538

539
        if (null === $graphqlType) {
×
540
            throw new InvalidTypeException(sprintf('The type "%s" is not supported.', $type->getBuiltinType()));
×
541
        }
542

543
        if (\is_string($graphqlType)) {
×
544
            if (!$this->typesContainer->has($graphqlType)) {
×
545
                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));
×
546
            }
547

548
            $graphqlType = $this->typesContainer->get($graphqlType);
×
549
        }
550

551
        if ($this->typeBuilder->isCollection($type)) {
×
NEW
552
            if (!$input && !$this->isEnumClass($resourceClass) && $this->pagination->isGraphQlEnabled($resourceOperation)) {
×
553
                // Deprecated path, to remove in API Platform 4.
554
                if ($this->typeBuilder instanceof TypeBuilderInterface) {
×
555
                    return $this->typeBuilder->getResourcePaginatedCollectionType($graphqlType, $resourceClass, $resourceOperation);
×
556
                }
557

558
                return $this->typeBuilder->getPaginatedCollectionType($graphqlType, $resourceOperation);
×
559
            }
560

561
            return GraphQLType::listOf($graphqlType);
×
562
        }
563

564
        return $forceNullable || !$graphqlType instanceof NullableType || $type->isNullable() || ($rootOperation instanceof Mutation && 'update' === $rootOperation->getName())
×
565
            ? $graphqlType
×
566
            : GraphQLType::nonNull($graphqlType);
×
567
    }
568

569
    private function normalizePropertyName(string $property, string $resourceClass): string
570
    {
571
        if (null === $this->nameConverter) {
×
572
            return $property;
×
573
        }
574
        if ($this->nameConverter instanceof AdvancedNameConverterInterface) {
×
575
            return $this->nameConverter->normalize($property, $resourceClass);
×
576
        }
577

578
        return $this->nameConverter->normalize($property);
×
579
    }
580
}
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