1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the API Platform project. |
5
|
|
|
* |
6
|
|
|
* (c) Kévin Dunglas <[email protected]> |
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\Core\GraphQl\Type; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
17
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface; |
18
|
|
|
use GraphQL\Type\Definition\ObjectType; |
19
|
|
|
use GraphQL\Type\Definition\WrappingType; |
20
|
|
|
use GraphQL\Type\Schema; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Builds the GraphQL schema. |
24
|
|
|
* |
25
|
|
|
* @experimental |
26
|
|
|
* |
27
|
|
|
* @author Raoul Clais <[email protected]> |
28
|
|
|
* @author Alan Poulain <[email protected]> |
29
|
|
|
* @author Kévin Dunglas <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
final class SchemaBuilder implements SchemaBuilderInterface |
32
|
|
|
{ |
33
|
|
|
private $resourceNameCollectionFactory; |
34
|
|
|
private $resourceMetadataFactory; |
35
|
|
|
private $typesFactory; |
36
|
|
|
private $typesContainer; |
37
|
|
|
private $fieldsBuilder; |
38
|
|
|
|
39
|
|
|
public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, TypesFactoryInterface $typesFactory, TypesContainerInterface $typesContainer, FieldsBuilderInterface $fieldsBuilder) |
40
|
|
|
{ |
41
|
|
|
$this->resourceNameCollectionFactory = $resourceNameCollectionFactory; |
42
|
|
|
$this->resourceMetadataFactory = $resourceMetadataFactory; |
43
|
|
|
$this->typesFactory = $typesFactory; |
44
|
|
|
$this->typesContainer = $typesContainer; |
45
|
|
|
$this->fieldsBuilder = $fieldsBuilder; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getSchema(): Schema |
49
|
|
|
{ |
50
|
|
|
$types = $this->typesFactory->getTypes(); |
51
|
|
|
foreach ($types as $typeId => $type) { |
52
|
|
|
$this->typesContainer->set($typeId, $type); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$queryFields = ['node' => $this->fieldsBuilder->getNodeQueryFields()]; |
56
|
|
|
$mutationFields = []; |
57
|
|
|
|
58
|
|
|
foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) { |
59
|
|
|
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
60
|
|
|
$graphqlConfiguration = $resourceMetadata->getGraphql() ?? []; |
61
|
|
|
foreach ($graphqlConfiguration as $operationName => $value) { |
62
|
|
|
if ('query' === $operationName) { |
63
|
|
|
$queryFields += $this->fieldsBuilder->getQueryFields($resourceClass, $resourceMetadata, $operationName, [], []); |
64
|
|
|
|
65
|
|
|
continue; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if ($resourceMetadata->getGraphqlAttribute($operationName, 'item_query')) { |
69
|
|
|
$queryFields += $this->fieldsBuilder->getQueryFields($resourceClass, $resourceMetadata, $operationName, $value, false); |
70
|
|
|
|
71
|
|
|
continue; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($resourceMetadata->getGraphqlAttribute($operationName, 'collection_query')) { |
75
|
|
|
$queryFields += $this->fieldsBuilder->getQueryFields($resourceClass, $resourceMetadata, $operationName, false, $value); |
76
|
|
|
|
77
|
|
|
continue; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$mutationFields += $this->fieldsBuilder->getMutationFields($resourceClass, $resourceMetadata, $operationName); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$schema = [ |
85
|
|
|
'query' => new ObjectType([ |
86
|
|
|
'name' => 'Query', |
87
|
|
|
'fields' => $queryFields, |
88
|
|
|
]), |
89
|
|
|
'typeLoader' => function ($name) { |
90
|
|
|
$type = $this->typesContainer->get($name); |
91
|
|
|
|
92
|
|
|
if ($type instanceof WrappingType) { |
93
|
|
|
return $type->getWrappedType(true); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $type; |
97
|
|
|
}, |
98
|
|
|
]; |
99
|
|
|
|
100
|
|
|
if ($mutationFields) { |
101
|
|
|
$schema['mutation'] = new ObjectType([ |
102
|
|
|
'name' => 'Mutation', |
103
|
|
|
'fields' => $mutationFields, |
104
|
|
|
]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return new Schema($schema); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|