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

api-platform / core / 14635100171

24 Apr 2025 06:39AM UTC coverage: 8.271% (+0.02%) from 8.252%
14635100171

Pull #6904

github

web-flow
Merge c9cefd82e into a3e5e53ea
Pull Request #6904: feat(graphql): added support for graphql subscriptions to work for actions

0 of 73 new or added lines in 3 files covered. (0.0%)

1999 existing lines in 144 files now uncovered.

13129 of 158728 relevant lines covered (8.27%)

13.6 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
    {
37
    }
161✔
38

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

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

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

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

59
                        continue;
159✔
60
                    }
61

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

65
                        continue;
159✔
66
                    }
67

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

UNCOV
71
                        continue;
133✔
72
                    }
73

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

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

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

94
                return Type::getNamedType($type);
22✔
95
            },
159✔
96
        ];
159✔
97

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

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

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

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

118
        return new Schema($schema);
159✔
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