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

api-platform / core / 10323454690

09 Aug 2024 05:46PM UTC coverage: 7.841%. Remained the same
10323454690

push

github

soyuka
Merge 3.4

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

1896 existing lines in 118 files now uncovered.

12688 of 161818 relevant lines covered (7.84%)

26.86 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|FieldsBuilderInterface $fieldsBuilder)
36
    {
37
        if ($this->fieldsBuilder instanceof FieldsBuilderInterface) {
441✔
38
            @trigger_error(\sprintf('$fieldsBuilder argument of SchemaBuilder implementing "%s" is deprecated since API Platform 3.1. It has to implement "%s" instead.', FieldsBuilderInterface::class, FieldsBuilderEnumInterface::class), \E_USER_DEPRECATED);
441✔
39
        }
40
    }
41

42
    public function getSchema(): Schema
43
    {
44
        $types = $this->typesFactory->getTypes();
435✔
45
        foreach ($types as $typeId => $type) {
435✔
46
            $this->typesContainer->set($typeId, $type);
435✔
47
        }
48

49
        $queryFields = ['node' => $this->fieldsBuilder->getNodeQueryFields()];
435✔
50
        $mutationFields = [];
435✔
51
        $subscriptionFields = [];
435✔
52

53
        foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) {
435✔
54
            $resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($resourceClass);
435✔
55
            foreach ($resourceMetadataCollection as $resourceMetadata) {
435✔
56
                foreach ($resourceMetadata->getGraphQlOperations() ?? [] as $operation) {
435✔
57
                    $configuration = null !== $operation->getArgs() ? ['args' => $operation->getArgs()] : [];
435✔
58

59
                    if ($operation instanceof Query && $operation instanceof CollectionOperationInterface) {
435✔
60
                        $queryFields += $this->fieldsBuilder->getCollectionQueryFields($resourceClass, $operation, $configuration);
435✔
61

62
                        continue;
435✔
63
                    }
64

65
                    if ($operation instanceof Query) {
435✔
66
                        $queryFields += $this->fieldsBuilder->getItemQueryFields($resourceClass, $operation, $configuration);
435✔
67

68
                        continue;
435✔
69
                    }
70

71
                    if ($operation instanceof Subscription && $operation->getMercure()) {
411✔
UNCOV
72
                        $subscriptionFields += $this->fieldsBuilder->getSubscriptionFields($resourceClass, $operation);
408✔
73

UNCOV
74
                        continue;
408✔
75
                    }
76

77
                    $mutationFields += $this->fieldsBuilder->getMutationFields($resourceClass, $operation);
411✔
78
                }
79
            }
80
        }
81

82
        $queryType = new ObjectType([
435✔
83
            'name' => 'Query',
435✔
84
            'fields' => $queryFields,
435✔
85
        ]);
435✔
86
        $this->typesContainer->set('Query', $queryType);
435✔
87

88
        $schema = [
435✔
89
            'query' => $queryType,
435✔
90
            'typeLoader' => function (string $typeName): ?NamedType {
435✔
91
                try {
UNCOV
92
                    $type = $this->typesContainer->get($typeName);
60✔
UNCOV
93
                } catch (TypeNotFoundException) {
3✔
UNCOV
94
                    return null;
3✔
95
                }
96

UNCOV
97
                return Type::getNamedType($type);
60✔
98
            },
435✔
99
        ];
435✔
100

101
        if ($mutationFields) {
435✔
102
            $mutationType = new ObjectType([
411✔
103
                'name' => 'Mutation',
411✔
104
                'fields' => $mutationFields,
411✔
105
            ]);
411✔
106
            $this->typesContainer->set('Mutation', $mutationType);
411✔
107

108
            $schema['mutation'] = $mutationType;
411✔
109
        }
110

111
        if ($subscriptionFields) {
435✔
UNCOV
112
            $subscriptionType = new ObjectType([
408✔
UNCOV
113
                'name' => 'Subscription',
408✔
UNCOV
114
                'fields' => $subscriptionFields,
408✔
UNCOV
115
            ]);
408✔
UNCOV
116
            $this->typesContainer->set('Subscription', $subscriptionType);
408✔
117

UNCOV
118
            $schema['subscription'] = $subscriptionType;
408✔
119
        }
120

121
        return new Schema($schema);
435✔
122
    }
123
}
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