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

api-platform / core / 9869461710

10 Jul 2024 06:43AM UTC coverage: 63.421%. Remained the same
9869461710

push

github

soyuka
cs(jsonld): non-nullable not needed

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

589 existing lines in 21 files now uncovered.

11178 of 17625 relevant lines covered (63.42%)

53.23 hits per line

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

92.31
/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
    {
UNCOV
37
        if ($this->fieldsBuilder instanceof FieldsBuilderInterface) {
3✔
UNCOV
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);
3✔
39
        }
40
    }
41

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

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

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

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

UNCOV
62
                        continue;
3✔
63
                    }
64

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

UNCOV
68
                        continue;
3✔
69
                    }
70

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

UNCOV
74
                        continue;
3✔
75
                    }
76

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

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

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

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

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

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

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

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

UNCOV
121
        return new Schema($schema);
3✔
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