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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

0 of 2 new or added lines in 1 file covered. (0.0%)

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 hits per line

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

100.0
/src/GraphQl/Type/SchemaBuilder.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\Metadata\CollectionOperationInterface;
17
use ApiPlatform\Metadata\GraphQl\Query;
18
use ApiPlatform\Metadata\GraphQl\Subscription;
19
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
20
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
21
use GraphQL\Type\Definition\NamedType;
22
use GraphQL\Type\Definition\ObjectType;
23
use GraphQL\Type\Definition\Type;
24
use GraphQL\Type\Schema;
25

26
/**
27
 * Builds the GraphQL schema.
28
 *
29
 * @author Raoul Clais <raoul.clais@gmail.com>
30
 * @author Alan Poulain <contact@alanpoulain.eu>
31
 * @author Kévin Dunglas <dunglas@gmail.com>
32
 */
33
final class SchemaBuilder implements SchemaBuilderInterface
34
{
35
    public function __construct(private readonly ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, private readonly TypesFactoryInterface $typesFactory, private readonly TypesContainerInterface $typesContainer, private readonly FieldsBuilderEnumInterface $fieldsBuilder)
36
    {
UNCOV
37
    }
148✔
38

39
    public function getSchema(): Schema
40
    {
UNCOV
41
        $types = $this->typesFactory->getTypes();
146✔
UNCOV
42
        foreach ($types as $typeId => $type) {
146✔
UNCOV
43
            $this->typesContainer->set($typeId, $type);
146✔
44
        }
45

UNCOV
46
        $queryFields = ['node' => $this->fieldsBuilder->getNodeQueryFields()];
146✔
UNCOV
47
        $mutationFields = [];
146✔
UNCOV
48
        $subscriptionFields = [];
146✔
49

UNCOV
50
        foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) {
146✔
UNCOV
51
            $resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($resourceClass);
146✔
UNCOV
52
            foreach ($resourceMetadataCollection as $resourceMetadata) {
146✔
UNCOV
53
                foreach ($resourceMetadata->getGraphQlOperations() ?? [] as $operation) {
146✔
UNCOV
54
                    $configuration = null !== $operation->getArgs() ? ['args' => $operation->getArgs()] : [];
146✔
55

UNCOV
56
                    if ($operation instanceof Query && $operation instanceof CollectionOperationInterface) {
146✔
UNCOV
57
                        $queryFields += $this->fieldsBuilder->getCollectionQueryFields($resourceClass, $operation, $configuration);
146✔
58

UNCOV
59
                        continue;
146✔
60
                    }
61

UNCOV
62
                    if ($operation instanceof Query) {
146✔
UNCOV
63
                        $queryFields += $this->fieldsBuilder->getItemQueryFields($resourceClass, $operation, $configuration);
146✔
64

UNCOV
65
                        continue;
146✔
66
                    }
67

UNCOV
68
                    if ($operation instanceof Subscription && $operation->getMercure()) {
138✔
69
                        $subscriptionFields += $this->fieldsBuilder->getSubscriptionFields($resourceClass, $operation);
133✔
70

71
                        continue;
133✔
72
                    }
73

UNCOV
74
                    $mutationFields += $this->fieldsBuilder->getMutationFields($resourceClass, $operation);
138✔
75
                }
76
            }
77
        }
78

UNCOV
79
        $queryType = new ObjectType([
146✔
UNCOV
80
            'name' => 'Query',
146✔
UNCOV
81
            'fields' => $queryFields,
146✔
UNCOV
82
        ]);
146✔
UNCOV
83
        $this->typesContainer->set('Query', $queryType);
146✔
84

UNCOV
85
        $schema = [
146✔
UNCOV
86
            'query' => $queryType,
146✔
UNCOV
87
            'typeLoader' => function (string $typeName): ?NamedType {
146✔
88
                try {
UNCOV
89
                    $type = $this->typesContainer->get($typeName);
21✔
90
                } catch (TypeNotFoundException) {
1✔
91
                    return null;
1✔
92
                }
93

UNCOV
94
                return Type::getNamedType($type);
21✔
UNCOV
95
            },
146✔
UNCOV
96
        ];
146✔
97

UNCOV
98
        if ($mutationFields) {
146✔
UNCOV
99
            $mutationType = new ObjectType([
138✔
UNCOV
100
                'name' => 'Mutation',
138✔
UNCOV
101
                'fields' => $mutationFields,
138✔
UNCOV
102
            ]);
138✔
UNCOV
103
            $this->typesContainer->set('Mutation', $mutationType);
138✔
104

UNCOV
105
            $schema['mutation'] = $mutationType;
138✔
106
        }
107

UNCOV
108
        if ($subscriptionFields) {
146✔
109
            $subscriptionType = new ObjectType([
133✔
110
                'name' => 'Subscription',
133✔
111
                'fields' => $subscriptionFields,
133✔
112
            ]);
133✔
113
            $this->typesContainer->set('Subscription', $subscriptionType);
133✔
114

115
            $schema['subscription'] = $subscriptionType;
133✔
116
        }
117

UNCOV
118
        return new Schema($schema);
146✔
119
    }
120
}
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

© 2025 Coveralls, Inc